Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
33e54c43
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
33e54c43
编写于
3月 08, 2017
作者:
H
hedaoyuan
提交者:
GitHub
3月 08, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1579 from hedaoyuan/fix_sentiment_demo
Use v2 attr and pooling fix sentiment train_v2.py
上级
0d784405
1ff95e3d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
23 addition
and
25 deletion
+23
-25
demo/sentiment/train_v2.py
demo/sentiment/train_v2.py
+23
-25
未找到文件。
demo/sentiment/train_v2.py
浏览文件 @
33e54c43
...
...
@@ -13,8 +13,6 @@
# limitations under the License.
import
sys
import
paddle.trainer_config_helpers.attrs
as
attrs
from
paddle.trainer_config_helpers.poolings
import
MaxPooling
import
paddle.v2
as
paddle
...
...
@@ -51,16 +49,14 @@ def stacked_lstm_net(input_dim,
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.
)
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
]
bias_attr
=
attrs
.
ParameterAttribute
(
initial_std
=
0.
,
l2_rate
=
0.
)
bias_attr
=
paddle
.
attr
.
Param
(
initial_std
=
0.
,
l2_rate
=
0.
)
relu
=
paddle
.
activation
.
Relu
()
linear
=
paddle
.
activation
.
Linear
()
...
...
@@ -90,8 +86,10 @@ def stacked_lstm_net(input_dim,
layer_attr
=
layer_attr
)
inputs
=
[
fc
,
lstm
]
fc_last
=
paddle
.
layer
.
pooling
(
input
=
inputs
[
0
],
pooling_type
=
MaxPooling
())
lstm_last
=
paddle
.
layer
.
pooling
(
input
=
inputs
[
1
],
pooling_type
=
MaxPooling
())
fc_last
=
paddle
.
layer
.
pooling
(
input
=
inputs
[
0
],
pooling_type
=
paddle
.
pooling
.
Max
())
lstm_last
=
paddle
.
layer
.
pooling
(
input
=
inputs
[
1
],
pooling_type
=
paddle
.
pooling
.
Max
())
output
=
paddle
.
layer
.
fc
(
input
=
[
fc_last
,
lstm_last
],
size
=
class_dim
,
act
=
paddle
.
activation
.
Softmax
(),
...
...
@@ -105,14 +103,23 @@ def stacked_lstm_net(input_dim,
if
__name__
==
'__main__'
:
# init
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
4
)
paddle
.
init
(
use_gpu
=
False
)
#
network config
#
data
print
'load dictionary...'
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
dict_dim
=
len
(
word_dict
)
class_dim
=
2
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
lambda
:
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
1000
),
batch_size
=
100
)
test_reader
=
paddle
.
batch
(
lambda
:
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
100
)
feeding
=
{
'word'
:
0
,
'label'
:
1
}
# network config
# Please choose the way to build the network
# by uncommenting the corresponding line.
cost
=
convolution_net
(
dict_dim
,
class_dim
=
class_dim
)
...
...
@@ -137,12 +144,7 @@ if __name__ == '__main__':
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
flush
()
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
lambda
:
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
128
),
feeding
=
{
'word'
:
0
,
'label'
:
1
})
result
=
trainer
.
test
(
reader
=
test_reader
,
feeding
=
feeding
)
print
"
\n
Test with Pass %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
# create trainer
...
...
@@ -151,11 +153,7 @@ if __name__ == '__main__':
update_equation
=
adam_optimizer
)
trainer
.
train
(
reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
lambda
:
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
1000
),
batch_size
=
100
),
reader
=
train_reader
,
event_handler
=
event_handler
,
feeding
=
{
'word'
:
0
,
'label'
:
1
},
num_passes
=
10
)
feeding
=
feeding
,
num_passes
=
2
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录