提交 c9023e06 编写于 作者: Q qiaolongfei

update 01 and 02 markdown

上级 4374dabe
...@@ -163,40 +163,31 @@ feeding={'x': 0, 'y': 1} ...@@ -163,40 +163,31 @@ feeding={'x': 0, 'y': 1}
Moreover, an event handler is provided to print the training progress: Moreover, an event handler is provided to print the training progress:
```python ```python
import matplotlib.pyplot as plt # event_handler to print training and testing info
from IPython import display from paddle.v2.plot import Ploter
import cPickle
step=0 train_title = "Train cost"
test_title = "Test cost"
plot_cost = Ploter(train_title, test_title)
train_costs=[],[] step = 0
test_costs=[],[]
def event_handler(event): def event_handler(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) plot_cost.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 100 == 0: # every 100 batches, record a test cost
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
uci_housing.test(), batch_size=2), uci_housing.test(), batch_size=2),
feeding=feeding) feeding=feeding)
test_costs[0].append(step) plot_cost.append(test_title, step, result.cost)
test_costs[1].append(result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) plot_cost.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
``` ```
...@@ -213,7 +204,7 @@ trainer.train( ...@@ -213,7 +204,7 @@ trainer.train(
num_passes=30) num_passes=30)
``` ```
![png](./image/train-and-test.png) ![png](./image/train_and_test.png)
## Summary ## 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. 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.
......
...@@ -159,40 +159,30 @@ feeding={'x': 0, 'y': 1} ...@@ -159,40 +159,30 @@ feeding={'x': 0, 'y': 1}
```python ```python
# event_handler to print training and testing info # event_handler to print training and testing info
import matplotlib.pyplot as plt from paddle.v2.plot import Ploter
from IPython import display
import cPickle
step=0 train_title = "Train cost"
test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
train_costs=[],[] step = 0
test_costs=[],[]
def event_handler(event): def event_handler(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) cost_ploter.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 100 == 0: # every 100 batches, record a test cost
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
uci_housing.test(), batch_size=2), uci_housing.test(), batch_size=2),
feeding=feeding) feeding=feeding)
test_costs[0].append(step) cost_ploter.append(test_title, step, result.cost)
test_costs[1].append(result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) cost_ploter.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
``` ```
...@@ -209,7 +199,7 @@ trainer.train( ...@@ -209,7 +199,7 @@ trainer.train(
num_passes=30) num_passes=30)
``` ```
![png](./image/train-and-test.png) ![png](./image/train_and_test.png)
## 总结 ## 总结
在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。 在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。
......
01.fit_a_line/image/ranges.png

27.2 KB | W: | H:

01.fit_a_line/image/ranges.png

6.5 KB | W: | H:

01.fit_a_line/image/ranges.png
01.fit_a_line/image/ranges.png
01.fit_a_line/image/ranges.png
01.fit_a_line/image/ranges.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -205,40 +205,31 @@ feeding={'x': 0, 'y': 1} ...@@ -205,40 +205,31 @@ feeding={'x': 0, 'y': 1}
Moreover, an event handler is provided to print the training progress: Moreover, an event handler is provided to print the training progress:
```python ```python
import matplotlib.pyplot as plt # event_handler to print training and testing info
from IPython import display from paddle.v2.plot import Ploter
import cPickle
step=0 train_title = "Train cost"
test_title = "Test cost"
plot_cost = Ploter(train_title, test_title)
train_costs=[],[] step = 0
test_costs=[],[]
def event_handler(event): def event_handler(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) plot_cost.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 100 == 0: # every 100 batches, record a test cost
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
uci_housing.test(), batch_size=2), uci_housing.test(), batch_size=2),
feeding=feeding) feeding=feeding)
test_costs[0].append(step) plot_cost.append(test_title, step, result.cost)
test_costs[1].append(result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) plot_cost.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
``` ```
...@@ -255,7 +246,7 @@ trainer.train( ...@@ -255,7 +246,7 @@ trainer.train(
num_passes=30) num_passes=30)
``` ```
![png](./image/train-and-test.png) ![png](./image/train_and_test.png)
## Summary ## 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. 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.
......
...@@ -201,40 +201,30 @@ feeding={'x': 0, 'y': 1} ...@@ -201,40 +201,30 @@ feeding={'x': 0, 'y': 1}
```python ```python
# event_handler to print training and testing info # event_handler to print training and testing info
import matplotlib.pyplot as plt from paddle.v2.plot import Ploter
from IPython import display
import cPickle
step=0 train_title = "Train cost"
test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
train_costs=[],[] step = 0
test_costs=[],[]
def event_handler(event): def event_handler(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) cost_ploter.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 100 == 0: # every 100 batches, record a test cost
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
uci_housing.test(), batch_size=2), uci_housing.test(), batch_size=2),
feeding=feeding) feeding=feeding)
test_costs[0].append(step) cost_ploter.append(test_title, step, result.cost)
test_costs[1].append(result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) cost_ploter.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
``` ```
...@@ -251,7 +241,7 @@ trainer.train( ...@@ -251,7 +241,7 @@ trainer.train(
num_passes=30) num_passes=30)
``` ```
![png](./image/train-and-test.png) ![png](./image/train_and_test.png)
## 总结 ## 总结
在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。 在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。
......
...@@ -231,6 +231,34 @@ Here `shuffle` is a reader decorator, which takes a reader A as its parameter, a ...@@ -231,6 +231,34 @@ Here `shuffle` is a reader decorator, which takes a reader A as its parameter, a
`batch` is a special decorator, whose input is a reader and output is a *batch reader*, which doesn't yield an instance at a time, but a minibatch. `batch` is a special decorator, whose input is a reader and output is a *batch reader*, which doesn't yield an instance at a time, but a minibatch.
`event_handler_plot` is used to plot a figure like below:
![png](./image/train_and_test.png)
```python
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
step = 0
# event_handler to plot a figure
def event_handler_plot(event):
global step
if isinstance(event, paddle.event.EndIteration):
if step % 100 == 0:
cost_ploter.append(train_title, step, event.cost)
cost_ploter.plot()
step += 1
if isinstance(event, paddle.event.EndPass):
result = trainer.test(reader=paddle.batch(
paddle.dataset.mnist.test(), batch_size=128))
cost_ploter.append(test_title, step, result.cost)
```
`event_handler` is used to plot some text data when training.
```python ```python
lists = [] lists = []
...@@ -246,13 +274,15 @@ def event_handler(event): ...@@ -246,13 +274,15 @@ def event_handler(event):
event.pass_id, result.cost, result.metrics) event.pass_id, result.cost, result.metrics)
lists.append((event.pass_id, result.cost, lists.append((event.pass_id, result.cost,
result.metrics['classification_error_evaluator'])) result.metrics['classification_error_evaluator']))
```
```python
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192), paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128), batch_size=128),
event_handler=event_handler, event_handler=event_handler_plot,
num_passes=100) num_passes=100)
``` ```
......
...@@ -236,6 +236,34 @@ trainer = paddle.trainer.SGD(cost=cost, ...@@ -236,6 +236,34 @@ trainer = paddle.trainer.SGD(cost=cost,
`batch`是一个特殊的decorator,它的输入是一个reader,输出是一个batched reader —— 在PaddlePaddle里,一个reader每次yield一条训练数据,而一个batched reader每次yield一个minbatch。 `batch`是一个特殊的decorator,它的输入是一个reader,输出是一个batched reader —— 在PaddlePaddle里,一个reader每次yield一条训练数据,而一个batched reader每次yield一个minbatch。
`event_handler_plot`可以用来在训练过程中画图如下:
![png](./image/train_and_test.png)
```python
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
step = 0
# event_handler to plot a figure
def event_handler_plot(event):
global step
if isinstance(event, paddle.event.EndIteration):
if step % 100 == 0:
cost_ploter.append(train_title, step, event.cost)
cost_ploter.plot()
step += 1
if isinstance(event, paddle.event.EndPass):
result = trainer.test(reader=paddle.batch(
paddle.dataset.mnist.test(), batch_size=128))
cost_ploter.append(test_title, step, result.cost)
```
`event_handler` 用来在训练过程中输出训练结果
```python ```python
lists = [] lists = []
...@@ -251,13 +279,15 @@ def event_handler(event): ...@@ -251,13 +279,15 @@ def event_handler(event):
event.pass_id, result.cost, result.metrics) event.pass_id, result.cost, result.metrics)
lists.append((event.pass_id, result.cost, lists.append((event.pass_id, result.cost,
result.metrics['classification_error_evaluator'])) result.metrics['classification_error_evaluator']))
```
```python
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192), paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128), batch_size=128),
event_handler=event_handler, event_handler=event_handler_plot,
num_passes=100) num_passes=100)
``` ```
......
...@@ -273,6 +273,34 @@ Here `shuffle` is a reader decorator, which takes a reader A as its parameter, a ...@@ -273,6 +273,34 @@ Here `shuffle` is a reader decorator, which takes a reader A as its parameter, a
`batch` is a special decorator, whose input is a reader and output is a *batch reader*, which doesn't yield an instance at a time, but a minibatch. `batch` is a special decorator, whose input is a reader and output is a *batch reader*, which doesn't yield an instance at a time, but a minibatch.
`event_handler_plot` is used to plot a figure like below:
![png](./image/train_and_test.png)
```python
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
step = 0
# event_handler to plot a figure
def event_handler_plot(event):
global step
if isinstance(event, paddle.event.EndIteration):
if step % 100 == 0:
cost_ploter.append(train_title, step, event.cost)
cost_ploter.plot()
step += 1
if isinstance(event, paddle.event.EndPass):
result = trainer.test(reader=paddle.batch(
paddle.dataset.mnist.test(), batch_size=128))
cost_ploter.append(test_title, step, result.cost)
```
`event_handler` is used to plot some text data when training.
```python ```python
lists = [] lists = []
...@@ -288,13 +316,15 @@ def event_handler(event): ...@@ -288,13 +316,15 @@ def event_handler(event):
event.pass_id, result.cost, result.metrics) event.pass_id, result.cost, result.metrics)
lists.append((event.pass_id, result.cost, lists.append((event.pass_id, result.cost,
result.metrics['classification_error_evaluator'])) result.metrics['classification_error_evaluator']))
```
```python
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192), paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128), batch_size=128),
event_handler=event_handler, event_handler=event_handler_plot,
num_passes=100) num_passes=100)
``` ```
......
...@@ -278,6 +278,34 @@ trainer = paddle.trainer.SGD(cost=cost, ...@@ -278,6 +278,34 @@ trainer = paddle.trainer.SGD(cost=cost,
`batch`是一个特殊的decorator,它的输入是一个reader,输出是一个batched reader —— 在PaddlePaddle里,一个reader每次yield一条训练数据,而一个batched reader每次yield一个minbatch。 `batch`是一个特殊的decorator,它的输入是一个reader,输出是一个batched reader —— 在PaddlePaddle里,一个reader每次yield一条训练数据,而一个batched reader每次yield一个minbatch。
`event_handler_plot`可以用来在训练过程中画图如下:
![png](./image/train_and_test.png)
```python
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
step = 0
# event_handler to plot a figure
def event_handler_plot(event):
global step
if isinstance(event, paddle.event.EndIteration):
if step % 100 == 0:
cost_ploter.append(train_title, step, event.cost)
cost_ploter.plot()
step += 1
if isinstance(event, paddle.event.EndPass):
result = trainer.test(reader=paddle.batch(
paddle.dataset.mnist.test(), batch_size=128))
cost_ploter.append(test_title, step, result.cost)
```
`event_handler` 用来在训练过程中输出训练结果
```python ```python
lists = [] lists = []
...@@ -293,13 +321,15 @@ def event_handler(event): ...@@ -293,13 +321,15 @@ def event_handler(event):
event.pass_id, result.cost, result.metrics) event.pass_id, result.cost, result.metrics)
lists.append((event.pass_id, result.cost, lists.append((event.pass_id, result.cost,
result.metrics['classification_error_evaluator'])) result.metrics['classification_error_evaluator']))
```
```python
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192), paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128), batch_size=128),
event_handler=event_handler, event_handler=event_handler_plot,
num_passes=100) num_passes=100)
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册