Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
delete untracked file
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 18, 2018
1 parent 1685f67 commit 89c0c00
Show file tree
Hide file tree
Showing 30 changed files with 413 additions and 136 deletions.
32 changes: 24 additions & 8 deletions 01.fit_a_line/README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ import paddle
import paddle.fluid as fluid
import numpy
from __future__ import print_function
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *

```

我们通过uci_housing模块引入了数据集合[UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing)
Expand Down Expand Up @@ -167,7 +177,7 @@ place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
训练器会读入一个训练程序和一些必要的其他参数:

```python
trainer = fluid.contrib.trainer.Trainer(
trainer = Trainer(
train_func=train_program,
place=place,
optimizer_func=optimizer_program)
Expand All @@ -186,30 +196,36 @@ 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(event):
def event_handler_plot(event):
global step
if isinstance(event, fluid.contrib.trainer.EndStepEvent):
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))
plot_cost.append(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)
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
plot_cost.append(test_title, step, test_metrics[0])
plot_cost.plot()

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()
step += 1

if isinstance(event, fluid.contrib.trainer.EndEpochEvent):
if isinstance(event, EndEpochEvent):
if event.epoch % 10 == 0:
# We can save the trained parameters for the inferences later
if params_dirname is not None:
Expand All @@ -226,7 +242,7 @@ def event_handler(event):
trainer.train(
reader=train_reader,
num_epochs=100,
event_handler=event_handler,
event_handler=event_handler_plot,
feed_order=feed_order)
```

Expand All @@ -249,7 +265,7 @@ def inference_program():
预测器会从`params_dirname`中读取已经训练好的模型,来对从未遇见过的数据进行预测。

```python
inferencer = fluid.contrib.inferencer.Inferencer(
inferencer = Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place)

batch_size = 10
Expand Down
32 changes: 23 additions & 9 deletions 01.fit_a_line/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ import paddle
import paddle.fluid as fluid
import numpy
from __future__ import print_function
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *

```

We encapsulated the [UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing) in our Python module `uci_housing`. This module can
Expand Down Expand Up @@ -180,7 +190,7 @@ place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
The trainer will take the `train_program` as input.

```python
trainer = fluid.contrib.trainer.Trainer(
trainer = Trainer(
train_func=train_program,
place=place,
optimizer_func=optimizer_program)
Expand All @@ -202,24 +212,28 @@ 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(event):
def event_handler_plot(event):
global step
if isinstance(event, fluid.contrib.trainer.EndStepEvent):
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))

plot_cost.append(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)
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))

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 the accuracy is good enough, we can stop the training.
print('loss is less than 10.0, stop')
trainer.stop()
Expand All @@ -244,7 +258,7 @@ We now can start training by calling `trainer.train()`.
trainer.train(
reader=train_reader,
num_epochs=100,
event_handler=event_handler,
event_handler=event_handler_plot,
feed_order=feed_order)

```
Expand Down Expand Up @@ -272,7 +286,7 @@ def inference_program():
Inferencer will load the trained model from `params_dirname` and use it to infer the unseen data.

```python
inferencer = fluid.contrib.inferencer.Inferencer(
inferencer = Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place)

batch_size = 10
Expand Down
32 changes: 24 additions & 8 deletions 01.fit_a_line/index.cn.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@
import paddle.fluid as fluid
import numpy
from __future__ import print_function
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *

```

我们通过uci_housing模块引入了数据集合[UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing)
Expand Down Expand Up @@ -209,7 +219,7 @@
训练器会读入一个训练程序和一些必要的其他参数:

```python
trainer = fluid.contrib.trainer.Trainer(
trainer = Trainer(
train_func=train_program,
place=place,
optimizer_func=optimizer_program)
Expand All @@ -228,30 +238,36 @@
# 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(event):
def event_handler_plot(event):
global step
if isinstance(event, fluid.contrib.trainer.EndStepEvent):
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))
plot_cost.append(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)
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))
plot_cost.append(test_title, step, test_metrics[0])
plot_cost.plot()

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()
step += 1

if isinstance(event, fluid.contrib.trainer.EndEpochEvent):
if isinstance(event, EndEpochEvent):
if event.epoch % 10 == 0:
# We can save the trained parameters for the inferences later
if params_dirname is not None:
Expand All @@ -268,7 +284,7 @@
trainer.train(
reader=train_reader,
num_epochs=100,
event_handler=event_handler,
event_handler=event_handler_plot,
feed_order=feed_order)
```

Expand All @@ -291,7 +307,7 @@
预测器会从`params_dirname`中读取已经训练好的模型,来对从未遇见过的数据进行预测。

```python
inferencer = fluid.contrib.inferencer.Inferencer(
inferencer = Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place)

batch_size = 10
Expand Down
32 changes: 23 additions & 9 deletions 01.fit_a_line/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@
import paddle.fluid as fluid
import numpy
from __future__ import print_function
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *

```

We encapsulated the [UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing) in our Python module `uci_housing`. This module can
Expand Down Expand Up @@ -222,7 +232,7 @@
The trainer will take the `train_program` as input.

```python
trainer = fluid.contrib.trainer.Trainer(
trainer = Trainer(
train_func=train_program,
place=place,
optimizer_func=optimizer_program)
Expand All @@ -244,24 +254,28 @@
# 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(event):
def event_handler_plot(event):
global step
if isinstance(event, fluid.contrib.trainer.EndStepEvent):
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0]))

plot_cost.append(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)
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))

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 the accuracy is good enough, we can stop the training.
print('loss is less than 10.0, stop')
trainer.stop()
Expand All @@ -286,7 +300,7 @@
trainer.train(
reader=train_reader,
num_epochs=100,
event_handler=event_handler,
event_handler=event_handler_plot,
feed_order=feed_order)

```
Expand Down Expand Up @@ -314,7 +328,7 @@
Inferencer will load the trained model from `params_dirname` and use it to infer the unseen data.

```python
inferencer = fluid.contrib.inferencer.Inferencer(
inferencer = Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place)

batch_size = 10
Expand Down
15 changes: 8 additions & 7 deletions 01.fit_a_line/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,26 @@ def optimizer_program():
# 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(event):
def event_handler_plot(event):
global step
if isinstance(event, EndStepEvent):
if step % 10 == 0: # record a train cost every 10 batches
print("%s, Step %d, Cost %f" %
(train_title, step, event.metrics[0]))

plot_cost.append(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)
print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0]))

plot_cost.append(test_title, step, test_metrics[0])
plot_cost.plot()
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')
Expand All @@ -105,7 +106,7 @@ def event_handler(event):
trainer.train(
reader=train_reader,
num_epochs=100,
event_handler=event_handler,
event_handler=event_handler_plot,
feed_order=feed_order)


Expand Down
Loading

0 comments on commit 89c0c00

Please sign in to comment.