Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
80868f79
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
80868f79
编写于
5月 22, 2018
作者:
S
Siddharth Goyal
提交者:
daminglu
5月 22, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add dynamic rnn model for sentiment analysis with new API (#10849)
上级
faedee0d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
164 addition
and
0 deletion
+164
-0
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_dynamic_rnn.py
...rstand_sentiment/test_understand_sentiment_dynamic_rnn.py
+164
-0
未找到文件。
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_dynamic_rnn.py
0 → 100644
浏览文件 @
80868f79
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
paddle
import
paddle.fluid
as
fluid
from
functools
import
partial
import
numpy
as
np
CLASS_DIM
=
2
EMB_DIM
=
128
BATCH_SIZE
=
128
LSTM_SIZE
=
128
def
dynamic_rnn_lstm
(
data
,
input_dim
,
class_dim
,
emb_dim
,
lstm_size
):
emb
=
fluid
.
layers
.
embedding
(
input
=
data
,
size
=
[
input_dim
,
emb_dim
],
is_sparse
=
True
)
sentence
=
fluid
.
layers
.
fc
(
input
=
emb
,
size
=
lstm_size
,
act
=
'tanh'
)
rnn
=
fluid
.
layers
.
DynamicRNN
()
with
rnn
.
block
():
word
=
rnn
.
step_input
(
sentence
)
prev_hidden
=
rnn
.
memory
(
value
=
0.0
,
shape
=
[
lstm_size
])
prev_cell
=
rnn
.
memory
(
value
=
0.0
,
shape
=
[
lstm_size
])
def
gate_common
(
ipt
,
hidden
,
size
):
gate0
=
fluid
.
layers
.
fc
(
input
=
ipt
,
size
=
size
,
bias_attr
=
True
)
gate1
=
fluid
.
layers
.
fc
(
input
=
hidden
,
size
=
size
,
bias_attr
=
False
)
return
gate0
+
gate1
forget_gate
=
fluid
.
layers
.
sigmoid
(
x
=
gate_common
(
word
,
prev_hidden
,
lstm_size
))
input_gate
=
fluid
.
layers
.
sigmoid
(
x
=
gate_common
(
word
,
prev_hidden
,
lstm_size
))
output_gate
=
fluid
.
layers
.
sigmoid
(
x
=
gate_common
(
word
,
prev_hidden
,
lstm_size
))
cell_gate
=
fluid
.
layers
.
sigmoid
(
x
=
gate_common
(
word
,
prev_hidden
,
lstm_size
))
cell
=
forget_gate
*
prev_cell
+
input_gate
*
cell_gate
hidden
=
output_gate
*
fluid
.
layers
.
tanh
(
x
=
cell
)
rnn
.
update_memory
(
prev_cell
,
cell
)
rnn
.
update_memory
(
prev_hidden
,
hidden
)
rnn
.
output
(
hidden
)
last
=
fluid
.
layers
.
sequence_last_step
(
rnn
())
prediction
=
fluid
.
layers
.
fc
(
input
=
last
,
size
=
class_dim
,
act
=
"softmax"
)
return
prediction
def
inference_program
(
word_dict
):
data
=
fluid
.
layers
.
data
(
name
=
"words"
,
shape
=
[
1
],
dtype
=
"int64"
,
lod_level
=
1
)
dict_dim
=
len
(
word_dict
)
pred
=
dynamic_rnn_lstm
(
data
,
dict_dim
,
CLASS_DIM
,
EMB_DIM
,
LSTM_SIZE
)
return
pred
def
train_program
(
word_dict
):
prediction
=
inference_program
(
word_dict
)
label
=
fluid
.
layers
.
data
(
name
=
"label"
,
shape
=
[
1
],
dtype
=
"int64"
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
prediction
,
label
=
label
)
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
train_program
,
save_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
0.002
)
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
trainer
=
fluid
.
Trainer
(
train_func
=
partial
(
train_program
,
word_dict
),
place
=
place
,
optimizer
=
optimizer
)
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
)
avg_cost
,
acc
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'words'
,
'label'
])
print
(
"avg_cost: %s"
%
avg_cost
)
print
(
"acc : %s"
%
acc
)
if
acc
>
0.2
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save_dirname
)
trainer
.
stop
()
else
:
print
(
'BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'
.
format
(
event
.
epoch
+
1
,
avg_cost
,
acc
))
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
elif
isinstance
(
event
,
fluid
.
EndStepEvent
):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
map
(
np
.
array
,
event
.
metrics
)))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
trainer
.
save_params
(
save_dirname
)
trainer
.
stop
()
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
25000
),
batch_size
=
BATCH_SIZE
)
trainer
.
train
(
num_epochs
=
1
,
event_handler
=
event_handler
,
reader
=
train_reader
,
feed_order
=
[
'words'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
save_dirname
,
place
=
place
)
def
create_random_lodtensor
(
lod
,
place
,
low
,
high
):
data
=
np
.
random
.
random_integers
(
low
,
high
,
[
lod
[
-
1
],
1
]).
astype
(
"int64"
)
res
=
fluid
.
LoDTensor
()
res
.
set
(
data
,
place
)
res
.
set_lod
([
lod
])
return
res
lod
=
[
0
,
4
,
10
]
tensor_words
=
create_random_lodtensor
(
lod
,
place
,
low
=
0
,
high
=
len
(
word_dict
)
-
1
)
results
=
inferencer
.
infer
({
'words'
:
tensor_words
})
print
(
"infer results: "
,
results
)
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"understand_sentiment_conv.inference.model"
train
(
use_cuda
,
train_program
,
save_path
)
infer
(
use_cuda
,
inference_program
,
save_path
)
if
__name__
==
'__main__'
:
for
use_cuda
in
(
False
,
True
):
main
(
use_cuda
=
use_cuda
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录