Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 01.fit_a_line/README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

### 开始训练
Expand All @@ -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实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。

Expand Down
36 changes: 36 additions & 0 deletions 01.fit_a_line/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down
Binary file modified 01.fit_a_line/image/ranges.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions 01.fit_a_line/index.cn.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@
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)
```

### 开始训练
Expand All @@ -259,6 +264,37 @@

![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实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。

Expand Down
36 changes: 36 additions & 0 deletions 01.fit_a_line/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@
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
Expand All @@ -264,6 +269,37 @@

![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.

Expand Down
25 changes: 25 additions & 0 deletions 01.fit_a_line/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def event_handler(event):
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)
Expand All @@ -45,6 +48,28 @@ def event_handler(event):
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()