提交 3d0283e5 编写于 作者: L lujun

update print out,test=develop

上级 3a24bdbd
...@@ -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)
``` ```
......
...@@ -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)
``` ```
......
...@@ -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.
先完成此消息的编辑!
想要评论请 注册