Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
3d0283e5
B
book
项目概览
PaddlePaddle
/
book
通知
17
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看板
提交
3d0283e5
编写于
12月 03, 2018
作者:
L
lujun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update print out,test=develop
上级
3a24bdbd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
25 addition
and
31 deletion
+25
-31
01.fit_a_line/README.cn.md
01.fit_a_line/README.cn.md
+8
-10
01.fit_a_line/index.cn.html
01.fit_a_line/index.cn.html
+8
-10
01.fit_a_line/train.py
01.fit_a_line/train.py
+9
-11
未找到文件。
01.fit_a_line/README.cn.md
浏览文件 @
3d0283e5
...
@@ -155,7 +155,7 @@ use_cuda = False
...
@@ -155,7 +155,7 @@ use_cuda = False
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
```
```
除此之外,还可以
定义一个事件响应器来处理类似
`打印训练进程`
的事件
:
除此之外,还可以
通过画图,来展现
`训练进程`
:
```
python
```
python
# Plot data
# Plot data
...
@@ -165,10 +165,6 @@ train_title = "Train cost"
...
@@ -165,10 +165,6 @@ train_title = "Train cost"
test_title
=
"Test cost"
test_title
=
"Test cost"
plot_cost
=
Ploter
(
train_title
,
test_title
)
plot_cost
=
Ploter
(
train_title
,
test_title
)
def
event_handler
(
title
,
loop_step
,
handler_val
):
plot_cost
.
append
(
title
,
loop_step
,
handler_val
)
plot_cost
.
plot
()
```
```
### 创建训练过程
### 创建训练过程
...
@@ -216,11 +212,13 @@ def train_loop(main_program):
...
@@ -216,11 +212,13 @@ def train_loop(main_program):
feed
=
feeder
.
feed
(
data_train
),
feed
=
feeder
.
feed
(
data_train
),
fetch_list
=
[
avg_loss
])
fetch_list
=
[
avg_loss
])
if
step
%
10
==
0
:
# record a train cost every 10 batches
if
step
%
10
==
0
:
# record a train cost every 10 batches
event_handler
(
train_title
,
step
,
avg_loss_value
[
0
])
plot_cost
.
append
(
train_title
,
step
,
avg_loss_value
[
0
])
plot_cost
.
plot
()
if
step
%
100
==
0
:
# record a test cost every 100 batches
if
step
%
100
==
0
:
# record a test cost every 100 batches
test_metics
=
train_test
(
train_program
=
main_program
,
test_metics
=
train_test
(
train_program
=
main_program
,
feeder
=
feeder_test
)
feeder
=
feeder_test
)
event_handler
(
test_title
,
step
,
test_metics
[
0
])
plot_cost
.
append
(
test_title
,
step
,
test_metics
[
0
])
plot_cost
.
plot
()
# If the accuracy is good enough, we can stop the training.
# If the accuracy is good enough, we can stop the training.
if
test_metics
[
0
]
<
10.0
:
if
test_metics
[
0
]
<
10.0
:
return
return
...
@@ -229,9 +227,9 @@ def train_loop(main_program):
...
@@ -229,9 +227,9 @@ def train_loop(main_program):
if
math
.
isnan
(
float
(
avg_loss_value
)):
if
math
.
isnan
(
float
(
avg_loss_value
)):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
if
params_dirname
is
not
None
:
if
params_dirname
is
not
None
:
# We can save the trained parameters for the inferences later
# We can save the trained parameters for the inferences later
fluid
.
io
.
save_inference_model
(
params_dirname
,
[
'x'
],
fluid
.
io
.
save_inference_model
(
params_dirname
,
[
'x'
],
[
y_predict
],
exe
)
[
y_predict
],
exe
)
```
```
...
...
01.fit_a_line/index.cn.html
浏览文件 @
3d0283e5
...
@@ -197,7 +197,7 @@ use_cuda = False
...
@@ -197,7 +197,7 @@ use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
```
```
除此之外,还可以
定义一个事件响应器来处理类似`打印训练进程`的事件
:
除此之外,还可以
通过画图,来展现`训练进程`
:
```python
```python
# Plot data
# Plot data
...
@@ -207,10 +207,6 @@ train_title = "Train cost"
...
@@ -207,10 +207,6 @@ train_title = "Train cost"
test_title = "Test cost"
test_title = "Test cost"
plot_cost = Ploter(train_title, test_title)
plot_cost = Ploter(train_title, test_title)
def event_handler(title, loop_step, handler_val):
plot_cost.append(title, loop_step, handler_val)
plot_cost.plot()
```
```
### 创建训练过程
### 创建训练过程
...
@@ -258,11 +254,13 @@ def train_loop(main_program):
...
@@ -258,11 +254,13 @@ def train_loop(main_program):
feed=feeder.feed(data_train),
feed=feeder.feed(data_train),
fetch_list=[avg_loss])
fetch_list=[avg_loss])
if step % 10 == 0: # record a train cost every 10 batches
if step % 10 == 0: # record a train cost every 10 batches
event_handler(train_title, step, avg_loss_value[0])
plot_cost.append(train_title, step, avg_loss_value[0])
plot_cost.plot()
if step % 100 == 0: # record a test cost every 100 batches
if step % 100 == 0: # record a test cost every 100 batches
test_metics = train_test(train_program=main_program,
test_metics = train_test(train_program=main_program,
feeder=feeder_test)
feeder=feeder_test)
event_handler(test_title, step, test_metics[0])
plot_cost.append(test_title, step, test_metics[0])
plot_cost.plot()
# If the accuracy is good enough, we can stop the training.
# If the accuracy is good enough, we can stop the training.
if test_metics[0]
<
10.0
:
if test_metics[0]
<
10.0
:
return
return
...
@@ -271,9 +269,9 @@ def train_loop(main_program):
...
@@ -271,9 +269,9 @@ def train_loop(main_program):
if
math.isnan
(
float
(
avg_loss_value
))
:
if
math.isnan
(
float
(
avg_loss_value
))
:
sys.exit
("
got
NaN
loss
,
training
failed.
")
sys.exit
("
got
NaN
loss
,
training
failed.
")
if
params_dirname
is
not
None:
if
params_dirname
is
not
None:
#
We
can
save
the
trained
parameters
for
the
inferences
later
#
We
can
save
the
trained
parameters
for
the
inferences
later
fluid.io.save_inference_model
(
params_dirname
,
['
x
'],
fluid.io.save_inference_model
(
params_dirname
,
['
x
'],
[
y_predict
],
exe
)
[
y_predict
],
exe
)
```
```
...
...
01.fit_a_line/train.py
浏览文件 @
3d0283e5
...
@@ -21,11 +21,6 @@ import math
...
@@ -21,11 +21,6 @@ import math
import
sys
import
sys
# event_handler prints training and testing info
def
event_handler
(
title
,
loop_step
,
handler_val
):
print
(
"%s, Step %d, Cost %f"
%
(
title
,
loop_step
,
handler_val
))
def
main
():
def
main
():
batch_size
=
20
batch_size
=
20
...
@@ -93,10 +88,13 @@ def main():
...
@@ -93,10 +88,13 @@ def main():
feed
=
feeder
.
feed
(
data_train
),
feed
=
feeder
.
feed
(
data_train
),
fetch_list
=
[
avg_loss
])
fetch_list
=
[
avg_loss
])
if
step
%
10
==
0
:
# record a train cost every 10 batches
if
step
%
10
==
0
:
# record a train cost every 10 batches
event_handler
(
train_title
,
step
,
avg_loss_value
[
0
])
print
(
"%s, Step %d, Cost %f"
%
(
train_title
,
step
,
avg_loss_value
[
0
]))
if
step
%
100
==
0
:
# record a test cost every 100 batches
test_metics
=
train_test
(
test_metics
=
train_test
(
program
=
test_program
,
feeder
=
feeder_test
)
program
=
test_program
,
feeder
=
feeder_test
)
event_handler
(
test_title
,
step
,
test_metics
[
0
])
print
(
"%s, Step %d, Cost %f"
%
(
test_title
,
step
,
test_metics
[
0
]))
# If the accuracy is good enough, we can stop the training.
# If the accuracy is good enough, we can stop the training.
if
test_metics
[
0
]
<
10.0
:
if
test_metics
[
0
]
<
10.0
:
return
return
...
@@ -105,10 +103,10 @@ def main():
...
@@ -105,10 +103,10 @@ def main():
if
math
.
isnan
(
float
(
avg_loss_value
)):
if
math
.
isnan
(
float
(
avg_loss_value
)):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
if
params_dirname
is
not
None
:
if
params_dirname
is
not
None
:
# We can save the trained parameters for the inferences later
# We can save the trained parameters for the inferences later
fluid
.
io
.
save_inference_model
(
params_dirname
,
[
'x'
],
[
y_predict
],
fluid
.
io
.
save_inference_model
(
params_dirname
,
[
'x'
],
exe
)
[
y_predict
],
exe
)
train_loop
()
train_loop
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录