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

Commit

Permalink
Merge pull request #395 from Canpio/fix
Browse files Browse the repository at this point in the history
Update chapter 03 and Chapter 06
  • Loading branch information
JiayiFeng authored Sep 14, 2017
2 parents bec4aed + b25a435 commit 8af63ba
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 37 deletions.
1 change: 1 addition & 0 deletions 03.image_classification/README.cn.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# 图像分类

本教程源代码目录在[book/image_classification](https://github.com/PaddlePaddle/book/tree/develop/03.image_classification), 初次使用请参考PaddlePaddle[安装教程](https://github.com/PaddlePaddle/book/blob/develop/README.cn.md#运行这本书),更多内容请参考本教程的[视频课堂](http://bit.baidu.com/course/detail/id/168.html)
Expand Down
1 change: 1 addition & 0 deletions 03.image_classification/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Image Classification
=======================

Expand Down
1 change: 1 addition & 0 deletions 03.image_classification/index.cn.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

<!-- This block will be replaced by each markdown file content. Please do not change lines below.-->
<div id="markdown" style='display:none'>

# 图像分类

本教程源代码目录在[book/image_classification](https://github.com/PaddlePaddle/book/tree/develop/03.image_classification), 初次使用请参考PaddlePaddle[安装教程](https://github.com/PaddlePaddle/book/blob/develop/README.cn.md#运行这本书),更多内容请参考本教程的[视频课堂](http://bit.baidu.com/course/detail/id/168.html)。
Expand Down
1 change: 1 addition & 0 deletions 03.image_classification/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

<!-- This block will be replaced by each markdown file content. Please do not change lines below.-->
<div id="markdown" style='display:none'>

Image Classification
=======================

Expand Down
6 changes: 6 additions & 0 deletions 03.image_classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def event_handler(event):
# Create trainer
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=momentum_optimizer)

# Save the inference topology to protobuf.
inference_topology = paddle.topology.Topology(layers=out)
with open("inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f)

trainer.train(
reader=paddle.batch(
paddle.reader.shuffle(
Expand Down
9 changes: 5 additions & 4 deletions 06.understand_sentiment/README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def stacked_lstm_net(input_dim,
"""
assert stacked_num % 2 == 1

layer_attr = paddle.attr.Extra(drop_rate=0.5)
fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
para_attr = [fc_para_attr, lstm_para_attr]
Expand All @@ -181,7 +180,7 @@ def stacked_lstm_net(input_dim,
act=linear,
bias_attr=bias_attr)
lstm1 = paddle.layer.lstmemory(
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
input=fc1, act=relu, bias_attr=bias_attr)

inputs = [fc1, lstm1]
for i in range(2, stacked_num + 1):
Expand All @@ -194,8 +193,7 @@ def stacked_lstm_net(input_dim,
input=fc,
reverse=(i % 2) == 0,
act=relu,
bias_attr=bias_attr,
layer_attr=layer_attr)
bias_attr=bias_attr)
inputs = [fc, lstm]

fc_last = paddle.layer.pooling(input=inputs[0], pooling_type=paddle.pooling.Max())
Expand Down Expand Up @@ -292,6 +290,9 @@ Paddle中提供了一系列优化算法的API,这里使用Adam优化算法。
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
with open('./params_pass_%d.tar' % event.pass_id, 'w') as f:
parameters.to_tar(f)

result = trainer.test(reader=test_reader, feeding=feeding)
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
```
Expand Down
17 changes: 9 additions & 8 deletions 06.understand_sentiment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def convolution_net(input_dim, class_dim=2, emb_dim=128, hid_dim=128):
act=paddle.activation.Softmax())
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost
return cost, output
```

1. Define input data and its dimension
Expand Down Expand Up @@ -175,7 +175,6 @@ def stacked_lstm_net(input_dim,
"""
assert stacked_num % 2 == 1

layer_attr = paddle.attr.Extra(drop_rate=0.5)
fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
para_attr = [fc_para_attr, lstm_para_attr]
Expand All @@ -192,7 +191,7 @@ def stacked_lstm_net(input_dim,
act=linear,
bias_attr=bias_attr)
lstm1 = paddle.layer.lstmemory(
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
input=fc1, act=relu, bias_attr=bias_attr)

inputs = [fc1, lstm1]
for i in range(2, stacked_num + 1):
Expand All @@ -205,8 +204,7 @@ def stacked_lstm_net(input_dim,
input=fc,
reverse=(i % 2) == 0,
act=relu,
bias_attr=bias_attr,
layer_attr=layer_attr)
bias_attr=bias_attr)
inputs = [fc, lstm]

fc_last = paddle.layer.pooling(
Expand All @@ -221,7 +219,7 @@ def stacked_lstm_net(input_dim,

lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost
return cost, output
```

1. Define input data and its dimension
Expand All @@ -245,9 +243,9 @@ dict_dim = len(word_dict)
class_dim = 2

# option 1
cost = convolution_net(dict_dim, class_dim=class_dim)
[cost, output] = convolution_net(dict_dim, class_dim=class_dim)
# option 2
# cost = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
# [cost, output] = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
```

## Model Training
Expand Down Expand Up @@ -311,6 +309,9 @@ def event_handler(event):
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
with open('./params_pass_%d.tar' % event.pass_id, 'w') as f:
parameters.to_tar(f)

result = trainer.test(reader=test_reader, feeding=feeding)
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
```
Expand Down
9 changes: 5 additions & 4 deletions 06.understand_sentiment/index.cn.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@
"""
assert stacked_num % 2 == 1

layer_attr = paddle.attr.Extra(drop_rate=0.5)
fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
para_attr = [fc_para_attr, lstm_para_attr]
Expand All @@ -223,7 +222,7 @@
act=linear,
bias_attr=bias_attr)
lstm1 = paddle.layer.lstmemory(
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
input=fc1, act=relu, bias_attr=bias_attr)

inputs = [fc1, lstm1]
for i in range(2, stacked_num + 1):
Expand All @@ -236,8 +235,7 @@
input=fc,
reverse=(i % 2) == 0,
act=relu,
bias_attr=bias_attr,
layer_attr=layer_attr)
bias_attr=bias_attr)
inputs = [fc, lstm]

fc_last = paddle.layer.pooling(input=inputs[0], pooling_type=paddle.pooling.Max())
Expand Down Expand Up @@ -334,6 +332,9 @@
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
with open('./params_pass_%d.tar' % event.pass_id, 'w') as f:
parameters.to_tar(f)

result = trainer.test(reader=test_reader, feeding=feeding)
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
```
Expand Down
17 changes: 9 additions & 8 deletions 06.understand_sentiment/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
act=paddle.activation.Softmax())
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost
return cost, output
```

1. Define input data and its dimension
Expand Down Expand Up @@ -217,7 +217,6 @@
"""
assert stacked_num % 2 == 1

layer_attr = paddle.attr.Extra(drop_rate=0.5)
fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
para_attr = [fc_para_attr, lstm_para_attr]
Expand All @@ -234,7 +233,7 @@
act=linear,
bias_attr=bias_attr)
lstm1 = paddle.layer.lstmemory(
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
input=fc1, act=relu, bias_attr=bias_attr)

inputs = [fc1, lstm1]
for i in range(2, stacked_num + 1):
Expand All @@ -247,8 +246,7 @@
input=fc,
reverse=(i % 2) == 0,
act=relu,
bias_attr=bias_attr,
layer_attr=layer_attr)
bias_attr=bias_attr)
inputs = [fc, lstm]

fc_last = paddle.layer.pooling(
Expand All @@ -263,7 +261,7 @@

lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost
return cost, output
```

1. Define input data and its dimension
Expand All @@ -287,9 +285,9 @@
class_dim = 2

# option 1
cost = convolution_net(dict_dim, class_dim=class_dim)
[cost, output] = convolution_net(dict_dim, class_dim=class_dim)
# option 2
# cost = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
# [cost, output] = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
```

## Model Training
Expand Down Expand Up @@ -353,6 +351,9 @@
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
with open('./params_pass_%d.tar' % event.pass_id, 'w') as f:
parameters.to_tar(f)

result = trainer.test(reader=test_reader, feeding=feeding)
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
```
Expand Down
27 changes: 14 additions & 13 deletions 06.understand_sentiment/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def convolution_net(input_dim, class_dim=2, emb_dim=128, hid_dim=128):
input=[conv_3, conv_4], size=class_dim, act=paddle.activation.Softmax())
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost
return cost, output


def stacked_lstm_net(input_dim,
Expand All @@ -53,7 +53,6 @@ def stacked_lstm_net(input_dim,
"""
assert stacked_num % 2 == 1

layer_attr = paddle.attr.Extra(drop_rate=0.5)
fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
para_attr = [fc_para_attr, lstm_para_attr]
Expand All @@ -67,8 +66,7 @@ def stacked_lstm_net(input_dim,

fc1 = paddle.layer.fc(
input=emb, size=hid_dim, act=linear, bias_attr=bias_attr)
lstm1 = paddle.layer.lstmemory(
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
lstm1 = paddle.layer.lstmemory(input=fc1, act=relu, bias_attr=bias_attr)

inputs = [fc1, lstm1]
for i in range(2, stacked_num + 1):
Expand All @@ -79,11 +77,7 @@ def stacked_lstm_net(input_dim,
param_attr=para_attr,
bias_attr=bias_attr)
lstm = paddle.layer.lstmemory(
input=fc,
reverse=(i % 2) == 0,
act=relu,
bias_attr=bias_attr,
layer_attr=layer_attr)
input=fc, reverse=(i % 2) == 0, act=relu, bias_attr=bias_attr)
inputs = [fc, lstm]

fc_last = paddle.layer.pooling(
Expand All @@ -99,7 +93,7 @@ def stacked_lstm_net(input_dim,

lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost
return cost, output


if __name__ == '__main__':
Expand All @@ -123,8 +117,8 @@ def stacked_lstm_net(input_dim,
# network config
# Please choose the way to build the network
# by uncommenting the corresponding line.
cost = convolution_net(dict_dim, class_dim=class_dim)
# cost = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
[cost, output] = convolution_net(dict_dim, class_dim=class_dim)
# [cost, output] = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)

# create parameters
parameters = paddle.parameters.create(cost)
Expand All @@ -145,15 +139,22 @@ def event_handler(event):
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
with open('./params_pass_%d.tar' % event.pass_id, 'w') as f:
parameters.to_tar(f)

result = trainer.test(reader=test_reader, feeding=feeding)
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)

# create trainer
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=adam_optimizer)
# Save the inference topology to protobuf.
inference_topology = paddle.topology.Topology(layers=output)
with open("./inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f)

trainer.train(
reader=train_reader,
event_handler=event_handler,
feeding=feeding,
num_passes=2)
num_passes=20)

0 comments on commit 8af63ba

Please sign in to comment.