diff --git a/01.fit_a_line/README.cn.md b/01.fit_a_line/README.cn.md
index 428522cb51df8e50dd49f5c0a3baabd7182ab720..8d1a8c761e090f8c07a440b45ebe01301064ce69 100644
--- a/01.fit_a_line/README.cn.md
+++ b/01.fit_a_line/README.cn.md
@@ -196,28 +196,24 @@ feed_order=['x', 'y']
# Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model"
-# Plot data
-from paddle.utils import Ploter
train_title = "Train cost"
test_title = "Test cost"
-plot_cost = Ploter(train_title, test_title)
step = 0
# event_handler prints training and testing info
-def event_handler_plot(event):
+def event_handler(event):
global step
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
- plot_cost.append(train_title, step, event.metrics[0])
+ print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))
if step % 100 == 0: # record a test cost every 100 batches
test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order)
- plot_cost.append(test_title, step, test_metrics[0])
- plot_cost.plot()
+ print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
if test_metrics[0] < 10.0:
# If the accuracy is good enough, we can stop the training.
@@ -242,7 +238,7 @@ def event_handler_plot(event):
trainer.train(
reader=train_reader,
num_epochs=100,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
feed_order=feed_order)
```
diff --git a/01.fit_a_line/README.md b/01.fit_a_line/README.md
index e6a1c847a775e005d7d16f179c67961304f1e8ff..13eef12b2b58316a87a407d4f1b6d3474f255c02 100644
--- a/01.fit_a_line/README.md
+++ b/01.fit_a_line/README.md
@@ -212,28 +212,24 @@ Moreover, an event handler is provided to print the training progress:
# Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model"
-# Plot data
-from paddle.utils import Ploter
train_title = "Train cost"
test_title = "Test cost"
-plot_cost = Ploter(train_title, test_title)
step = 0
# event_handler prints training and testing info
-def event_handler_plot(event):
+def event_handler(event):
global step
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
- plot_cost.append(train_title, step, event.metrics[0])
+ print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))
+
if step % 100 == 0: # record a test cost every 100 batches
test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order)
- plot_cost.append(test_title, step, test_metrics[0])
- plot_cost.plot()
- if test_metrics[0] < 10.0:
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
+ if test_metrics[0] < 10.0:
# If the accuracy is good enough, we can stop the training.
print('loss is less than 10.0, stop')
trainer.stop()
@@ -258,7 +254,7 @@ We now can start training by calling `trainer.train()`.
trainer.train(
reader=train_reader,
num_epochs=100,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
feed_order=feed_order)
```
diff --git a/01.fit_a_line/index.cn.html b/01.fit_a_line/index.cn.html
index 138021ad415eff07a2f3b6a440a31a35e1d0ad5e..e549995fe229b8e8850c85fd227cf9a91b0db224 100644
--- a/01.fit_a_line/index.cn.html
+++ b/01.fit_a_line/index.cn.html
@@ -238,28 +238,24 @@ feed_order=['x', 'y']
# Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model"
-# Plot data
-from paddle.utils import Ploter
train_title = "Train cost"
test_title = "Test cost"
-plot_cost = Ploter(train_title, test_title)
step = 0
# event_handler prints training and testing info
-def event_handler_plot(event):
+def event_handler(event):
global step
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
- plot_cost.append(train_title, step, event.metrics[0])
+ print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))
if step % 100 == 0: # record a test cost every 100 batches
test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order)
- plot_cost.append(test_title, step, test_metrics[0])
- plot_cost.plot()
+ print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
if test_metrics[0] < 10.0:
# If the accuracy is good enough, we can stop the training.
@@ -284,7 +280,7 @@ def event_handler_plot(event):
trainer.train(
reader=train_reader,
num_epochs=100,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
feed_order=feed_order)
```
diff --git a/01.fit_a_line/index.html b/01.fit_a_line/index.html
index 7593fe93d1a8b8a2d831f2c46182e37ec502625a..744cbba8ff5e11ec33dd88013987587a96979394 100644
--- a/01.fit_a_line/index.html
+++ b/01.fit_a_line/index.html
@@ -254,28 +254,24 @@ Moreover, an event handler is provided to print the training progress:
# Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model"
-# Plot data
-from paddle.utils import Ploter
train_title = "Train cost"
test_title = "Test cost"
-plot_cost = Ploter(train_title, test_title)
step = 0
# event_handler prints training and testing info
-def event_handler_plot(event):
+def event_handler(event):
global step
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
- plot_cost.append(train_title, step, event.metrics[0])
+ print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))
+
if step % 100 == 0: # record a test cost every 100 batches
test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order)
- plot_cost.append(test_title, step, test_metrics[0])
- plot_cost.plot()
- if test_metrics[0] < 10.0:
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
+ if test_metrics[0] < 10.0:
# If the accuracy is good enough, we can stop the training.
print('loss is less than 10.0, stop')
trainer.stop()
@@ -300,7 +296,7 @@ We now can start training by calling `trainer.train()`.
trainer.train(
reader=train_reader,
num_epochs=100,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
feed_order=feed_order)
```
diff --git a/01.fit_a_line/train.py b/01.fit_a_line/train.py
index 5f99b0f7bd455b9dd5b719392ed03d4478c3a934..6077a16b2b663d020035597d1d3b0276238d9e30 100644
--- a/01.fit_a_line/train.py
+++ b/01.fit_a_line/train.py
@@ -69,26 +69,23 @@ feed_order = ['x', 'y']
# Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model"
-from paddle.utils import Ploter
-
train_title = "Train cost"
test_title = "Test cost"
-plot_cost = Ploter(train_title, test_title)
step = 0
# event_handler prints training and testing info
-def event_handler_plot(event):
+def event_handler(event):
global step
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
- plot_cost.append(train_title, step, event.metrics[0])
+ print("%s, Step %d, Cost %f" %
+ (train_title, step, event.metrics[0]))
if step % 100 == 0: # record a test cost every 100 batches
test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order)
- plot_cost.append(test_title, step, test_metrics[0])
- plot_cost.plot()
+ print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
if test_metrics[0] < 10.0:
# If the accuracy is good enough, we can stop the training.
print('loss is less than 10.0, stop')
@@ -106,7 +103,7 @@ def event_handler_plot(event):
trainer.train(
reader=train_reader,
num_epochs=100,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
feed_order=feed_order)
diff --git a/02.recognize_digits/README.cn.md b/02.recognize_digits/README.cn.md
index c21a3fa5386b478a778212844ca607938afafd11..144ecbbd38b03a2389ab68bb6707e7ea8cc64b8b 100644
--- a/02.recognize_digits/README.cn.md
+++ b/02.recognize_digits/README.cn.md
@@ -321,42 +321,6 @@ def event_handler(event):
lists.append((event.epoch, avg_cost, acc))
```
-`event_handler_plot` 可以用来在训练过程中画图如下:
-
-
-
-图7 训练结果
-
-
-
-```python
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-step = 0
-lists = []
-
-# event_handler to plot a figure
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 100 == 0:
- # event.metrics maps with train program return arguments.
- # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- # save parameters
- trainer.save_params(params_dirname)
-
- avg_cost, acc = trainer.test(
- reader=test_reader, feed_order=['img', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
- lists.append((event.epoch, avg_cost, acc))
-```
#### 开始训练
diff --git a/02.recognize_digits/README.md b/02.recognize_digits/README.md
index b32ba920cfde25ee349f78f3dc14d59240f624a9..44230810dd6ec199c1d937e48f096e59fb3dba52 100644
--- a/02.recognize_digits/README.md
+++ b/02.recognize_digits/README.md
@@ -324,38 +324,6 @@ def event_handler(event):
lists.append((event.epoch, avg_cost, acc))
```
-`event_handler_plot` is used to plot a figure like below:
-
-![png](./image/train_and_test.png)
-
-```python
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-step = 0
-lists = []
-
-# event_handler to plot a figure
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 100 == 0:
- # event.metrics maps with train program return arguments.
- # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- # save parameters
- trainer.save_params(params_dirname)
-
- avg_cost, acc = trainer.test(
- reader=test_reader, feed_order=['img', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
- lists.append((event.epoch, avg_cost, acc))
-```
#### Start training
@@ -365,7 +333,7 @@ Now that we setup the event_handler and the reader, we can start training the mo
# Train the model now
trainer.train(
num_epochs=5,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
reader=train_reader,
feed_order=['img', 'label'])
```
diff --git a/02.recognize_digits/index.cn.html b/02.recognize_digits/index.cn.html
index 55017f73573a1e236173377b14f03d4d7b567238..3b7df167753da39db84c0dfcb97a604ebe74dffa 100644
--- a/02.recognize_digits/index.cn.html
+++ b/02.recognize_digits/index.cn.html
@@ -363,42 +363,6 @@ def event_handler(event):
lists.append((event.epoch, avg_cost, acc))
```
-`event_handler_plot` 可以用来在训练过程中画图如下:
-
-
-
-图7 训练结果
-
-
-
-```python
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-step = 0
-lists = []
-
-# event_handler to plot a figure
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 100 == 0:
- # event.metrics maps with train program return arguments.
- # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- # save parameters
- trainer.save_params(params_dirname)
-
- avg_cost, acc = trainer.test(
- reader=test_reader, feed_order=['img', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
- lists.append((event.epoch, avg_cost, acc))
-```
#### 开始训练
diff --git a/02.recognize_digits/index.html b/02.recognize_digits/index.html
index 21a55a285e90520d3ac8593854f36a25c2d5fd46..8121a46f8b26f358b9f2fe898bc3603b1ae6d90f 100644
--- a/02.recognize_digits/index.html
+++ b/02.recognize_digits/index.html
@@ -366,38 +366,6 @@ def event_handler(event):
lists.append((event.epoch, avg_cost, acc))
```
-`event_handler_plot` is used to plot a figure like below:
-
-![png](./image/train_and_test.png)
-
-```python
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-step = 0
-lists = []
-
-# event_handler to plot a figure
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 100 == 0:
- # event.metrics maps with train program return arguments.
- # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- # save parameters
- trainer.save_params(params_dirname)
-
- avg_cost, acc = trainer.test(
- reader=test_reader, feed_order=['img', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
- lists.append((event.epoch, avg_cost, acc))
-```
#### Start training
@@ -407,7 +375,7 @@ Now that we setup the event_handler and the reader, we can start training the mo
# Train the model now
trainer.train(
num_epochs=5,
- event_handler=event_handler_plot,
+ event_handler=event_handler,
reader=train_reader,
feed_order=['img', 'label'])
```
diff --git a/03.image_classification/README.cn.md b/03.image_classification/README.cn.md
index 3652ed6e6241e3725ee0731c23fc4f62cc56d586..761eed3c4347ec1eda0df1688ab7f0c45e31abba 100644
--- a/03.image_classification/README.cn.md
+++ b/03.image_classification/README.cn.md
@@ -383,42 +383,6 @@ test_reader = paddle.batch(
可以使用`event_handler`回调函数来观察训练过程,或进行测试等, 该回调函数是`trainer.train`函数里设定。
-`event_handler_plot`可以用来利用回调数据来打点画图:
-
-
-
-图12. 训练结果
-
-
-
-```python
-params_dirname = "image_classification_resnet.inference.model"
-
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-
-step = 0
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 1 == 0:
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- avg_cost, accuracy = trainer.test(
- reader=test_reader,
- feed_order=['pixel', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
-
- # save parameters
- if params_dirname is not None:
- trainer.save_params(params_dirname)
-```
-
`event_handler` 用来在训练过程中输出文本日志
```python
diff --git a/03.image_classification/README.md b/03.image_classification/README.md
index fc58147ab77bdd6f470df504f65847bd9363e9b6..73cfe690955bf001757e79e3e18eb62b45f0df86 100644
--- a/03.image_classification/README.md
+++ b/03.image_classification/README.md
@@ -385,38 +385,6 @@ test_reader = paddle.batch(
Callback function `event_handler` will be called during training when a pre-defined event happens.
-`event_handler_plot`is used to plot a figure like below:
-
-![png](./image/train_and_test.png)
-
-```python
-params_dirname = "image_classification_resnet.inference.model"
-
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-
-step = 0
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 1 == 0:
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- avg_cost, accuracy = trainer.test(
- reader=test_reader,
- feed_order=['pixel', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
-
- # save parameters
- if params_dirname is not None:
- trainer.save_params(params_dirname)
-```
-
`event_handler` is used to plot some text data when training.
```python
diff --git a/03.image_classification/index.cn.html b/03.image_classification/index.cn.html
index 6278edb200f80f3336a2b4722756142bb708079e..da5aab5488df8018a22e652f59874e0301c9ad47 100644
--- a/03.image_classification/index.cn.html
+++ b/03.image_classification/index.cn.html
@@ -425,42 +425,6 @@ test_reader = paddle.batch(
可以使用`event_handler`回调函数来观察训练过程,或进行测试等, 该回调函数是`trainer.train`函数里设定。
-`event_handler_plot`可以用来利用回调数据来打点画图:
-
-
-
-图12. 训练结果
-
-
-
-```python
-params_dirname = "image_classification_resnet.inference.model"
-
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-
-step = 0
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 1 == 0:
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- avg_cost, accuracy = trainer.test(
- reader=test_reader,
- feed_order=['pixel', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
-
- # save parameters
- if params_dirname is not None:
- trainer.save_params(params_dirname)
-```
-
`event_handler` 用来在训练过程中输出文本日志
```python
diff --git a/03.image_classification/index.html b/03.image_classification/index.html
index fe6d7b92033443127b64371374dbe1ae8c7207d6..71bde172262a77e04d770e330670385fd74dadf2 100644
--- a/03.image_classification/index.html
+++ b/03.image_classification/index.html
@@ -427,38 +427,6 @@ test_reader = paddle.batch(
Callback function `event_handler` will be called during training when a pre-defined event happens.
-`event_handler_plot`is used to plot a figure like below:
-
-![png](./image/train_and_test.png)
-
-```python
-params_dirname = "image_classification_resnet.inference.model"
-
-from paddle.utils import Ploter
-
-train_title = "Train cost"
-test_title = "Test cost"
-cost_ploter = Ploter(train_title, test_title)
-
-step = 0
-def event_handler_plot(event):
- global step
- if isinstance(event, EndStepEvent):
- if step % 1 == 0:
- cost_ploter.append(train_title, step, event.metrics[0])
- cost_ploter.plot()
- step += 1
- if isinstance(event, EndEpochEvent):
- avg_cost, accuracy = trainer.test(
- reader=test_reader,
- feed_order=['pixel', 'label'])
- cost_ploter.append(test_title, step, avg_cost)
-
- # save parameters
- if params_dirname is not None:
- trainer.save_params(params_dirname)
-```
-
`event_handler` is used to plot some text data when training.
```python
diff --git a/05.recommender_system/README.cn.md b/05.recommender_system/README.cn.md
index ccd7286ab31cf06960c1753927e8f27cbc55e45e..0ea8bcca3b3cb68dc9c4bfbbab0c85c3b9c7eef5 100644
--- a/05.recommender_system/README.cn.md
+++ b/05.recommender_system/README.cn.md
@@ -439,30 +439,26 @@ feed_order = [
```python
# Specify the directory path to save the parameters
params_dirname = "recommender_system.inference.model"
-
-from paddle.utils import Ploter
-test_title = "Test cost"
-plot_cost = Ploter(test_title)
-
-
def event_handler(event):
if isinstance(event, EndStepEvent):
+ test_reader = paddle.batch(
+ paddle.dataset.movielens.test(), batch_size=BATCH_SIZE)
avg_cost_set = trainer.test(
reader=test_reader, feed_order=feed_order)
# get avg cost
avg_cost = np.array(avg_cost_set).mean()
- plot_cost.append(test_title, event.step, avg_cost_set[0])
- plot_cost.plot()
-
print("avg_cost: %s" % avg_cost)
- print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
- float(avg_cost)))
- if event.step == 20: # Adjust this number for accuracy
+ if float(avg_cost) < 4: # Change this number to adjust accuracy
trainer.save_params(params_dirname)
trainer.stop()
+ else:
+ print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
+ float(avg_cost)))
+ if math.isnan(float(avg_cost)):
+ sys.exit("got NaN loss, training failed.")
```
### 开始训练
diff --git a/05.recommender_system/README.md b/05.recommender_system/README.md
index 5f3e395601de8cd41a1334b90880e456c8de076f..718709361beab54dc0730fa1ca531caaeb32d90e 100644
--- a/05.recommender_system/README.md
+++ b/05.recommender_system/README.md
@@ -415,30 +415,26 @@ For example, we can check the cost by `trainer.test` when `EndStepEvent` occurs
# Specify the directory path to save the parameters
params_dirname = "recommender_system.inference.model"
-from paddle.utils import Ploter
-test_title = "Test cost"
-plot_cost = Ploter(test_title)
-
-
def event_handler(event):
if isinstance(event, EndStepEvent):
+ test_reader = paddle.batch(
+ paddle.dataset.movielens.test(), batch_size=BATCH_SIZE)
avg_cost_set = trainer.test(
reader=test_reader, feed_order=feed_order)
# get avg cost
avg_cost = np.array(avg_cost_set).mean()
- plot_cost.append(test_title, event.step, avg_cost_set[0])
- plot_cost.plot()
-
print("avg_cost: %s" % avg_cost)
- print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
- float(avg_cost)))
- if event.step == 20: # Adjust this number for accuracy
+ if float(avg_cost) < 4: # Change this number to adjust accuracy
trainer.save_params(params_dirname)
trainer.stop()
-
+ else:
+ print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
+ float(avg_cost)))
+ if math.isnan(float(avg_cost)):
+ sys.exit("got NaN loss, training failed.")
```
### Training
diff --git a/05.recommender_system/index.cn.html b/05.recommender_system/index.cn.html
index 3c9f63ffd4b9e0987deb6839b07f5555a6dbfd98..974725df2c86b1cd4a2e4bae0945b7cc382288fb 100644
--- a/05.recommender_system/index.cn.html
+++ b/05.recommender_system/index.cn.html
@@ -481,30 +481,26 @@ feed_order = [
```python
# Specify the directory path to save the parameters
params_dirname = "recommender_system.inference.model"
-
-from paddle.utils import Ploter
-test_title = "Test cost"
-plot_cost = Ploter(test_title)
-
-
def event_handler(event):
if isinstance(event, EndStepEvent):
+ test_reader = paddle.batch(
+ paddle.dataset.movielens.test(), batch_size=BATCH_SIZE)
avg_cost_set = trainer.test(
reader=test_reader, feed_order=feed_order)
# get avg cost
avg_cost = np.array(avg_cost_set).mean()
- plot_cost.append(test_title, event.step, avg_cost_set[0])
- plot_cost.plot()
-
print("avg_cost: %s" % avg_cost)
- print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
- float(avg_cost)))
- if event.step == 20: # Adjust this number for accuracy
+ if float(avg_cost) < 4: # Change this number to adjust accuracy
trainer.save_params(params_dirname)
trainer.stop()
+ else:
+ print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
+ float(avg_cost)))
+ if math.isnan(float(avg_cost)):
+ sys.exit("got NaN loss, training failed.")
```
### 开始训练
diff --git a/05.recommender_system/index.html b/05.recommender_system/index.html
index 512f5f4779d5be914a3f80697b7646c8da85c351..403da9fc02348f2936e1ca6862660a9a5d624776 100644
--- a/05.recommender_system/index.html
+++ b/05.recommender_system/index.html
@@ -457,30 +457,26 @@ For example, we can check the cost by `trainer.test` when `EndStepEvent` occurs
# Specify the directory path to save the parameters
params_dirname = "recommender_system.inference.model"
-from paddle.utils import Ploter
-test_title = "Test cost"
-plot_cost = Ploter(test_title)
-
-
def event_handler(event):
if isinstance(event, EndStepEvent):
+ test_reader = paddle.batch(
+ paddle.dataset.movielens.test(), batch_size=BATCH_SIZE)
avg_cost_set = trainer.test(
reader=test_reader, feed_order=feed_order)
# get avg cost
avg_cost = np.array(avg_cost_set).mean()
- plot_cost.append(test_title, event.step, avg_cost_set[0])
- plot_cost.plot()
-
print("avg_cost: %s" % avg_cost)
- print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
- float(avg_cost)))
- if event.step == 20: # Adjust this number for accuracy
+ if float(avg_cost) < 4: # Change this number to adjust accuracy
trainer.save_params(params_dirname)
trainer.stop()
-
+ else:
+ print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
+ float(avg_cost)))
+ if math.isnan(float(avg_cost)):
+ sys.exit("got NaN loss, training failed.")
```
### Training