Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0a33f170
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0a33f170
编写于
3月 01, 2017
作者:
H
hedaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add stacked lstm network
上级
803da664
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
72 addition
and
2 deletion
+72
-2
demo/sentiment/train_v2.py
demo/sentiment/train_v2.py
+72
-2
未找到文件。
demo/sentiment/train_v2.py
浏览文件 @
0a33f170
from
os.path
import
join
as
join_path
import
paddle.trainer_config_helpers.attrs
as
attrs
from
paddle.trainer_config_helpers.poolings
import
MaxPooling
import
paddle.v2
as
paddle
import
paddle.v2.layer
as
layer
import
paddle.v2.activation
as
activation
...
...
@@ -115,7 +117,73 @@ def convolution_net(input_dim,
output
=
layer
.
fc
(
input
=
[
conv_3
,
conv_4
],
size
=
class_dim
,
act
=
activation
.
Softmax
())
lbl
=
layer
.
data
(
"label"
,
data_type
.
integer_value
(
1
))
lbl
=
layer
.
data
(
"label"
,
data_type
.
integer_value
(
2
))
cost
=
layer
.
classification_cost
(
input
=
output
,
label
=
lbl
)
return
cost
def
stacked_lstm_net
(
input_dim
,
class_dim
=
2
,
emb_dim
=
128
,
hid_dim
=
512
,
stacked_num
=
3
,
is_predict
=
False
):
"""
A Wrapper for sentiment classification task.
This network uses bi-directional recurrent network,
consisting three LSTM layers. This configure is referred to
the paper as following url, but use fewer layrs.
http://www.aclweb.org/anthology/P15-1109
input_dim: here is word dictionary dimension.
class_dim: number of categories.
emb_dim: dimension of word embedding.
hid_dim: dimension of hidden layer.
stacked_num: number of stacked lstm-hidden layer.
is_predict: is predicting or not.
Some layers is not needed in network when predicting.
"""
assert
stacked_num
%
2
==
1
layer_attr
=
attrs
.
ExtraLayerAttribute
(
drop_rate
=
0.5
)
fc_para_attr
=
attrs
.
ParameterAttribute
(
learning_rate
=
1e-3
)
lstm_para_attr
=
attrs
.
ParameterAttribute
(
initial_std
=
0.
,
learning_rate
=
1.
)
para_attr
=
[
fc_para_attr
,
lstm_para_attr
]
bias_attr
=
attrs
.
ParameterAttribute
(
initial_std
=
0.
,
l2_rate
=
0.
)
relu
=
activation
.
Relu
()
linear
=
activation
.
Linear
()
data
=
layer
.
data
(
"word"
,
data_type
.
integer_value_sequence
(
input_dim
))
emb
=
layer
.
embedding
(
input
=
data
,
size
=
emb_dim
)
fc1
=
layer
.
fc
(
input
=
emb
,
size
=
hid_dim
,
act
=
linear
,
bias_attr
=
bias_attr
)
lstm1
=
layer
.
lstmemory
(
input
=
fc1
,
act
=
relu
,
bias_attr
=
bias_attr
,
layer_attr
=
layer_attr
)
inputs
=
[
fc1
,
lstm1
]
for
i
in
range
(
2
,
stacked_num
+
1
):
fc
=
layer
.
fc
(
input
=
inputs
,
size
=
hid_dim
,
act
=
linear
,
param_attr
=
para_attr
,
bias_attr
=
bias_attr
)
lstm
=
layer
.
lstmemory
(
input
=
fc
,
reverse
=
(
i
%
2
)
==
0
,
act
=
relu
,
bias_attr
=
bias_attr
,
layer_attr
=
layer_attr
)
inputs
=
[
fc
,
lstm
]
fc_last
=
layer
.
pooling
(
input
=
inputs
[
0
],
pooling_type
=
MaxPooling
())
lstm_last
=
layer
.
pooling
(
input
=
inputs
[
1
],
pooling_type
=
MaxPooling
())
output
=
layer
.
fc
(
input
=
[
fc_last
,
lstm_last
],
size
=
class_dim
,
act
=
activation
.
Softmax
(),
bias_attr
=
bias_attr
,
param_attr
=
para_attr
)
lbl
=
layer
.
data
(
"label"
,
data_type
.
integer_value
(
2
))
cost
=
layer
.
classification_cost
(
input
=
output
,
label
=
lbl
)
return
cost
...
...
@@ -177,7 +245,9 @@ if __name__ == '__main__':
paddle
.
init
(
use_gpu
=
True
,
trainer_count
=
4
)
# network config
cost
=
convolution_net
(
dict_dim
,
class_dim
=
class_dim
,
is_predict
=
is_predict
)
# cost = convolution_net(dict_dim, class_dim=class_dim, is_predict=is_predict)
cost
=
stacked_lstm_net
(
dict_dim
,
class_dim
=
class_dim
,
stacked_num
=
3
,
is_predict
=
is_predict
)
# create parameters
parameters
=
paddle
.
parameters
.
create
(
cost
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录