diff --git a/01.fit_a_line/README.cn.md b/01.fit_a_line/README.cn.md index dac7188109b96871c4ecca2b380e42cce2ca57db..30e78a42ddb5c36072485c5dbecd63433dc91065 100644 --- a/01.fit_a_line/README.cn.md +++ b/01.fit_a_line/README.cn.md @@ -200,6 +200,11 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 + + if isinstance(event, paddle.event.EndPass): + if event.pass_id % 10 == 0: + with open('params_pass_%d.tar' % event.pass_id, 'w') as f: + parameters.to_tar(f) ``` ### 开始训练 @@ -217,6 +222,37 @@ trainer.train( ![png](./image/train_and_test.png) +### 应用模型 + +#### 1. 生成测试数据 + +```python +test_data_creator = paddle.dataset.uci_housing.test() +test_data = [] +test_label = [] + +for item in test_data_creator(): + test_data.append((item[0],)) + test_label.append(item[1]) + if len(test_data) == 5: + break +``` + +#### 2. 推测 inference + +```python +# load parameters from tar file. +# users can remove the comments and change the model name +# with open('params_pass_20.tar', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + +probs = paddle.infer( + output_layer=y_predict, parameters=parameters, input=test_data) + +for i in xrange(len(probs)): + print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) +``` + ## 总结 在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。 diff --git a/01.fit_a_line/README.md b/01.fit_a_line/README.md index 76c312fe9faa17095f1ef6b2a8cbb4ee0f35c092..814cd1c110581318e59c6a43e7ce47227d640cd3 100644 --- a/01.fit_a_line/README.md +++ b/01.fit_a_line/README.md @@ -205,6 +205,11 @@ def event_handler_plot(event): plot_cost.plot() step += 1 + + if isinstance(event, paddle.event.EndPass): + if event.pass_id % 10 == 0: + with open('params_pass_%d.tar' % event.pass_id, 'w') as f: + parameters.to_tar(f) ``` ### Start Training @@ -222,6 +227,37 @@ trainer.train( ![png](./image/train_and_test.png) +### Apply model + +#### 1. generate testing data + +```python +test_data_creator = paddle.dataset.uci_housing.test() +test_data = [] +test_label = [] + +for item in test_data_creator(): + test_data.append((item[0],)) + test_label.append(item[1]) + if len(test_data) == 5: + break +``` + +#### 2. inference + +```python +# load parameters from tar file. +# users can remove the comments and change the model name +# with open('params_pass_20.tar', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + +probs = paddle.infer( + output_layer=y_predict, parameters=parameters, input=test_data) + +for i in xrange(len(probs)): + print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) +``` + ## Summary This chapter introduces *Linear Regression* and how to train and test this model with PaddlePaddle, using the UCI Housing Data Set. Because a large number of more complex models and techniques are derived from linear regression, it is important to understand its underlying theory and limitation. diff --git a/01.fit_a_line/image/ranges.png b/01.fit_a_line/image/ranges.png index 3661ace169ad2bbda6355cff615b771108b258de..5d86b12715f46afbafb7d50e2938e184219b5b95 100644 Binary files a/01.fit_a_line/image/ranges.png and b/01.fit_a_line/image/ranges.png differ diff --git a/01.fit_a_line/index.cn.html b/01.fit_a_line/index.cn.html index cfa3f2b369276e72f94e0df926fb0c6b4bdb2500..933e5a4d8bfb53cb50a9675ea907d939203742da 100644 --- a/01.fit_a_line/index.cn.html +++ b/01.fit_a_line/index.cn.html @@ -242,6 +242,11 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 + + if isinstance(event, paddle.event.EndPass): + if event.pass_id % 10 == 0: + with open('params_pass_%d.tar' % event.pass_id, 'w') as f: + parameters.to_tar(f) ``` ### 开始训练 @@ -259,6 +264,37 @@ trainer.train( ![png](./image/train_and_test.png) +### 应用模型 + +#### 1. 生成测试数据 + +```python +test_data_creator = paddle.dataset.uci_housing.test() +test_data = [] +test_label = [] + +for item in test_data_creator(): + test_data.append((item[0],)) + test_label.append(item[1]) + if len(test_data) == 5: + break +``` + +#### 2. 推测 inference + +```python +# load parameters from tar file. +# users can remove the comments and change the model name +# with open('params_pass_20.tar', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + +probs = paddle.infer( + output_layer=y_predict, parameters=parameters, input=test_data) + +for i in xrange(len(probs)): + print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) +``` + ## 总结 在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。 diff --git a/01.fit_a_line/index.html b/01.fit_a_line/index.html index 7ad0c91c4ff6275f546f61df17ca80bcd1847aa8..f143f7fa65fe9ab4168739eaa9495292c64e3326 100644 --- a/01.fit_a_line/index.html +++ b/01.fit_a_line/index.html @@ -247,6 +247,11 @@ def event_handler_plot(event): plot_cost.plot() step += 1 + + if isinstance(event, paddle.event.EndPass): + if event.pass_id % 10 == 0: + with open('params_pass_%d.tar' % event.pass_id, 'w') as f: + parameters.to_tar(f) ``` ### Start Training @@ -264,6 +269,37 @@ trainer.train( ![png](./image/train_and_test.png) +### Apply model + +#### 1. generate testing data + +```python +test_data_creator = paddle.dataset.uci_housing.test() +test_data = [] +test_label = [] + +for item in test_data_creator(): + test_data.append((item[0],)) + test_label.append(item[1]) + if len(test_data) == 5: + break +``` + +#### 2. inference + +```python +# load parameters from tar file. +# users can remove the comments and change the model name +# with open('params_pass_20.tar', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + +probs = paddle.infer( + output_layer=y_predict, parameters=parameters, input=test_data) + +for i in xrange(len(probs)): + print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) +``` + ## Summary This chapter introduces *Linear Regression* and how to train and test this model with PaddlePaddle, using the UCI Housing Data Set. Because a large number of more complex models and techniques are derived from linear regression, it is important to understand its underlying theory and limitation. diff --git a/01.fit_a_line/train.py b/01.fit_a_line/train.py index fc4d263eb73e5370fa5288abb2f1f11a1f0a22e0..dba53181b453350efcfca80b31cb288e91076e93 100644 --- a/01.fit_a_line/train.py +++ b/01.fit_a_line/train.py @@ -31,6 +31,9 @@ def main(): event.pass_id, event.batch_id, event.cost) if isinstance(event, paddle.event.EndPass): + if event.pass_id % 10 == 0: + with open('params_pass_%d.tar' % event.pass_id, 'w') as f: + parameters.to_tar(f) result = trainer.test( reader=paddle.batch(uci_housing.test(), batch_size=2), feeding=feeding) @@ -45,6 +48,28 @@ def main(): event_handler=event_handler, num_passes=30) + # inference + test_data_creator = paddle.dataset.uci_housing.test() + test_data = [] + test_label = [] + + for item in test_data_creator(): + test_data.append((item[0], )) + test_label.append(item[1]) + if len(test_data) == 5: + break + + # load parameters from tar file. + # users can remove the comments and change the model name + # with open('params_pass_20.tar', 'r') as f: + # parameters = paddle.parameters.Parameters.from_tar(f) + + probs = paddle.infer( + output_layer=y_predict, parameters=parameters, input=test_data) + + for i in xrange(len(probs)): + print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) + if __name__ == '__main__': main()