未验证 提交 e5a48432 编写于 作者: T Tao Luo 提交者: GitHub

Merge pull request #648 from junjun315/06-stuff

update to low level api--06 understand sentiment,test=develop
...@@ -110,24 +110,16 @@ Paddle在`dataset/imdb.py`中提实现了imdb数据集的自动下载和读取 ...@@ -110,24 +110,16 @@ Paddle在`dataset/imdb.py`中提实现了imdb数据集的自动下载和读取
from __future__ import print_function from __future__ import print_function
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from functools import partial
import numpy as np import numpy as np
try: import sys
from paddle.fluid.contrib.trainer import * import math
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 *
CLASS_DIM = 2 CLASS_DIM = 2
EMB_DIM = 128 EMB_DIM = 128
HID_DIM = 512 HID_DIM = 512
STACKED_NUM = 3 STACKED_NUM = 3
BATCH_SIZE = 128 BATCH_SIZE = 128
USE_GPU = False
``` ```
...@@ -212,8 +204,7 @@ def inference_program(word_dict): ...@@ -212,8 +204,7 @@ def inference_program(word_dict):
在测试过程中,分类器会计算各个输出的概率。第一个返回的数值规定为 损耗(cost)。 在测试过程中,分类器会计算各个输出的概率。第一个返回的数值规定为 损耗(cost)。
```python ```python
def train_program(word_dict): def train_program(prediction):
prediction = inference_program(word_dict)
label = fluid.layers.data(name="label", shape=[1], dtype="int64") label = fluid.layers.data(name="label", shape=[1], dtype="int64")
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(cost) avg_cost = fluid.layers.mean(cost)
...@@ -258,59 +249,77 @@ train_reader = paddle.batch( ...@@ -258,59 +249,77 @@ train_reader = paddle.batch(
训练器需要一个训练程序和一个训练优化函数。 训练器需要一个训练程序和一个训练优化函数。
```python ```python
trainer = Trainer( exe = fluid.Executor(place)
train_func=partial(train_program, word_dict), prediction = inference_program(word_dict)
place=place, [avg_cost, accuracy] = train_program(prediction)
optimizer_func=optimizer_func) sgd_optimizer = optimizer_func()
sgd_optimizer.minimize(avg_cost)
``` ```
### 提供数据 ### 提供数据并构建主训练循环
`feed_order`用来定义每条产生的数据和`paddle.layer.data`之间的映射关系。比如,`imdb.train`产生的第一列的数据对应的是`words`这个特征。 `feed_order`用来定义每条产生的数据和`paddle.layer.data`之间的映射关系。比如,`imdb.train`产生的第一列的数据对应的是`words`这个特征。
```python
feed_order = ['words', 'label']
```
### 事件处理器
回调函数event_handler在一个之前定义好的事件发生后会被调用。例如,我们可以在每步训练结束后查看误差。
```python ```python
# Specify the directory path to save the parameters # Specify the directory path to save the parameters
params_dirname = "understand_sentiment_conv.inference.model" params_dirname = "understand_sentiment_conv.inference.model"
def event_handler(event): feed_order = ['words', 'label']
if isinstance(event, EndStepEvent): pass_num = 1
print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch, list(map(np.array, event.metrics)))) def train_loop(main_program):
exe.run(fluid.default_startup_program())
if event.step == 10: feed_var_list_loop = [
trainer.save_params(params_dirname) main_program.global_block().var(var_name) for var_name in feed_order
trainer.stop() ]
feeder = fluid.DataFeeder(
feed_list=feed_var_list_loop, place=place)
test_program = fluid.default_main_program().clone(for_test=True)
for epoch_id in range(pass_num):
for step_id, data in enumerate(train_reader()):
metrics = exe.run(main_program,
feed=feeder.feed(data),
fetch_list=[avg_cost, accuracy])
avg_cost_test, acc_test = train_test(test_program, test_reader)
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
step_id, avg_cost_test, acc_test))
print("Step {0}, Epoch {1} Metrics {2}".format(
step_id, epoch_id, list(map(np.array,
metrics))))
if step_id == 30:
if params_dirname is not None:
fluid.io.save_inference_model(params_dirname, ["words"],
prediction, exe)
return
``` ```
### 训练过程处理
我们在训练主循环里打印了每一步输出,可以观察训练情况。
### 开始训练 ### 开始训练
最后,我们传入训练循环数(num_epoch)和一些别的参数,调用 trainer.train 来开始训练 最后,我们启动训练主循环来开始训练。训练时间较长,如果为了更快的返回结果,可以通过调整损耗值范围或者训练步数,以减少准确率的代价来缩短训练时间
```python ```python
trainer.train( train_loop(fluid.default_main_program())
num_epochs=1,
event_handler=event_handler,
reader=train_reader,
feed_order=feed_order)
``` ```
## 应用模型 ## 应用模型
### 构建预测器 ### 构建预测器
传入`inference_program``params_dirname`来初始化一个预测器, `params_dirname`用来存放训练过程中的各个参数。 和训练过程一样,我们需要创建一个预测过程,并使用训练得到的模型和参数来进行预测,`params_dirname`用来存放训练过程中的各个参数。
```python ```python
inferencer = Inferencer( place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
infer_func=partial(inference_program, word_dict), param_path=params_dirname, place=place) exe = fluid.Executor(place)
inference_scope = fluid.core.Scope()
``` ```
### 生成测试用输入数据 ### 生成测试用输入数据
...@@ -334,15 +343,25 @@ base_shape = [[len(c) for c in lod]] ...@@ -334,15 +343,25 @@ base_shape = [[len(c) for c in lod]]
tensor_words = fluid.create_lod_tensor(lod, base_shape, place) tensor_words = fluid.create_lod_tensor(lod, base_shape, place)
``` ```
## 应用模型 ## 应用模型并进行预测
现在我们可以对每一条评论进行正面或者负面的预测啦。 现在我们可以对每一条评论进行正面或者负面的预测啦。
```python ```python
results = inferencer.infer({'words': tensor_words}) with fluid.scope_guard(inference_scope):
for i, r in enumerate(results[0]): [inferencer, feed_target_names,
print("Predict probability of ", r[0], " to be positive and ", r[1], " to be negative for review \'", reviews_str[i], "\'") fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
assert feed_target_names[0] == "words"
results = exe.run(inference_program,
feed={feed_target_names[0]: tensor_words},
fetch_list=fetch_targets,
return_numpy=False)
np_data = np.array(results[0])
for i, r in enumerate(np_data):
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'")
``` ```
......
...@@ -152,24 +152,16 @@ Paddle在`dataset/imdb.py`中提实现了imdb数据集的自动下载和读取 ...@@ -152,24 +152,16 @@ Paddle在`dataset/imdb.py`中提实现了imdb数据集的自动下载和读取
from __future__ import print_function from __future__ import print_function
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from functools import partial
import numpy as np import numpy as np
try: import sys
from paddle.fluid.contrib.trainer import * import math
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 *
CLASS_DIM = 2 CLASS_DIM = 2
EMB_DIM = 128 EMB_DIM = 128
HID_DIM = 512 HID_DIM = 512
STACKED_NUM = 3 STACKED_NUM = 3
BATCH_SIZE = 128 BATCH_SIZE = 128
USE_GPU = False
``` ```
...@@ -254,8 +246,7 @@ def inference_program(word_dict): ...@@ -254,8 +246,7 @@ def inference_program(word_dict):
在测试过程中,分类器会计算各个输出的概率。第一个返回的数值规定为 损耗(cost)。 在测试过程中,分类器会计算各个输出的概率。第一个返回的数值规定为 损耗(cost)。
```python ```python
def train_program(word_dict): def train_program(prediction):
prediction = inference_program(word_dict)
label = fluid.layers.data(name="label", shape=[1], dtype="int64") label = fluid.layers.data(name="label", shape=[1], dtype="int64")
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(cost) avg_cost = fluid.layers.mean(cost)
...@@ -300,59 +291,77 @@ train_reader = paddle.batch( ...@@ -300,59 +291,77 @@ train_reader = paddle.batch(
训练器需要一个训练程序和一个训练优化函数。 训练器需要一个训练程序和一个训练优化函数。
```python ```python
trainer = Trainer( exe = fluid.Executor(place)
train_func=partial(train_program, word_dict), prediction = inference_program(word_dict)
place=place, [avg_cost, accuracy] = train_program(prediction)
optimizer_func=optimizer_func) sgd_optimizer = optimizer_func()
sgd_optimizer.minimize(avg_cost)
``` ```
### 提供数据 ### 提供数据并构建主训练循环
`feed_order`用来定义每条产生的数据和`paddle.layer.data`之间的映射关系。比如,`imdb.train`产生的第一列的数据对应的是`words`这个特征。 `feed_order`用来定义每条产生的数据和`paddle.layer.data`之间的映射关系。比如,`imdb.train`产生的第一列的数据对应的是`words`这个特征。
```python
feed_order = ['words', 'label']
```
### 事件处理器
回调函数event_handler在一个之前定义好的事件发生后会被调用。例如,我们可以在每步训练结束后查看误差。
```python ```python
# Specify the directory path to save the parameters # Specify the directory path to save the parameters
params_dirname = "understand_sentiment_conv.inference.model" params_dirname = "understand_sentiment_conv.inference.model"
def event_handler(event): feed_order = ['words', 'label']
if isinstance(event, EndStepEvent): pass_num = 1
print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch, list(map(np.array, event.metrics)))) def train_loop(main_program):
exe.run(fluid.default_startup_program())
if event.step == 10: feed_var_list_loop = [
trainer.save_params(params_dirname) main_program.global_block().var(var_name) for var_name in feed_order
trainer.stop() ]
feeder = fluid.DataFeeder(
feed_list=feed_var_list_loop, place=place)
test_program = fluid.default_main_program().clone(for_test=True)
for epoch_id in range(pass_num):
for step_id, data in enumerate(train_reader()):
metrics = exe.run(main_program,
feed=feeder.feed(data),
fetch_list=[avg_cost, accuracy])
avg_cost_test, acc_test = train_test(test_program, test_reader)
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
step_id, avg_cost_test, acc_test))
print("Step {0}, Epoch {1} Metrics {2}".format(
step_id, epoch_id, list(map(np.array,
metrics))))
if step_id == 30:
if params_dirname is not None:
fluid.io.save_inference_model(params_dirname, ["words"],
prediction, exe)
return
``` ```
### 训练过程处理
我们在训练主循环里打印了每一步输出,可以观察训练情况。
### 开始训练 ### 开始训练
最后,我们传入训练循环数(num_epoch)和一些别的参数,调用 trainer.train 来开始训练 最后,我们启动训练主循环来开始训练。训练时间较长,如果为了更快的返回结果,可以通过调整损耗值范围或者训练步数,以减少准确率的代价来缩短训练时间
```python ```python
trainer.train( train_loop(fluid.default_main_program())
num_epochs=1,
event_handler=event_handler,
reader=train_reader,
feed_order=feed_order)
``` ```
## 应用模型 ## 应用模型
### 构建预测器 ### 构建预测器
传入`inference_program`和`params_dirname`来初始化一个预测器, `params_dirname`用来存放训练过程中的各个参数。 和训练过程一样,我们需要创建一个预测过程,并使用训练得到的模型和参数来进行预测,`params_dirname`用来存放训练过程中的各个参数。
```python ```python
inferencer = Inferencer( place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
infer_func=partial(inference_program, word_dict), param_path=params_dirname, place=place) exe = fluid.Executor(place)
inference_scope = fluid.core.Scope()
``` ```
### 生成测试用输入数据 ### 生成测试用输入数据
...@@ -376,15 +385,25 @@ base_shape = [[len(c) for c in lod]] ...@@ -376,15 +385,25 @@ base_shape = [[len(c) for c in lod]]
tensor_words = fluid.create_lod_tensor(lod, base_shape, place) tensor_words = fluid.create_lod_tensor(lod, base_shape, place)
``` ```
## 应用模型 ## 应用模型并进行预测
现在我们可以对每一条评论进行正面或者负面的预测啦。 现在我们可以对每一条评论进行正面或者负面的预测啦。
```python ```python
results = inferencer.infer({'words': tensor_words}) with fluid.scope_guard(inference_scope):
for i, r in enumerate(results[0]): [inferencer, feed_target_names,
print("Predict probability of ", r[0], " to be positive and ", r[1], " to be negative for review \'", reviews_str[i], "\'") fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
assert feed_target_names[0] == "words"
results = exe.run(inference_program,
feed={feed_target_names[0]: tensor_words},
fetch_list=fetch_targets,
return_numpy=False)
np_data = np.array(results[0])
for i, r in enumerate(np_data):
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'")
``` ```
......
...@@ -14,22 +14,11 @@ ...@@ -14,22 +14,11 @@
from __future__ import print_function from __future__ import print_function
import os
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from functools import partial
import numpy as np import numpy as np
import sys import sys
import math
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 *
CLASS_DIM = 2 CLASS_DIM = 2
EMB_DIM = 128 EMB_DIM = 128
...@@ -66,8 +55,7 @@ def inference_program(word_dict): ...@@ -66,8 +55,7 @@ def inference_program(word_dict):
return net return net
def train_program(word_dict): def train_program(prediction):
prediction = inference_program(word_dict)
label = fluid.layers.data(name="label", shape=[1], dtype="int64") label = fluid.layers.data(name="label", shape=[1], dtype="int64")
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(cost) avg_cost = fluid.layers.mean(cost)
...@@ -79,8 +67,9 @@ def optimizer_func(): ...@@ -79,8 +67,9 @@ def optimizer_func():
return fluid.optimizer.Adagrad(learning_rate=0.002) return fluid.optimizer.Adagrad(learning_rate=0.002)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
print("Loading IMDB word dict....") print("Loading IMDB word dict....")
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
...@@ -94,83 +83,131 @@ def train(use_cuda, train_program, params_dirname): ...@@ -94,83 +83,131 @@ def train(use_cuda, train_program, params_dirname):
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.dataset.imdb.test(word_dict), batch_size=BATCH_SIZE) paddle.dataset.imdb.test(word_dict), batch_size=BATCH_SIZE)
trainer = Trainer(
train_func=partial(train_program, word_dict),
place=place,
optimizer_func=optimizer_func)
feed_order = ['words', 'label'] feed_order = ['words', 'label']
pass_num = 1
def event_handler(event): main_program = fluid.default_main_program()
if isinstance(event, EndStepEvent): star_program = fluid.default_startup_program()
if event.step % 10 == 0: prediction = inference_program(word_dict)
avg_cost, acc = trainer.test( train_func_outputs = train_program(prediction)
reader=test_reader, feed_order=feed_order) avg_cost = train_func_outputs[0]
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( test_program = main_program.clone(for_test=True)
event.step, avg_cost, acc))
# [avg_cost, accuracy] = train_program(prediction)
print("Step {0}, Epoch {1} Metrics {2}".format( sgd_optimizer = optimizer_func()
event.step, event.epoch, list(map(np.array, sgd_optimizer.minimize(avg_cost)
event.metrics)))) exe = fluid.Executor(place)
elif isinstance(event, EndEpochEvent): def train_test(program, reader):
trainer.save_params(params_dirname) count = 0
feed_var_list = [
trainer.train( program.global_block().var(var_name) for var_name in feed_order
num_epochs=1, ]
event_handler=event_handler, feeder_test = fluid.DataFeeder(feed_list=feed_var_list, place=place)
reader=train_reader, test_exe = fluid.Executor(place)
feed_order=feed_order) accumulated = len(train_func_outputs) * [0]
for test_data in reader():
avg_cost_np = test_exe.run(
def infer(use_cuda, inference_program, params_dirname=None): program=program,
feed=feeder_test.feed(test_data),
fetch_list=train_func_outputs)
accumulated = [
x[0] + x[1][0] for x in zip(accumulated, avg_cost_np)
]
count += 1
return [x / count for x in accumulated]
def train_loop():
feed_var_list_loop = [
main_program.global_block().var(var_name) for var_name in feed_order
]
feeder = fluid.DataFeeder(feed_list=feed_var_list_loop, place=place)
exe.run(star_program)
for epoch_id in range(pass_num):
for step_id, data in enumerate(train_reader()):
metrics = exe.run(
main_program,
feed=feeder.feed(data),
fetch_list=[var.name for var in train_func_outputs])
print("step: {0}, Metrics {1}".format(
step_id, list(map(np.array, metrics))))
if (step_id + 1) % 10 == 0:
avg_cost_test, acc_test = train_test(test_program,
test_reader)
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
step_id, avg_cost_test, acc_test))
print("Step {0}, Epoch {1} Metrics {2}".format(
step_id, epoch_id, list(map(np.array, metrics))))
if math.isnan(float(metrics[0])):
sys.exit("got NaN loss, training failed.")
if params_dirname is not None:
fluid.io.save_inference_model(params_dirname, ["words"],
prediction, exe)
train_loop()
def infer(use_cuda, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
inferencer = Inferencer( exe = fluid.Executor(place)
infer_func=partial(inference_program, word_dict),
param_path=params_dirname, inference_scope = fluid.core.Scope()
place=place) with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc,
# Setup input by creating LoDTensor to represent sequence of words. # the feed_target_names (the names of variables that will be feeded
# Here each word is the basic element of the LoDTensor and the shape of # data using feed operators), and the fetch_targets (variables that
# each word (base_shape) should be [1] since it is simply an index to # we want to obtain data from using fetch operators).
# look up for the corresponding word vector. [inferencer, feed_target_names,
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]], fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
# which has only one lod level. Then the created LoDTensor will have only
# one higher level structure (sequence of words, or sentence) than the basic # Setup input by creating LoDTensor to represent sequence of words.
# element (word). Hence the LoDTensor will hold data for three sentences of # Here each word is the basic element of the LoDTensor and the shape of
# length 3, 4 and 2, respectively. # each word (base_shape) should be [1] since it is simply an index to
# Note that lod info should be a list of lists. # look up for the corresponding word vector.
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]],
reviews_str = [ # which has only one lod level. Then the created LoDTensor will have only
'read the book forget the movie', 'this is a great movie', # one higher level structure (sequence of words, or sentence) than the basic
'this is very bad' # element (word). Hence the LoDTensor will hold data for three sentences of
] # length 3, 4 and 2, respectively.
reviews = [c.split() for c in reviews_str] # Note that lod info should be a list of lists.
reviews_str = [
UNK = word_dict['<unk>'] 'read the book forget the movie', 'this is a great movie',
lod = [] 'this is very bad'
for c in reviews: ]
lod.append([word_dict.get(words, UNK) for words in c]) reviews = [c.split() for c in reviews_str]
base_shape = [[len(c) for c in lod]] UNK = word_dict['<unk>']
lod = []
tensor_words = fluid.create_lod_tensor(lod, base_shape, place) for c in reviews:
results = inferencer.infer({'words': tensor_words}) lod.append([word_dict.get(words, UNK) for words in c])
for i, r in enumerate(results[0]): base_shape = [[len(c) for c in lod]]
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'") tensor_words = fluid.create_lod_tensor(lod, base_shape, place)
assert feed_target_names[0] == "words"
results = exe.run(
inferencer,
feed={feed_target_names[0]: tensor_words},
fetch_list=fetch_targets,
return_numpy=False)
np_data = np.array(results[0])
for i, r in enumerate(np_data):
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'")
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
params_dirname = "understand_sentiment_conv.inference.model" params_dirname = "understand_sentiment_conv.inference.model"
train(use_cuda, train_program, params_dirname) train(use_cuda, params_dirname)
infer(use_cuda, inference_program, params_dirname) infer(use_cuda, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -14,28 +14,16 @@ ...@@ -14,28 +14,16 @@
from __future__ import print_function from __future__ import print_function
import os
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from functools import partial
import numpy as np import numpy as np
import sys import sys
import math
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 *
CLASS_DIM = 2 CLASS_DIM = 2
EMB_DIM = 128 EMB_DIM = 128
BATCH_SIZE = 128 BATCH_SIZE = 128
LSTM_SIZE = 128 LSTM_SIZE = 128
USE_GPU = False
def dynamic_rnn_lstm(data, input_dim, class_dim, emb_dim, lstm_size): def dynamic_rnn_lstm(data, input_dim, class_dim, emb_dim, lstm_size):
...@@ -83,8 +71,7 @@ def inference_program(word_dict): ...@@ -83,8 +71,7 @@ def inference_program(word_dict):
return pred return pred
def train_program(word_dict): def train_program(prediction):
prediction = inference_program(word_dict)
label = fluid.layers.data(name="label", shape=[1], dtype="int64") label = fluid.layers.data(name="label", shape=[1], dtype="int64")
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(cost) avg_cost = fluid.layers.mean(cost)
...@@ -96,7 +83,7 @@ def optimizer_func(): ...@@ -96,7 +83,7 @@ def optimizer_func():
return fluid.optimizer.Adagrad(learning_rate=0.002) return fluid.optimizer.Adagrad(learning_rate=0.002)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
print("Loading IMDB word dict....") print("Loading IMDB word dict....")
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
...@@ -111,83 +98,128 @@ def train(use_cuda, train_program, params_dirname): ...@@ -111,83 +98,128 @@ def train(use_cuda, train_program, params_dirname):
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.dataset.imdb.test(word_dict), batch_size=BATCH_SIZE) paddle.dataset.imdb.test(word_dict), batch_size=BATCH_SIZE)
trainer = Trainer(
train_func=partial(train_program, word_dict),
place=place,
optimizer_func=optimizer_func)
feed_order = ['words', 'label'] feed_order = ['words', 'label']
pass_num = 1
def event_handler(event): main_program = fluid.default_main_program()
if isinstance(event, EndStepEvent): star_program = fluid.default_startup_program()
if event.step % 10 == 0: prediction = inference_program(word_dict)
avg_cost, acc = trainer.test( train_func_outputs = train_program(prediction)
reader=test_reader, feed_order=feed_order) avg_cost = train_func_outputs[0]
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( test_program = main_program.clone(for_test=True)
event.step, avg_cost, acc))
sgd_optimizer = optimizer_func()
print("Step {0}, Epoch {1} Metrics {2}".format( sgd_optimizer.minimize(avg_cost)
event.step, event.epoch, list(map(np.array, exe = fluid.Executor(place)
event.metrics))))
def train_test(program, reader):
elif isinstance(event, EndEpochEvent): count = 0
trainer.save_params(params_dirname) feed_var_list = [
program.global_block().var(var_name) for var_name in feed_order
trainer.train( ]
num_epochs=1, feeder_test = fluid.DataFeeder(feed_list=feed_var_list, place=place)
event_handler=event_handler, test_exe = fluid.Executor(place)
reader=train_reader, accumulated = len(train_func_outputs) * [0]
feed_order=feed_order) for test_data in reader():
avg_cost_np = test_exe.run(
program=program,
def infer(use_cuda, inference_program, params_dirname=None): feed=feeder_test.feed(test_data),
fetch_list=train_func_outputs)
accumulated = [
x[0] + x[1][0] for x in zip(accumulated, avg_cost_np)
]
count += 1
return [x / count for x in accumulated]
def train_loop():
feed_var_list_loop = [
main_program.global_block().var(var_name) for var_name in feed_order
]
feeder = fluid.DataFeeder(feed_list=feed_var_list_loop, place=place)
exe.run(fluid.default_startup_program())
for epoch_id in range(pass_num):
for step_id, data in enumerate(train_reader()):
metrics = exe.run(
main_program,
feed=feeder.feed(data),
fetch_list=[var.name for var in train_func_outputs])
if (step_id + 1) % 10 == 0:
#avg_cost_test, acc_test = train_test(test_program, test_reader)
#print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
# step_id, avg_cost_test, acc_test))
print("Step {0}, Epoch {1} Metrics {2}".format(
step_id, epoch_id, list(map(np.array, metrics))))
if math.isnan(float(metrics[0])):
sys.exit("got NaN loss, training failed.")
if params_dirname is not None:
fluid.io.save_inference_model(params_dirname, ["words"],
prediction, exe)
train_loop()
def infer(use_cuda, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
inferencer = Inferencer( exe = fluid.Executor(place)
infer_func=partial(inference_program, word_dict),
param_path=params_dirname, inference_scope = fluid.core.Scope()
place=place) with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc,
# Setup input by creating LoDTensor to represent sequence of words. # the feed_target_names (the names of variables that will be feeded
# Here each word is the basic element of the LoDTensor and the shape of # data using feed operators), and the fetch_targets (variables that
# each word (base_shape) should be [1] since it is simply an index to # we want to obtain data from using fetch operators).
# look up for the corresponding word vector. [inferencer, feed_target_names,
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]], fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
# which has only one lod level. Then the created LoDTensor will have only
# one higher level structure (sequence of words, or sentence) than the basic # Setup input by creating LoDTensor to represent sequence of words.
# element (word). Hence the LoDTensor will hold data for three sentences of # Here each word is the basic element of the LoDTensor and the shape of
# length 3, 4 and 2, respectively. # each word (base_shape) should be [1] since it is simply an index to
# Note that lod info should be a list of lists. # look up for the corresponding word vector.
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]],
reviews_str = [ # which has only one lod level. Then the created LoDTensor will have only
'read the book forget the movie', 'this is a great movie', # one higher level structure (sequence of words, or sentence) than the basic
'this is very bad' # element (word). Hence the LoDTensor will hold data for three sentences of
] # length 3, 4 and 2, respectively.
reviews = [c.split() for c in reviews_str] # Note that lod info should be a list of lists.
reviews_str = [
UNK = word_dict['<unk>'] 'read the book forget the movie', 'this is a great movie',
lod = [] 'this is very bad'
for c in reviews: ]
lod.append([word_dict.get(words, UNK) for words in c]) reviews = [c.split() for c in reviews_str]
base_shape = [[len(c) for c in lod]] UNK = word_dict['<unk>']
lod = []
tensor_words = fluid.create_lod_tensor(lod, base_shape, place) for c in reviews:
results = inferencer.infer({'words': tensor_words}) lod.append([word_dict.get(words, UNK) for words in c])
for i, r in enumerate(results[0]): base_shape = [[len(c) for c in lod]]
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'") tensor_words = fluid.create_lod_tensor(lod, base_shape, place)
assert feed_target_names[0] == "words"
results = exe.run(
inferencer,
feed={feed_target_names[0]: tensor_words},
fetch_list=fetch_targets,
return_numpy=False)
np_data = np.array(results[0])
for i, r in enumerate(np_data):
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'")
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
params_dirname = "understand_sentiment_conv.inference.model" params_dirname = "understand_sentiment_conv.inference.model"
train(use_cuda, train_program, params_dirname) train(use_cuda, params_dirname)
infer(use_cuda, inference_program, params_dirname) infer(use_cuda, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -17,19 +17,9 @@ from __future__ import print_function ...@@ -17,19 +17,9 @@ from __future__ import print_function
import os import os
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from functools import partial
import numpy as np import numpy as np
import sys import sys
import math
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 *
CLASS_DIM = 2 CLASS_DIM = 2
EMB_DIM = 128 EMB_DIM = 128
...@@ -74,8 +64,8 @@ def inference_program(word_dict): ...@@ -74,8 +64,8 @@ def inference_program(word_dict):
return net return net
def train_program(word_dict): def train_program(prediction):
prediction = inference_program(word_dict) # prediction = inference_program(word_dict)
label = fluid.layers.data(name="label", shape=[1], dtype="int64") label = fluid.layers.data(name="label", shape=[1], dtype="int64")
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(cost) avg_cost = fluid.layers.mean(cost)
...@@ -87,8 +77,9 @@ def optimizer_func(): ...@@ -87,8 +77,9 @@ def optimizer_func():
return fluid.optimizer.Adagrad(learning_rate=0.002) return fluid.optimizer.Adagrad(learning_rate=0.002)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
print("Loading IMDB word dict....") print("Loading IMDB word dict....")
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
...@@ -102,83 +93,131 @@ def train(use_cuda, train_program, params_dirname): ...@@ -102,83 +93,131 @@ def train(use_cuda, train_program, params_dirname):
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.dataset.imdb.test(word_dict), batch_size=BATCH_SIZE) paddle.dataset.imdb.test(word_dict), batch_size=BATCH_SIZE)
trainer = Trainer(
train_func=partial(train_program, word_dict),
place=place,
optimizer_func=optimizer_func)
feed_order = ['words', 'label'] feed_order = ['words', 'label']
pass_num = 1
def event_handler(event): main_program = fluid.default_main_program()
if isinstance(event, EndStepEvent): star_program = fluid.default_startup_program()
if event.step % 10 == 0: prediction = inference_program(word_dict)
avg_cost, acc = trainer.test( train_func_outputs = train_program(prediction)
reader=test_reader, feed_order=feed_order) avg_cost = train_func_outputs[0]
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( test_program = main_program.clone(for_test=True)
event.step, avg_cost, acc))
# [avg_cost, accuracy] = train_program(prediction)
print("Step {0}, Epoch {1} Metrics {2}".format( sgd_optimizer = optimizer_func()
event.step, event.epoch, list(map(np.array, sgd_optimizer.minimize(avg_cost)
event.metrics)))) exe = fluid.Executor(place)
elif isinstance(event, EndEpochEvent): def train_test(program, reader):
trainer.save_params(params_dirname) count = 0
feed_var_list = [
trainer.train( program.global_block().var(var_name) for var_name in feed_order
num_epochs=1, ]
event_handler=event_handler, feeder_test = fluid.DataFeeder(feed_list=feed_var_list, place=place)
reader=train_reader, test_exe = fluid.Executor(place)
feed_order=feed_order) accumulated = len(train_func_outputs) * [0]
for test_data in reader():
avg_cost_np = test_exe.run(
def infer(use_cuda, inference_program, params_dirname=None): program=program,
feed=feeder_test.feed(test_data),
fetch_list=train_func_outputs)
accumulated = [
x[0] + x[1][0] for x in zip(accumulated, avg_cost_np)
]
count += 1
return [x / count for x in accumulated]
def train_loop():
feed_var_list_loop = [
main_program.global_block().var(var_name) for var_name in feed_order
]
feeder = fluid.DataFeeder(feed_list=feed_var_list_loop, place=place)
exe.run(fluid.default_startup_program())
for epoch_id in range(pass_num):
for step_id, data in enumerate(train_reader()):
metrics = exe.run(
main_program,
feed=feeder.feed(data),
fetch_list=[var.name for var in train_func_outputs])
print("step: {0}, Metrics {1}".format(
step_id, list(map(np.array, metrics))))
if (step_id + 1) % 10 == 0:
avg_cost_test, acc_test = train_test(test_program,
test_reader)
print('Step {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
step_id, avg_cost_test, acc_test))
print("Step {0}, Epoch {1} Metrics {2}".format(
step_id, epoch_id, list(map(np.array, metrics))))
if math.isnan(float(metrics[0])):
sys.exit("got NaN loss, training failed.")
if params_dirname is not None:
fluid.io.save_inference_model(params_dirname, ["words"],
prediction, exe)
train_loop()
def infer(use_cuda, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
inferencer = Inferencer( exe = fluid.Executor(place)
infer_func=partial(inference_program, word_dict),
param_path=params_dirname, inference_scope = fluid.core.Scope()
place=place) with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc,
# Setup input by creating LoDTensor to represent sequence of words. # the feed_target_names (the names of variables that will be feeded
# Here each word is the basic element of the LoDTensor and the shape of # data using feed operators), and the fetch_targets (variables that
# each word (base_shape) should be [1] since it is simply an index to # we want to obtain data from using fetch operators).
# look up for the corresponding word vector. [inferencer, feed_target_names,
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]], fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
# which has only one lod level. Then the created LoDTensor will have only
# one higher level structure (sequence of words, or sentence) than the basic # Setup input by creating LoDTensor to represent sequence of words.
# element (word). Hence the LoDTensor will hold data for three sentences of # Here each word is the basic element of the LoDTensor and the shape of
# length 3, 4 and 2, respectively. # each word (base_shape) should be [1] since it is simply an index to
# Note that lod info should be a list of lists. # look up for the corresponding word vector.
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]],
reviews_str = [ # which has only one lod level. Then the created LoDTensor will have only
'read the book forget the movie', 'this is a great movie', # one higher level structure (sequence of words, or sentence) than the basic
'this is very bad' # element (word). Hence the LoDTensor will hold data for three sentences of
] # length 3, 4 and 2, respectively.
reviews = [c.split() for c in reviews_str] # Note that lod info should be a list of lists.
reviews_str = [
UNK = word_dict['<unk>'] 'read the book forget the movie', 'this is a great movie',
lod = [] 'this is very bad'
for c in reviews: ]
lod.append([word_dict.get(words, UNK) for words in c]) reviews = [c.split() for c in reviews_str]
base_shape = [[len(c) for c in lod]] UNK = word_dict['<unk>']
lod = []
tensor_words = fluid.create_lod_tensor(lod, base_shape, place) for c in reviews:
results = inferencer.infer({'words': tensor_words}) lod.append([word_dict.get(words, UNK) for words in c])
for i, r in enumerate(results[0]): base_shape = [[len(c) for c in lod]]
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'") tensor_words = fluid.create_lod_tensor(lod, base_shape, place)
assert feed_target_names[0] == "words"
results = exe.run(
inferencer,
feed={feed_target_names[0]: tensor_words},
fetch_list=fetch_targets,
return_numpy=False)
np_data = np.array(results[0])
for i, r in enumerate(np_data):
print("Predict probability of ", r[0], " to be positive and ", r[1],
" to be negative for review \'", reviews_str[i], "\'")
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
params_dirname = "understand_sentiment_stacked_lstm.inference.model" params_dirname = "understand_sentiment_stacked_lstm.inference.model"
train(use_cuda, train_program, params_dirname) train(use_cuda, params_dirname)
infer(use_cuda, inference_program, params_dirname) infer(use_cuda, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册