diff --git a/01.fit_a_line/README.en.md b/01.fit_a_line/README.en.md index 7f42b7da50adec54a779d388cec6572177dd88f3..3ed49f0a0dad05488100abca064aa702664ab041 100644 --- a/01.fit_a_line/README.en.md +++ b/01.fit_a_line/README.en.md @@ -163,40 +163,31 @@ feeding={'x': 0, 'y': 1} Moreover, an event handler is provided to print the training progress: ```python -import matplotlib.pyplot as plt -from IPython import display -import cPickle +# event_handler to print training and testing info +from paddle.v2.plot import Ploter -step=0 +train_title = "Train cost" +test_title = "Test cost" +plot_cost = Ploter(train_title, test_title) -train_costs=[],[] -test_costs=[],[] +step = 0 def event_handler(event): global step - global train_costs - global test_costs if isinstance(event, paddle.event.EndIteration): - need_plot = False if step % 10 == 0: # every 10 batches, record a train cost - train_costs[0].append(step) - train_costs[1].append(event.cost) + plot_cost.append(train_title, step, 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( reader=paddle.batch( uci_housing.test(), batch_size=2), feeding=feeding) - test_costs[0].append(step) - test_costs[1].append(result.cost) + plot_cost.append(test_title, step, result.cost) if step % 100 == 0: # every 100 batches, update cost plot - plt.plot(*train_costs) - 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() + plot_cost.plot() + step += 1 ``` @@ -213,7 +204,7 @@ trainer.train( num_passes=30) ``` -![png](./image/train-and-test.png) +![png](./image/train_and_test.png) ## 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/README.md b/01.fit_a_line/README.md index 2c3f1fd4dd61aec8ad6e97881d6229f4e6ef91b1..0eee811ec19e8d3c7486b1abea773bc23cfe55bc 100644 --- a/01.fit_a_line/README.md +++ b/01.fit_a_line/README.md @@ -159,40 +159,30 @@ feeding={'x': 0, 'y': 1} ```python # event_handler to print training and testing info -import matplotlib.pyplot as plt -from IPython import display -import cPickle +from paddle.v2.plot import Ploter -step=0 +train_title = "Train cost" +test_title = "Test cost" +cost_ploter = Ploter(train_title, test_title) -train_costs=[],[] -test_costs=[],[] +step = 0 def event_handler(event): global step - global train_costs - global test_costs if isinstance(event, paddle.event.EndIteration): - need_plot = False if step % 10 == 0: # every 10 batches, record a train cost - train_costs[0].append(step) - train_costs[1].append(event.cost) + cost_ploter.append(train_title, step, 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( reader=paddle.batch( uci_housing.test(), batch_size=2), feeding=feeding) - test_costs[0].append(step) - test_costs[1].append(result.cost) + cost_ploter.append(test_title, step, result.cost) if step % 100 == 0: # every 100 batches, update cost plot - plt.plot(*train_costs) - 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() + cost_ploter.plot() + step += 1 ``` @@ -209,7 +199,7 @@ trainer.train( num_passes=30) ``` -![png](./image/train-and-test.png) +![png](./image/train_and_test.png) ## 总结 在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。 diff --git a/01.fit_a_line/image/ranges.png b/01.fit_a_line/image/ranges.png index 3661ace169ad2bbda6355cff615b771108b258de..2f588f15c8a57f7f86699a4bc1b9879591928270 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/image/train-and-test.png b/01.fit_a_line/image/train-and-test.png deleted file mode 100644 index bcd304a6a0baf30ecfbc43e08fc0aca179d05958..0000000000000000000000000000000000000000 Binary files a/01.fit_a_line/image/train-and-test.png and /dev/null differ diff --git a/01.fit_a_line/index.en.html b/01.fit_a_line/index.en.html index 06f5559174c252eaf951211d86ce86c7897d9ed9..82bcd531c7db05bf95462653a42ddf8537813be5 100644 --- a/01.fit_a_line/index.en.html +++ b/01.fit_a_line/index.en.html @@ -205,40 +205,31 @@ feeding={'x': 0, 'y': 1} Moreover, an event handler is provided to print the training progress: ```python -import matplotlib.pyplot as plt -from IPython import display -import cPickle +# event_handler to print training and testing info +from paddle.v2.plot import Ploter -step=0 +train_title = "Train cost" +test_title = "Test cost" +plot_cost = Ploter(train_title, test_title) -train_costs=[],[] -test_costs=[],[] +step = 0 def event_handler(event): global step - global train_costs - global test_costs if isinstance(event, paddle.event.EndIteration): - need_plot = False if step % 10 == 0: # every 10 batches, record a train cost - train_costs[0].append(step) - train_costs[1].append(event.cost) + plot_cost.append(train_title, step, 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( reader=paddle.batch( uci_housing.test(), batch_size=2), feeding=feeding) - test_costs[0].append(step) - test_costs[1].append(result.cost) + plot_cost.append(test_title, step, result.cost) if step % 100 == 0: # every 100 batches, update cost plot - plt.plot(*train_costs) - 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() + plot_cost.plot() + step += 1 ``` @@ -255,7 +246,7 @@ trainer.train( num_passes=30) ``` -![png](./image/train-and-test.png) +![png](./image/train_and_test.png) ## 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/index.html b/01.fit_a_line/index.html index a866e13bf3321c22973198d8ae639327348c4587..b7534329942e3d21d9a1eb2d9b120446a806f6a0 100644 --- a/01.fit_a_line/index.html +++ b/01.fit_a_line/index.html @@ -201,40 +201,30 @@ feeding={'x': 0, 'y': 1} ```python # event_handler to print training and testing info -import matplotlib.pyplot as plt -from IPython import display -import cPickle +from paddle.v2.plot import Ploter -step=0 +train_title = "Train cost" +test_title = "Test cost" +cost_ploter = Ploter(train_title, test_title) -train_costs=[],[] -test_costs=[],[] +step = 0 def event_handler(event): global step - global train_costs - global test_costs if isinstance(event, paddle.event.EndIteration): - need_plot = False if step % 10 == 0: # every 10 batches, record a train cost - train_costs[0].append(step) - train_costs[1].append(event.cost) + cost_ploter.append(train_title, step, 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( reader=paddle.batch( uci_housing.test(), batch_size=2), feeding=feeding) - test_costs[0].append(step) - test_costs[1].append(result.cost) + cost_ploter.append(test_title, step, result.cost) if step % 100 == 0: # every 100 batches, update cost plot - plt.plot(*train_costs) - 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() + cost_ploter.plot() + step += 1 ``` @@ -251,7 +241,7 @@ trainer.train( num_passes=30) ``` -![png](./image/train-and-test.png) +![png](./image/train_and_test.png) ## 总结 在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。 diff --git a/02.recognize_digits/README.en.md b/02.recognize_digits/README.en.md index 15a70e75af4caa39f32d7b4db4233fc080a17eed..c5efb71f10bcaede1335e3acd955a2ae806e75b2 100644 --- a/02.recognize_digits/README.en.md +++ b/02.recognize_digits/README.en.md @@ -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. +`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 lists = [] @@ -246,13 +274,15 @@ def event_handler(event): event.pass_id, result.cost, result.metrics) lists.append((event.pass_id, result.cost, result.metrics['classification_error_evaluator'])) +``` +```python trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), - event_handler=event_handler, + event_handler=event_handler_plot, num_passes=100) ``` diff --git a/02.recognize_digits/README.md b/02.recognize_digits/README.md index 85f0defa1989b3832a50564e46274ae7270bed60..97ba662923ebac27a06621816f22d09bfc8bdbdd 100644 --- a/02.recognize_digits/README.md +++ b/02.recognize_digits/README.md @@ -236,6 +236,34 @@ trainer = paddle.trainer.SGD(cost=cost, `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 lists = [] @@ -251,13 +279,15 @@ def event_handler(event): event.pass_id, result.cost, result.metrics) lists.append((event.pass_id, result.cost, result.metrics['classification_error_evaluator'])) +``` +```python trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), - event_handler=event_handler, + event_handler=event_handler_plot, num_passes=100) ``` diff --git a/02.recognize_digits/image/train_and_test.png b/02.recognize_digits/image/train_and_test.png new file mode 100644 index 0000000000000000000000000000000000000000..5cb87b450d0398bcfaec0e647c362052069797e7 Binary files /dev/null and b/02.recognize_digits/image/train_and_test.png differ diff --git a/02.recognize_digits/index.en.html b/02.recognize_digits/index.en.html index 03960fd4b38172a6b821c9b612a7d7c2034bd8a2..45c85c8290baebb379c4f87a594230ee07836c2f 100644 --- a/02.recognize_digits/index.en.html +++ b/02.recognize_digits/index.en.html @@ -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. +`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 lists = [] @@ -288,13 +316,15 @@ def event_handler(event): event.pass_id, result.cost, result.metrics) lists.append((event.pass_id, result.cost, result.metrics['classification_error_evaluator'])) +``` +```python trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), - event_handler=event_handler, + event_handler=event_handler_plot, num_passes=100) ``` diff --git a/02.recognize_digits/index.html b/02.recognize_digits/index.html index 3b45d841952289b2fcd11ae149538596a5dd6a1f..38500236c3d6a4e27f3fbc8d859cb780625f7bad 100644 --- a/02.recognize_digits/index.html +++ b/02.recognize_digits/index.html @@ -278,6 +278,34 @@ trainer = paddle.trainer.SGD(cost=cost, `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 lists = [] @@ -293,13 +321,15 @@ def event_handler(event): event.pass_id, result.cost, result.metrics) lists.append((event.pass_id, result.cost, result.metrics['classification_error_evaluator'])) +``` +```python trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), - event_handler=event_handler, + event_handler=event_handler_plot, num_passes=100) ```