Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
8af63bad
B
book
项目概览
PaddlePaddle
/
book
通知
17
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8af63bad
编写于
9月 14, 2017
作者:
F
fengjiayi
提交者:
GitHub
9月 14, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #395 from Canpio/fix
Update chapter 03 and Chapter 06
上级
bec4aeda
b25a435b
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
52 addition
and
37 deletion
+52
-37
03.image_classification/README.cn.md
03.image_classification/README.cn.md
+1
-0
03.image_classification/README.md
03.image_classification/README.md
+1
-0
03.image_classification/index.cn.html
03.image_classification/index.cn.html
+1
-0
03.image_classification/index.html
03.image_classification/index.html
+1
-0
03.image_classification/train.py
03.image_classification/train.py
+6
-0
06.understand_sentiment/README.cn.md
06.understand_sentiment/README.cn.md
+5
-4
06.understand_sentiment/README.md
06.understand_sentiment/README.md
+9
-8
06.understand_sentiment/index.cn.html
06.understand_sentiment/index.cn.html
+5
-4
06.understand_sentiment/index.html
06.understand_sentiment/index.html
+9
-8
06.understand_sentiment/train.py
06.understand_sentiment/train.py
+14
-13
未找到文件。
03.image_classification/README.cn.md
浏览文件 @
8af63bad
# 图像分类
本教程源代码目录在
[
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
)
。
...
...
03.image_classification/README.md
浏览文件 @
8af63bad
Image Classification
=======================
...
...
03.image_classification/index.cn.html
浏览文件 @
8af63bad
...
...
@@ -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)。
...
...
03.image_classification/index.html
浏览文件 @
8af63bad
...
...
@@ -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
=======================
...
...
03.image_classification/train.py
浏览文件 @
8af63bad
...
...
@@ -81,6 +81,12 @@ def main():
# 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
(
...
...
06.understand_sentiment/README.cn.md
浏览文件 @
8af63bad
...
...
@@ -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
]
...
...
@@ -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
):
...
...
@@ -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
())
...
...
@@ -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
"
\n
Test with Pass %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
```
...
...
06.understand_sentiment/README.md
浏览文件 @
8af63bad
...
...
@@ -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
...
...
@@ -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
]
...
...
@@ -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
):
...
...
@@ -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
(
...
...
@@ -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
...
...
@@ -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
...
...
@@ -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
"
\n
Test with Pass %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
```
...
...
06.understand_sentiment/index.cn.html
浏览文件 @
8af63bad
...
...
@@ -206,7 +206,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]
...
...
@@ -223,7 +222,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):
...
...
@@ -236,8 +235,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())
...
...
@@ -334,6 +332,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)
```
...
...
06.understand_sentiment/index.html
浏览文件 @
8af63bad
...
...
@@ -178,7 +178,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
...
...
@@ -217,7 +217,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]
...
...
@@ -234,7 +233,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):
...
...
@@ -247,8 +246,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(
...
...
@@ -263,7 +261,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
...
...
@@ -287,9 +285,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
...
...
@@ -353,6 +351,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)
```
...
...
06.understand_sentiment/train.py
浏览文件 @
8af63bad
...
...
@@ -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
,
...
...
@@ -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
]
...
...
@@ -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
):
...
...
@@ -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
(
...
...
@@ -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__'
:
...
...
@@ -123,8 +117,8 @@ if __name__ == '__main__':
# 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
)
...
...
@@ -145,15 +139,22 @@ if __name__ == '__main__':
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
"
\n
Test 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
=
2
0
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录