提交 abcf34a3 编写于 作者: Q qijun

update event_handler

上级 ab35852f
...@@ -300,10 +300,10 @@ reader=paddle.batch( ...@@ -300,10 +300,10 @@ reader=paddle.batch(
batch_size=256) batch_size=256)
``` ```
`feeding` is devoted to specifying the correspondence between each yield record and `paddle.layer.data`. For instance, the first column of data generated by `movielens.train` corresponds to `user_id` feature. `reader_dict` is devoted to specifying the correspondence between each yield record and `paddle.layer.data`. For instance, the first column of data generated by `movielens.train` corresponds to `user_id` feature.
```python ```python
feeding = { reader_dict = {
'user_id': 0, 'user_id': 0,
'gender_id': 1, 'gender_id': 1,
'age_id': 2, 'age_id': 2,
...@@ -315,7 +315,15 @@ feeding = { ...@@ -315,7 +315,15 @@ feeding = {
} }
``` ```
Callback function `event_handler` will be called during training when a pre-defined event happens. Callback function `event_handler` and `event_handler_plot` will be called during training when a pre-defined event happens.
```python
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d Batch %d Cost %.2f" % (
event.pass_id, event.batch_id, event.cost)
```
```python ```python
step=0 step=0
...@@ -323,7 +331,7 @@ step=0 ...@@ -323,7 +331,7 @@ step=0
train_costs=[],[] train_costs=[],[]
test_costs=[],[] test_costs=[],[]
def event_handler(event): def event_handler_plot(event):
global step global step
global train_costs global train_costs
global test_costs global test_costs
...@@ -354,9 +362,9 @@ Finally, we can invoke `trainer.train` to start training: ...@@ -354,9 +362,9 @@ Finally, we can invoke `trainer.train` to start training:
```python ```python
trainer.train( trainer.train(
reader=reader, reader=reader,
event_handler=event_handler, event_handler=event_handler_plot,
feeding=feeding, feeding=reader_dict,
num_passes=200) num_passes=2)
``` ```
## Conclusion ## Conclusion
......
...@@ -325,17 +325,8 @@ trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, ...@@ -325,17 +325,8 @@ trainer = paddle.trainer.SGD(cost=cost, parameters=parameters,
例如,这里的reader_dict表示的是,对于数据层 `user_id`,使用了reader中每一条数据的第0个元素。`gender_id`数据层使用了第1个元素。以此类推。 例如,这里的reader_dict表示的是,对于数据层 `user_id`,使用了reader中每一条数据的第0个元素。`gender_id`数据层使用了第1个元素。以此类推。
训练过程是完全自动的。我们可以使用event_handler来观察训练过程,或进行测试等。这里我们在event_handler里面绘制了训练误差曲线和测试误差曲线。并且保存了模型。
```python ```python
%matplotlib inline reader_dict = {
import matplotlib.pyplot as plt
from IPython import display
import cPickle
feeding = {
'user_id': 0, 'user_id': 0,
'gender_id': 1, 'gender_id': 1,
'age_id': 2, 'age_id': 2,
...@@ -345,13 +336,31 @@ feeding = { ...@@ -345,13 +336,31 @@ feeding = {
'movie_title': 6, 'movie_title': 6,
'score': 7 'score': 7
} }
```
训练过程是完全自动的。我们可以使用event_handler与event_handler_plot来观察训练过程,或进行测试等。这里我们在event_handler_plot里面绘制了训练误差曲线和测试误差曲线。并且保存了模型。
```python
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d Batch %d Cost %.2f" % (
event.pass_id, event.batch_id, event.cost)
```
```python
%matplotlib inline
import matplotlib.pyplot as plt
from IPython import display
import cPickle
step=0 step=0
train_costs=[],[] train_costs=[],[]
test_costs=[],[] test_costs=[],[]
def event_handler(event): def event_handler_plot(event):
global step global step
global train_costs global train_costs
global test_costs global test_costs
...@@ -381,8 +390,8 @@ trainer.train( ...@@ -381,8 +390,8 @@ trainer.train(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.movielens.train(), buf_size=8192), paddle.dataset.movielens.train(), buf_size=8192),
batch_size=256), batch_size=256),
event_handler=event_handler, event_handler=event_handler_plot,
feeding=feeding, feeding=reader_dict,
num_passes=2) num_passes=2)
``` ```
......
...@@ -342,10 +342,10 @@ reader=paddle.batch( ...@@ -342,10 +342,10 @@ reader=paddle.batch(
batch_size=256) batch_size=256)
``` ```
`feeding` is devoted to specifying the correspondence between each yield record and `paddle.layer.data`. For instance, the first column of data generated by `movielens.train` corresponds to `user_id` feature. `reader_dict` is devoted to specifying the correspondence between each yield record and `paddle.layer.data`. For instance, the first column of data generated by `movielens.train` corresponds to `user_id` feature.
```python ```python
feeding = { reader_dict = {
'user_id': 0, 'user_id': 0,
'gender_id': 1, 'gender_id': 1,
'age_id': 2, 'age_id': 2,
...@@ -357,7 +357,15 @@ feeding = { ...@@ -357,7 +357,15 @@ feeding = {
} }
``` ```
Callback function `event_handler` will be called during training when a pre-defined event happens. Callback function `event_handler` and `event_handler_plot` will be called during training when a pre-defined event happens.
```python
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d Batch %d Cost %.2f" % (
event.pass_id, event.batch_id, event.cost)
```
```python ```python
step=0 step=0
...@@ -365,7 +373,7 @@ step=0 ...@@ -365,7 +373,7 @@ step=0
train_costs=[],[] train_costs=[],[]
test_costs=[],[] test_costs=[],[]
def event_handler(event): def event_handler_plot(event):
global step global step
global train_costs global train_costs
global test_costs global test_costs
...@@ -396,9 +404,9 @@ Finally, we can invoke `trainer.train` to start training: ...@@ -396,9 +404,9 @@ Finally, we can invoke `trainer.train` to start training:
```python ```python
trainer.train( trainer.train(
reader=reader, reader=reader,
event_handler=event_handler, event_handler=event_handler_plot,
feeding=feeding, feeding=reader_dict,
num_passes=200) num_passes=2)
``` ```
## Conclusion ## Conclusion
......
...@@ -367,17 +367,8 @@ trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, ...@@ -367,17 +367,8 @@ trainer = paddle.trainer.SGD(cost=cost, parameters=parameters,
例如,这里的reader_dict表示的是,对于数据层 `user_id`,使用了reader中每一条数据的第0个元素。`gender_id`数据层使用了第1个元素。以此类推。 例如,这里的reader_dict表示的是,对于数据层 `user_id`,使用了reader中每一条数据的第0个元素。`gender_id`数据层使用了第1个元素。以此类推。
训练过程是完全自动的。我们可以使用event_handler来观察训练过程,或进行测试等。这里我们在event_handler里面绘制了训练误差曲线和测试误差曲线。并且保存了模型。
```python ```python
%matplotlib inline reader_dict = {
import matplotlib.pyplot as plt
from IPython import display
import cPickle
feeding = {
'user_id': 0, 'user_id': 0,
'gender_id': 1, 'gender_id': 1,
'age_id': 2, 'age_id': 2,
...@@ -387,13 +378,31 @@ feeding = { ...@@ -387,13 +378,31 @@ feeding = {
'movie_title': 6, 'movie_title': 6,
'score': 7 'score': 7
} }
```
训练过程是完全自动的。我们可以使用event_handler与event_handler_plot来观察训练过程,或进行测试等。这里我们在event_handler_plot里面绘制了训练误差曲线和测试误差曲线。并且保存了模型。
```python
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d Batch %d Cost %.2f" % (
event.pass_id, event.batch_id, event.cost)
```
```python
%matplotlib inline
import matplotlib.pyplot as plt
from IPython import display
import cPickle
step=0 step=0
train_costs=[],[] train_costs=[],[]
test_costs=[],[] test_costs=[],[]
def event_handler(event): def event_handler_plot(event):
global step global step
global train_costs global train_costs
global test_costs global test_costs
...@@ -423,8 +432,8 @@ trainer.train( ...@@ -423,8 +432,8 @@ trainer.train(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.movielens.train(), buf_size=8192), paddle.dataset.movielens.train(), buf_size=8192),
batch_size=256), batch_size=256),
event_handler=event_handler, event_handler=event_handler_plot,
feeding=feeding, feeding=reader_dict,
num_passes=2) num_passes=2)
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册