Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 21b503b

Browse files
authored
Merge pull request #349 from jacquesqiao/01-infer
add inference to 01
2 parents 848efd5 + 9f9559a commit 21b503b

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-0
lines changed

01.fit_a_line/README.cn.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ def event_handler_plot(event):
200200
cost_ploter.plot()
201201

202202
step += 1
203+
204+
if isinstance(event, paddle.event.EndPass):
205+
if event.pass_id % 10 == 0:
206+
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
207+
parameters.to_tar(f)
203208
```
204209

205210
### 开始训练
@@ -217,6 +222,37 @@ trainer.train(
217222

218223
![png](./image/train_and_test.png)
219224

225+
### 应用模型
226+
227+
#### 1. 生成测试数据
228+
229+
```python
230+
test_data_creator = paddle.dataset.uci_housing.test()
231+
test_data = []
232+
test_label = []
233+
234+
for item in test_data_creator():
235+
test_data.append((item[0],))
236+
test_label.append(item[1])
237+
if len(test_data) == 5:
238+
break
239+
```
240+
241+
#### 2. 推测 inference
242+
243+
```python
244+
# load parameters from tar file.
245+
# users can remove the comments and change the model name
246+
# with open('params_pass_20.tar', 'r') as f:
247+
# parameters = paddle.parameters.Parameters.from_tar(f)
248+
249+
probs = paddle.infer(
250+
output_layer=y_predict, parameters=parameters, input=test_data)
251+
252+
for i in xrange(len(probs)):
253+
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
254+
```
255+
220256
## 总结
221257
在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。
222258

01.fit_a_line/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ def event_handler_plot(event):
205205
plot_cost.plot()
206206

207207
step += 1
208+
209+
if isinstance(event, paddle.event.EndPass):
210+
if event.pass_id % 10 == 0:
211+
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
212+
parameters.to_tar(f)
208213
```
209214

210215
### Start Training
@@ -222,6 +227,37 @@ trainer.train(
222227

223228
![png](./image/train_and_test.png)
224229

230+
### Apply model
231+
232+
#### 1. generate testing data
233+
234+
```python
235+
test_data_creator = paddle.dataset.uci_housing.test()
236+
test_data = []
237+
test_label = []
238+
239+
for item in test_data_creator():
240+
test_data.append((item[0],))
241+
test_label.append(item[1])
242+
if len(test_data) == 5:
243+
break
244+
```
245+
246+
#### 2. inference
247+
248+
```python
249+
# load parameters from tar file.
250+
# users can remove the comments and change the model name
251+
# with open('params_pass_20.tar', 'r') as f:
252+
# parameters = paddle.parameters.Parameters.from_tar(f)
253+
254+
probs = paddle.infer(
255+
output_layer=y_predict, parameters=parameters, input=test_data)
256+
257+
for i in xrange(len(probs)):
258+
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
259+
```
260+
225261
## Summary
226262
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.
227263

01.fit_a_line/image/ranges.png

-20.7 KB
Loading

01.fit_a_line/index.cn.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@
242242
cost_ploter.plot()
243243

244244
step += 1
245+
246+
if isinstance(event, paddle.event.EndPass):
247+
if event.pass_id % 10 == 0:
248+
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
249+
parameters.to_tar(f)
245250
```
246251

247252
### 开始训练
@@ -259,6 +264,37 @@
259264

260265
![png](./image/train_and_test.png)
261266

267+
### 应用模型
268+
269+
#### 1. 生成测试数据
270+
271+
```python
272+
test_data_creator = paddle.dataset.uci_housing.test()
273+
test_data = []
274+
test_label = []
275+
276+
for item in test_data_creator():
277+
test_data.append((item[0],))
278+
test_label.append(item[1])
279+
if len(test_data) == 5:
280+
break
281+
```
282+
283+
#### 2. 推测 inference
284+
285+
```python
286+
# load parameters from tar file.
287+
# users can remove the comments and change the model name
288+
# with open('params_pass_20.tar', 'r') as f:
289+
# parameters = paddle.parameters.Parameters.from_tar(f)
290+
291+
probs = paddle.infer(
292+
output_layer=y_predict, parameters=parameters, input=test_data)
293+
294+
for i in xrange(len(probs)):
295+
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
296+
```
297+
262298
## 总结
263299
在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。
264300

01.fit_a_line/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@
247247
plot_cost.plot()
248248

249249
step += 1
250+
251+
if isinstance(event, paddle.event.EndPass):
252+
if event.pass_id % 10 == 0:
253+
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
254+
parameters.to_tar(f)
250255
```
251256

252257
### Start Training
@@ -264,6 +269,37 @@
264269

265270
![png](./image/train_and_test.png)
266271

272+
### Apply model
273+
274+
#### 1. generate testing data
275+
276+
```python
277+
test_data_creator = paddle.dataset.uci_housing.test()
278+
test_data = []
279+
test_label = []
280+
281+
for item in test_data_creator():
282+
test_data.append((item[0],))
283+
test_label.append(item[1])
284+
if len(test_data) == 5:
285+
break
286+
```
287+
288+
#### 2. inference
289+
290+
```python
291+
# load parameters from tar file.
292+
# users can remove the comments and change the model name
293+
# with open('params_pass_20.tar', 'r') as f:
294+
# parameters = paddle.parameters.Parameters.from_tar(f)
295+
296+
probs = paddle.infer(
297+
output_layer=y_predict, parameters=parameters, input=test_data)
298+
299+
for i in xrange(len(probs)):
300+
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
301+
```
302+
267303
## Summary
268304
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.
269305

01.fit_a_line/train.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def event_handler(event):
3131
event.pass_id, event.batch_id, event.cost)
3232

3333
if isinstance(event, paddle.event.EndPass):
34+
if event.pass_id % 10 == 0:
35+
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
36+
parameters.to_tar(f)
3437
result = trainer.test(
3538
reader=paddle.batch(uci_housing.test(), batch_size=2),
3639
feeding=feeding)
@@ -45,6 +48,28 @@ def event_handler(event):
4548
event_handler=event_handler,
4649
num_passes=30)
4750

51+
# inference
52+
test_data_creator = paddle.dataset.uci_housing.test()
53+
test_data = []
54+
test_label = []
55+
56+
for item in test_data_creator():
57+
test_data.append((item[0], ))
58+
test_label.append(item[1])
59+
if len(test_data) == 5:
60+
break
61+
62+
# load parameters from tar file.
63+
# users can remove the comments and change the model name
64+
# with open('params_pass_20.tar', 'r') as f:
65+
# parameters = paddle.parameters.Parameters.from_tar(f)
66+
67+
probs = paddle.infer(
68+
output_layer=y_predict, parameters=parameters, input=test_data)
69+
70+
for i in xrange(len(probs)):
71+
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
72+
4873

4974
if __name__ == '__main__':
5075
main()

0 commit comments

Comments
 (0)