`feeding` is devoted to specify 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.
fromIPythonimportdisplay
importcPickle
```python
feeding={
feeding={
'user_id':0,
'user_id':0,
'gender_id':1,
'gender_id':1,
...
@@ -312,12 +310,11 @@ feeding = {
...
@@ -312,12 +310,11 @@ feeding = {
'movie_title':6,
'movie_title':6,
'score':7
'score':7
}
}
```
step=0
Callback function `event_handler` is used to track training and testing process that might be triggered once the action to which it is attached is executed.
train_costs=[],[]
test_costs=[],[]
```python
defevent_handler(event):
defevent_handler(event):
globalstep
globalstep
globaltrain_costs
globaltrain_costs
...
@@ -327,13 +324,13 @@ def event_handler(event):
...
@@ -327,13 +324,13 @@ def event_handler(event):
ifstep%10==0:# every 10 batches, record a train cost
ifstep%10==0:# every 10 batches, record a train cost
train_costs[0].append(step)
train_costs[0].append(step)
train_costs[1].append(event.cost)
train_costs[1].append(event.cost)
ifstep%1000==0:# every 1000 batches, record a test cost
ifstep%1000==0:# every 1000 batches, record a test cost
result=trainer.test(reader=paddle.batch(
result=trainer.test(reader=paddle.batch(
paddle.dataset.movielens.test(),batch_size=256))
paddle.dataset.movielens.test(),batch_size=256))
test_costs[0].append(step)
test_costs[0].append(step)
test_costs[1].append(result.cost)
test_costs[1].append(result.cost)
ifstep%100==0:# every 100 batches, update cost plot
ifstep%100==0:# every 100 batches, update cost plot
plt.plot(*train_costs)
plt.plot(*train_costs)
plt.plot(*test_costs)
plt.plot(*test_costs)
...
@@ -342,15 +339,27 @@ def event_handler(event):
...
@@ -342,15 +339,27 @@ def event_handler(event):
display.display(plt.gcf())
display.display(plt.gcf())
plt.gcf().clear()
plt.gcf().clear()
step+=1
step+=1
```
Finally, we can invoke `trainer.train` to start training: