From f5dc9eadaafc74e606aef849145693ece2e743e9 Mon Sep 17 00:00:00 2001 From: zhushuang02 Date: Fri, 15 Dec 2017 10:22:59 +0800 Subject: [PATCH] Add refer code to get started --- doc/getstarted/concepts/src/infer.py | 14 ++++++++++++++ doc/getstarted/concepts/src/train.py | 5 +++++ doc/getstarted/concepts/use_concepts_cn.rst | 5 +++++ 3 files changed, 24 insertions(+) create mode 100644 doc/getstarted/concepts/src/infer.py diff --git a/doc/getstarted/concepts/src/infer.py b/doc/getstarted/concepts/src/infer.py new file mode 100644 index 00000000000..780d831da83 --- /dev/null +++ b/doc/getstarted/concepts/src/infer.py @@ -0,0 +1,14 @@ +import paddle.v2 as paddle +import numpy as np + +paddle.init(use_gpu=False) +x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(2)) +y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear()) + +# loading the model which generated by training +with open('hello_params_pass_90.tar', 'r') as f: + parameters = paddle.parameters.Parameters.from_tar(f) + +i = [[[1, 2]]] + +print paddle.infer(output_layer=y_predict, parameters=parameters, input=i) diff --git a/doc/getstarted/concepts/src/train.py b/doc/getstarted/concepts/src/train.py index 8aceb23406a..4bccbfca3c7 100644 --- a/doc/getstarted/concepts/src/train.py +++ b/doc/getstarted/concepts/src/train.py @@ -26,6 +26,11 @@ def event_handler(event): if event.batch_id % 1 == 0: print "Pass %d, Batch %d, Cost %f" % (event.pass_id, event.batch_id, event.cost) + # product model every 10 pass + if isinstance(event, paddle.event.EndPass): + if event.pass_id % 10 == 0: + with open('params_pass_%d.tar' % event.pass_id, 'w') as f: + trainer.save_parameter_to_tar(f) # define training dataset reader diff --git a/doc/getstarted/concepts/use_concepts_cn.rst b/doc/getstarted/concepts/use_concepts_cn.rst index c243083794b..7ff0b29ac3c 100644 --- a/doc/getstarted/concepts/use_concepts_cn.rst +++ b/doc/getstarted/concepts/use_concepts_cn.rst @@ -147,4 +147,9 @@ PaddlePaddle支持不同类型的输入数据,主要包括四种类型,和 .. literalinclude:: src/train.py :linenos: +使用以上训练好的模型进行预测的例子: + +.. literalinclude:: src/infer.py + :linenos: + 有关线性回归的实际应用,可以参考PaddlePaddle book的 `第一章节 `_。 -- GitLab