Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
13c6f692
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
13c6f692
编写于
10月 25, 2018
作者:
R
ruri
提交者:
Tao Luo
10月 25, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove utils plot until paddle support (#639)
* Remove Utils until paddle support * delete figures until paddle support
上级
90dfa576
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
57 addition
and
364 deletion
+57
-364
01.fit_a_line/README.cn.md
01.fit_a_line/README.cn.md
+4
-8
01.fit_a_line/README.md
01.fit_a_line/README.md
+5
-9
01.fit_a_line/index.cn.html
01.fit_a_line/index.cn.html
+4
-8
01.fit_a_line/index.html
01.fit_a_line/index.html
+5
-9
01.fit_a_line/train.py
01.fit_a_line/train.py
+5
-8
02.recognize_digits/README.cn.md
02.recognize_digits/README.cn.md
+0
-36
02.recognize_digits/README.md
02.recognize_digits/README.md
+1
-33
02.recognize_digits/index.cn.html
02.recognize_digits/index.cn.html
+0
-36
02.recognize_digits/index.html
02.recognize_digits/index.html
+1
-33
03.image_classification/README.cn.md
03.image_classification/README.cn.md
+0
-36
03.image_classification/README.md
03.image_classification/README.md
+0
-32
03.image_classification/index.cn.html
03.image_classification/index.cn.html
+0
-36
03.image_classification/index.html
03.image_classification/index.html
+0
-32
05.recommender_system/README.cn.md
05.recommender_system/README.cn.md
+8
-12
05.recommender_system/README.md
05.recommender_system/README.md
+8
-12
05.recommender_system/index.cn.html
05.recommender_system/index.cn.html
+8
-12
05.recommender_system/index.html
05.recommender_system/index.html
+8
-12
未找到文件。
01.fit_a_line/README.cn.md
浏览文件 @
13c6f692
...
...
@@ -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
p
lot_cost
.
append
(
train_title
,
step
,
event
.
metrics
[
0
]
)
p
rint
(
"%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
)
```
...
...
01.fit_a_line/README.md
浏览文件 @
13c6f692
...
...
@@ -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
)
```
...
...
01.fit_a_line/index.cn.html
浏览文件 @
13c6f692
...
...
@@ -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
p
lot_cost.append(train_title, step, event.metrics[0]
)
p
rint("%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)
```
...
...
01.fit_a_line/index.html
浏览文件 @
13c6f692
...
...
@@ -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)
```
...
...
01.fit_a_line/train.py
浏览文件 @
13c6f692
...
...
@@ -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
)
...
...
02.recognize_digits/README.cn.md
浏览文件 @
13c6f692
...
...
@@ -321,42 +321,6 @@ def event_handler(event):
lists
.
append
((
event
.
epoch
,
avg_cost
,
acc
))
```
`event_handler_plot`
可以用来在训练过程中画图如下:
<div
align=
"center"
>
<img
src=
"https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/image/train_and_test.png?raw=true"
width=
"400"
><br/>
图7 训练结果
</div>
```
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
))
```
#### 开始训练
...
...
02.recognize_digits/README.md
浏览文件 @
13c6f692
...
...
@@ -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'])
```
...
...
02.recognize_digits/index.cn.html
浏览文件 @
13c6f692
...
...
@@ -363,42 +363,6 @@ def event_handler(event):
lists.append((event.epoch, avg_cost, acc))
```
`event_handler_plot` 可以用来在训练过程中画图如下:
<div
align=
"center"
>
<img
src=
"https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/image/train_and_test.png?raw=true"
width=
"400"
><br/>
图7 训练结果
</div>
```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))
```
#### 开始训练
...
...
02.recognize_digits/index.html
浏览文件 @
13c6f692
...
...
@@ -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'])
```
...
...
03.image_classification/README.cn.md
浏览文件 @
13c6f692
...
...
@@ -383,42 +383,6 @@ test_reader = paddle.batch(
可以使用
`event_handler`
回调函数来观察训练过程,或进行测试等, 该回调函数是
`trainer.train`
函数里设定。
`event_handler_plot`
可以用来利用回调数据来打点画图:
<p
align=
"center"
>
<img
src=
"https://github.com/PaddlePaddle/book/blob/develop/03.image_classification/image/train_and_test.png?raw=true"
width=
"350"
><br/>
图12. 训练结果
</p>
```
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
...
...
03.image_classification/README.md
浏览文件 @
13c6f692
...
...
@@ -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
...
...
03.image_classification/index.cn.html
浏览文件 @
13c6f692
...
...
@@ -425,42 +425,6 @@ test_reader = paddle.batch(
可以使用`event_handler`回调函数来观察训练过程,或进行测试等, 该回调函数是`trainer.train`函数里设定。
`event_handler_plot`可以用来利用回调数据来打点画图:
<p
align=
"center"
>
<img
src=
"https://github.com/PaddlePaddle/book/blob/develop/03.image_classification/image/train_and_test.png?raw=true"
width=
"350"
><br/>
图12. 训练结果
</p>
```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
...
...
03.image_classification/index.html
浏览文件 @
13c6f692
...
...
@@ -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
...
...
05.recommender_system/README.cn.md
浏览文件 @
13c6f692
...
...
@@ -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."
)
```
### 开始训练
...
...
05.recommender_system/README.md
浏览文件 @
13c6f692
...
...
@@ -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
...
...
05.recommender_system/index.cn.html
浏览文件 @
13c6f692
...
...
@@ -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.
")
```
###
开始训练
...
...
05.recommender_system/index.html
浏览文件 @
13c6f692
...
...
@@ -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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录