未验证 提交 742ea426 编写于 作者: D daminglu 提交者: GitHub

Re-write Chapter 1 in Book to use new Fluid API (#524)

上级 3cac9f3f
...@@ -54,8 +54,9 @@ After setting up our model, there are several major steps to go through to train ...@@ -54,8 +54,9 @@ After setting up our model, there are several major steps to go through to train
Our program starts with importing necessary packages: Our program starts with importing necessary packages:
```python ```python
import paddle.v2 as paddle import paddle
import paddle.v2.dataset.uci_housing as uci_housing import paddle.fluid as fluid
import numpy
``` ```
We encapsulated the [UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing) in our Python module `uci_housing`. This module can We encapsulated the [UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing) in our Python module `uci_housing`. This module can
...@@ -116,49 +117,58 @@ When training complex models, we usually have one more split: the validation set ...@@ -116,49 +117,58 @@ When training complex models, we usually have one more split: the validation set
`fit_a_line/trainer.py` demonstrates the training using [PaddlePaddle](http://paddlepaddle.org). `fit_a_line/trainer.py` demonstrates the training using [PaddlePaddle](http://paddlepaddle.org).
### Initialize PaddlePaddle ### Datafeeder Configuration
```python We first define data feeders for test and train. The feeder reads a `BATCH_SIZE` of data each time and feed them to the training/testing process. Users can shuffle a batch out of a `buf_size` in order to make the data random.
paddle.init(use_gpu=False, trainer_count=1)
```
### Model Configuration ```python
BATCH_SIZE = 20
Linear regression is essentially a fully-connected layer with linear activation: train_reader = paddle.batch(
paddle.reader.shuffle(
paddle.dataset.uci_housing.train(), buf_size=500),
batch_size=BATCH_SIZE)
```python test_reader = paddle.batch(
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) paddle.reader.shuffle(
y_predict = paddle.layer.fc(input=x, paddle.dataset.uci_housing.test(), buf_size=500),
size=1, batch_size=BATCH_SIZE)
act=paddle.activation.Linear())
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
cost = paddle.layer.square_error_cost(input=y_predict, label=y)
``` ```
### Save Topology ### Train Program Configuration
The train_program must return the avg_loss as its first returned parameter and then use the inference_program to setup the train_program
```python ```python
# Save the inference topology to protobuf. def train_program():
inference_topology = paddle.topology.Topology(layers=y_predict) y = fluid.layers.data(name='y', shape=[1], dtype='float32')
with open("inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f) # feature vector of length 13
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
loss = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_loss = fluid.layers.mean(loss)
return avg_loss
``` ```
### Create Parameters ### Specify Place
Specify your training environment, you should specify if the training is on CPU or GPU.
```python ```python
parameters = paddle.parameters.create(cost) use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
``` ```
### Create Trainer ### Create Trainer
The trainer will take the train_program.
```python ```python
optimizer = paddle.optimizer.Momentum(momentum=0) trainer = fluid.Trainer(
train_func=train_program,
trainer = paddle.trainer.SGD(cost=cost, place=place,
parameters=parameters, optimizer=fluid.optimizer.SGD(learning_rate=0.001))
update_equation=optimizer)
``` ```
### Feeding Data ### Feeding Data
...@@ -168,105 +178,92 @@ PaddlePaddle provides the ...@@ -168,105 +178,92 @@ PaddlePaddle provides the
for loading the training data. A reader may return multiple columns, and we need a Python dictionary to specify the mapping from column index to data layers. for loading the training data. A reader may return multiple columns, and we need a Python dictionary to specify the mapping from column index to data layers.
```python ```python
feeding={'x': 0, 'y': 1} 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
# event_handler to print training and testing info # Specify the directory path to save the parameters
def event_handler(event): params_folder = "fit_a_line.inference.model"
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f" % (
event.pass_id, event.batch_id, event.cost)
if isinstance(event, paddle.event.EndPass):
result = trainer.test(
reader=paddle.batch(
uci_housing.test(), batch_size=2),
feeding=feeding)
print "Test %d, Cost %f" % (event.pass_id, result.cost)
```
```python # Plot data
# event_handler to plot training and testing info
from paddle.v2.plot import Ploter from paddle.v2.plot import Ploter
train_title = "Train cost" 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)
step = 0 step = 0
def event_handler_plot(event): # event_handler to print training and testing info
def event_handler(event):
global step global step
if isinstance(event, paddle.event.EndIteration): if isinstance(event, fluid.EndStepEvent):
if step % 10 == 0: # every 10 batches, record a train cost
plot_cost.append(train_title, step, event.cost)
if step % 100 == 0: # every 100 batches, record a test cost if step % 100 == 0: # every 100 batches, record a test cost
result = trainer.test( test_metrics = trainer.test(
reader=paddle.batch( reader=test_reader, feed_order=feed_order)
uci_housing.test(), batch_size=2),
feeding=feeding)
plot_cost.append(test_title, step, result.cost)
if step % 100 == 0: # every 100 batches, update cost plot print(test_metrics[0])
plot_cost.append(test_title, step, test_metrics[0])
plot_cost.plot() plot_cost.plot()
step += 1 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()
if step >= 2000:
# Or if it has been running for enough steps
print('has been running for 2000 steps, stop')
trainer.stop()
if isinstance(event, paddle.event.EndPass): # We can save the trained parameters for the inferences later
if event.pass_id % 10 == 0: if params_folder is not None:
with open('params_pass_%d.tar' % event.pass_id, 'w') as f: trainer.save_params(params_folder)
trainer.save_parameter_to_tar(f)
step += 1
``` ```
### Start Training ### Start Training
```python ```python
%matplotlib inline
# The training could take up to a few minutes.
trainer.train( trainer.train(
reader=paddle.batch( reader=train_reader,
paddle.reader.shuffle( num_epochs=100,
uci_housing.train(), buf_size=500), event_handler=event_handler,
batch_size=2), feed_order=feed_order)
feeding=feeding,
event_handler=event_handler_plot,
num_passes=30)
``` ```
![png](./image/train_and_test.png) ![png](./image/train_and_test.png)
### Apply model ### Inference
Initialize the Inferencer with the inference_program and the params_folder, which is where we saved our params
#### 1. generate testing data #### Setup the Inference Program.
Similar to the trainer.train, the Inferencer needs to take an inference_program to do inferring.
Prune the train_program to only have the y_predict.
```python ```python
test_data_creator = paddle.dataset.uci_housing.test() def inference_program():
test_data = [] x = fluid.layers.data(name='x', shape=[13], dtype='float32')
test_label = [] y_predict = fluid.layers.fc(input=x, size=1, act=None)
return y_predict
for item in test_data_creator():
test_data.append((item[0],))
test_label.append(item[1])
if len(test_data) == 5:
break
``` ```
#### 2. inference
```python ```python
# load parameters from tar file. inferencer = fluid.Inferencer(
# users can remove the comments and change the model name infer_func=inference_program, param_path=params_folder, place=place)
# with open('params_pass_20.tar', 'r') as f:
# parameters = paddle.parameters.Parameters.from_tar(f)
probs = paddle.infer( batch_size = 10
output_layer=y_predict, parameters=parameters, input=test_data) tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32")
for i in xrange(len(probs)): results = inferencer.infer({'x': tensor_x})
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) print("infer results: ", results[0])
``` ```
## Summary ## Summary
......
...@@ -96,8 +96,9 @@ After setting up our model, there are several major steps to go through to train ...@@ -96,8 +96,9 @@ After setting up our model, there are several major steps to go through to train
Our program starts with importing necessary packages: Our program starts with importing necessary packages:
```python ```python
import paddle.v2 as paddle import paddle
import paddle.v2.dataset.uci_housing as uci_housing import paddle.fluid as fluid
import numpy
``` ```
We encapsulated the [UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing) in our Python module `uci_housing`. This module can We encapsulated the [UCI Housing Data Set](https://archive.ics.uci.edu/ml/datasets/Housing) in our Python module `uci_housing`. This module can
...@@ -158,49 +159,58 @@ When training complex models, we usually have one more split: the validation set ...@@ -158,49 +159,58 @@ When training complex models, we usually have one more split: the validation set
`fit_a_line/trainer.py` demonstrates the training using [PaddlePaddle](http://paddlepaddle.org). `fit_a_line/trainer.py` demonstrates the training using [PaddlePaddle](http://paddlepaddle.org).
### Initialize PaddlePaddle ### Datafeeder Configuration
```python We first define data feeders for test and train. The feeder reads a `BATCH_SIZE` of data each time and feed them to the training/testing process. Users can shuffle a batch out of a `buf_size` in order to make the data random.
paddle.init(use_gpu=False, trainer_count=1)
```
### Model Configuration ```python
BATCH_SIZE = 20
Linear regression is essentially a fully-connected layer with linear activation: train_reader = paddle.batch(
paddle.reader.shuffle(
paddle.dataset.uci_housing.train(), buf_size=500),
batch_size=BATCH_SIZE)
```python test_reader = paddle.batch(
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) paddle.reader.shuffle(
y_predict = paddle.layer.fc(input=x, paddle.dataset.uci_housing.test(), buf_size=500),
size=1, batch_size=BATCH_SIZE)
act=paddle.activation.Linear())
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
cost = paddle.layer.square_error_cost(input=y_predict, label=y)
``` ```
### Save Topology ### Train Program Configuration
The train_program must return the avg_loss as its first returned parameter and then use the inference_program to setup the train_program
```python ```python
# Save the inference topology to protobuf. def train_program():
inference_topology = paddle.topology.Topology(layers=y_predict) y = fluid.layers.data(name='y', shape=[1], dtype='float32')
with open("inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f) # feature vector of length 13
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
loss = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_loss = fluid.layers.mean(loss)
return avg_loss
``` ```
### Create Parameters ### Specify Place
Specify your training environment, you should specify if the training is on CPU or GPU.
```python ```python
parameters = paddle.parameters.create(cost) use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
``` ```
### Create Trainer ### Create Trainer
The trainer will take the train_program.
```python ```python
optimizer = paddle.optimizer.Momentum(momentum=0) trainer = fluid.Trainer(
train_func=train_program,
trainer = paddle.trainer.SGD(cost=cost, place=place,
parameters=parameters, optimizer=fluid.optimizer.SGD(learning_rate=0.001))
update_equation=optimizer)
``` ```
### Feeding Data ### Feeding Data
...@@ -210,105 +220,92 @@ PaddlePaddle provides the ...@@ -210,105 +220,92 @@ PaddlePaddle provides the
for loading the training data. A reader may return multiple columns, and we need a Python dictionary to specify the mapping from column index to data layers. for loading the training data. A reader may return multiple columns, and we need a Python dictionary to specify the mapping from column index to data layers.
```python ```python
feeding={'x': 0, 'y': 1} 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
# event_handler to print training and testing info # Specify the directory path to save the parameters
def event_handler(event): params_folder = "fit_a_line.inference.model"
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f" % (
event.pass_id, event.batch_id, event.cost)
if isinstance(event, paddle.event.EndPass):
result = trainer.test(
reader=paddle.batch(
uci_housing.test(), batch_size=2),
feeding=feeding)
print "Test %d, Cost %f" % (event.pass_id, result.cost)
```
```python # Plot data
# event_handler to plot training and testing info
from paddle.v2.plot import Ploter from paddle.v2.plot import Ploter
train_title = "Train cost" 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)
step = 0 step = 0
def event_handler_plot(event): # event_handler to print training and testing info
def event_handler(event):
global step global step
if isinstance(event, paddle.event.EndIteration): if isinstance(event, fluid.EndStepEvent):
if step % 10 == 0: # every 10 batches, record a train cost
plot_cost.append(train_title, step, event.cost)
if step % 100 == 0: # every 100 batches, record a test cost if step % 100 == 0: # every 100 batches, record a test cost
result = trainer.test( test_metrics = trainer.test(
reader=paddle.batch( reader=test_reader, feed_order=feed_order)
uci_housing.test(), batch_size=2),
feeding=feeding)
plot_cost.append(test_title, step, result.cost)
if step % 100 == 0: # every 100 batches, update cost plot print(test_metrics[0])
plot_cost.append(test_title, step, test_metrics[0])
plot_cost.plot() plot_cost.plot()
step += 1 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()
if step >= 2000:
# Or if it has been running for enough steps
print('has been running for 2000 steps, stop')
trainer.stop()
if isinstance(event, paddle.event.EndPass): # We can save the trained parameters for the inferences later
if event.pass_id % 10 == 0: if params_folder is not None:
with open('params_pass_%d.tar' % event.pass_id, 'w') as f: trainer.save_params(params_folder)
trainer.save_parameter_to_tar(f)
step += 1
``` ```
### Start Training ### Start Training
```python ```python
%matplotlib inline
# The training could take up to a few minutes.
trainer.train( trainer.train(
reader=paddle.batch( reader=train_reader,
paddle.reader.shuffle( num_epochs=100,
uci_housing.train(), buf_size=500), event_handler=event_handler,
batch_size=2), feed_order=feed_order)
feeding=feeding,
event_handler=event_handler_plot,
num_passes=30)
``` ```
![png](./image/train_and_test.png) ![png](./image/train_and_test.png)
### Apply model ### Inference
Initialize the Inferencer with the inference_program and the params_folder, which is where we saved our params
#### 1. generate testing data #### Setup the Inference Program.
Similar to the trainer.train, the Inferencer needs to take an inference_program to do inferring.
Prune the train_program to only have the y_predict.
```python ```python
test_data_creator = paddle.dataset.uci_housing.test() def inference_program():
test_data = [] x = fluid.layers.data(name='x', shape=[13], dtype='float32')
test_label = [] y_predict = fluid.layers.fc(input=x, size=1, act=None)
return y_predict
for item in test_data_creator():
test_data.append((item[0],))
test_label.append(item[1])
if len(test_data) == 5:
break
``` ```
#### 2. inference
```python ```python
# load parameters from tar file. inferencer = fluid.Inferencer(
# users can remove the comments and change the model name infer_func=inference_program, param_path=params_folder, place=place)
# with open('params_pass_20.tar', 'r') as f:
# parameters = paddle.parameters.Parameters.from_tar(f)
probs = paddle.infer( batch_size = 10
output_layer=y_predict, parameters=parameters, input=test_data) tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32")
for i in xrange(len(probs)): results = inferencer.infer({'x': tensor_x})
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0]) print("infer results: ", results[0])
``` ```
## Summary ## Summary
......
import os # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
import paddle.v2 as paddle #
import paddle.v2.dataset.uci_housing as uci_housing # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
with_gpu = os.getenv('WITH_GPU', '0') != '0' # You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
def main(): #
# init # Unless required by applicable law or agreed to in writing, software
paddle.init(use_gpu=with_gpu, trainer_count=1) # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# network config # See the License for the specific language governing permissions and
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) # limitations under the License.
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) import paddle
cost = paddle.layer.square_error_cost(input=y_predict, label=y) import paddle.fluid as fluid
import numpy
# Save the inference topology to protobuf.
inference_topology = paddle.topology.Topology(layers=y_predict) BATCH_SIZE = 20
with open("inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f) train_reader = paddle.batch(
paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500),
# create parameters batch_size=BATCH_SIZE)
parameters = paddle.parameters.create(cost)
test_reader = paddle.batch(
# create optimizer paddle.reader.shuffle(paddle.dataset.uci_housing.test(), buf_size=500),
optimizer = paddle.optimizer.Momentum(momentum=0) batch_size=BATCH_SIZE)
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=optimizer) def train_program():
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
feeding = {'x': 0, 'y': 1}
# feature vector of length 13
# event_handler to print training and testing info x = fluid.layers.data(name='x', shape=[13], dtype='float32')
def event_handler(event): y_predict = fluid.layers.fc(input=x, size=1, act=None)
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0: loss = fluid.layers.square_error_cost(input=y_predict, label=y)
print "Pass %d, Batch %d, Cost %f" % ( avg_loss = fluid.layers.mean(loss)
event.pass_id, event.batch_id, event.cost)
return avg_loss
if isinstance(event, paddle.event.EndPass):
if event.pass_id % 10 == 0:
with open('params_pass_%d.tar' % event.pass_id, 'w') as f: # can use CPU or GPU
trainer.save_parameter_to_tar(f) use_cuda = False
result = trainer.test( place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
reader=paddle.batch(uci_housing.test(), batch_size=2),
feeding=feeding) trainer = fluid.Trainer(
print "Test %d, Cost %f" % (event.pass_id, result.cost) train_func=train_program,
place=place,
# training optimizer=fluid.optimizer.SGD(learning_rate=0.001))
trainer.train(
reader=paddle.batch( feed_order = ['x', 'y']
paddle.reader.shuffle(uci_housing.train(), buf_size=500),
batch_size=2), # Specify the directory path to save the parameters
feeding=feeding, params_folder = "fit_a_line.inference.model"
event_handler=event_handler,
num_passes=30) # Plot data
from paddle.v2.plot import Ploter
# inference
test_data_creator = paddle.dataset.uci_housing.test() train_title = "Train cost"
test_data = [] test_title = "Test cost"
test_label = [] plot_cost = Ploter(train_title, test_title)
step = 0
for item in test_data_creator():
test_data.append((item[0], ))
test_label.append(item[1]) # event_handler to print training and testing info
if len(test_data) == 5: def event_handler(event):
break global step
if isinstance(event, fluid.EndStepEvent):
# load parameters from tar file. if step % 100 == 0: # every 100 batches, record a test cost
# users can remove the comments and change the model name test_metrics = trainer.test(
# with open('params_pass_20.tar', 'r') as f: reader=test_reader, feed_order=feed_order)
# parameters = paddle.parameters.Parameters.from_tar(f)
print(test_metrics[0])
probs = paddle.infer(
output_layer=y_predict, parameters=parameters, input=test_data) plot_cost.append(test_title, step, test_metrics[0])
plot_cost.plot()
for i in xrange(len(probs)):
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][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')
if __name__ == '__main__': trainer.stop()
main()
if step >= 2000:
# Or if it has been running for enough steps
print('has been running for 2000 steps, stop')
trainer.stop()
# We can save the trained parameters for the inferences later
if params_folder is not None:
trainer.save_params(params_folder)
step += 1
# The training could take up to a few minutes.
trainer.train(
reader=train_reader,
num_epochs=100,
event_handler=event_handler,
feed_order=feed_order)
def inference_program():
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
return y_predict
inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=params_folder, place=place)
batch_size = 10
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32")
results = inferencer.infer({'x': tensor_x})
print("infer results: ", results[0])
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册