提交 90355785 编写于 作者: J JiabinYang

Refine the comments and the inference result format for 01.fit_a_line

上级 afe261b2
...@@ -189,7 +189,7 @@ feed_order=['x', 'y'] ...@@ -189,7 +189,7 @@ feed_order=['x', 'y']
Moreover, an event handler is provided to print the training progress: Moreover, an event handler is provided to print the training progress:
```python ```python
# Specify the directory path to save the parameters # Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model" params_dirname = "fit_a_line.inference.model"
# Plot data # Plot data
...@@ -200,11 +200,11 @@ plot_cost = Ploter(train_title, test_title) ...@@ -200,11 +200,11 @@ plot_cost = Ploter(train_title, test_title)
step = 0 step = 0
# event_handler to print training and testing info # event_handler prints training and testing info
def event_handler_plot(event): def event_handler_plot(event):
global step global step
if isinstance(event, fluid.EndStepEvent): if isinstance(event, fluid.EndStepEvent):
if event.step % 10 == 0: # every 10 batches, record a test cost if event.step % 10 == 0: #record a test cost every 10 batches
test_metrics = trainer.test( test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order) reader=test_reader, feed_order=feed_order)
...@@ -263,10 +263,20 @@ inferencer = fluid.Inferencer( ...@@ -263,10 +263,20 @@ inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
batch_size = 10 batch_size = 10
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32") test_reader = paddle.batch(paddle.dataset.uci_housing.test(),batch_size=batch_size)
test_data = test_reader().next()
test_feat = numpy.array([data[0] for data in test_data]).astype("float32")
test_label = numpy.array([data[1] for data in test_data]).astype("float32")
results = inferencer.infer({'x': tensor_x}) results = inferencer.infer({'x': test_feat})
print("infer results: ", results[0])
print("infer results: (House Price)")
for k in range(0, batch_size-1):
print("%d. %f" % (k, results[0][k]))
print("\nground truth:")
for k in range(0, batch_size-1):
print("%d. %f" % (k, test_label[k]))
``` ```
## Summary ## Summary
......
...@@ -231,7 +231,7 @@ feed_order=['x', 'y'] ...@@ -231,7 +231,7 @@ feed_order=['x', 'y']
Moreover, an event handler is provided to print the training progress: Moreover, an event handler is provided to print the training progress:
```python ```python
# Specify the directory path to save the parameters # Specify the directory to save the parameters
params_dirname = "fit_a_line.inference.model" params_dirname = "fit_a_line.inference.model"
# Plot data # Plot data
...@@ -242,11 +242,11 @@ plot_cost = Ploter(train_title, test_title) ...@@ -242,11 +242,11 @@ plot_cost = Ploter(train_title, test_title)
step = 0 step = 0
# event_handler to print training and testing info # event_handler prints training and testing info
def event_handler_plot(event): def event_handler_plot(event):
global step global step
if isinstance(event, fluid.EndStepEvent): if isinstance(event, fluid.EndStepEvent):
if event.step % 10 == 0: # every 10 batches, record a test cost if event.step % 10 == 0: #record a test cost every 10 batches
test_metrics = trainer.test( test_metrics = trainer.test(
reader=test_reader, feed_order=feed_order) reader=test_reader, feed_order=feed_order)
...@@ -305,10 +305,20 @@ inferencer = fluid.Inferencer( ...@@ -305,10 +305,20 @@ inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
batch_size = 10 batch_size = 10
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32") test_reader = paddle.batch(paddle.dataset.uci_housing.test(),batch_size=batch_size)
test_data = test_reader().next()
test_feat = numpy.array([data[0] for data in test_data]).astype("float32")
test_label = numpy.array([data[1] for data in test_data]).astype("float32")
results = inferencer.infer({'x': tensor_x}) results = inferencer.infer({'x': test_feat})
print("infer results: ", results[0])
print("infer results: (House Price)")
for k in range(0, batch_size-1):
print("%d. %f" % (k, results[0][k]))
print("\nground truth:")
for k in range(0, batch_size-1):
print("%d. %f" % (k, test_label[k]))
``` ```
## Summary ## Summary
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册