Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
02690a1e
B
book
项目概览
PaddlePaddle
/
book
通知
16
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看板
提交
02690a1e
编写于
8月 07, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Eval for understand_sentiment chapter
上级
d500ac9c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
2 deletion
+48
-2
06.understand_sentiment/train_conv.py
06.understand_sentiment/train_conv.py
+1
-1
06.understand_sentiment/train_stacked_lstm.py
06.understand_sentiment/train_stacked_lstm.py
+47
-1
未找到文件。
06.understand_sentiment/train_conv.py
浏览文件 @
02690a1e
...
@@ -162,5 +162,5 @@ def main(use_cuda):
...
@@ -162,5 +162,5 @@ def main(use_cuda):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
use_cuda
=
False
# set to True if training with GPU
use_cuda
=
True
# set to True if training with GPU
main
(
use_cuda
)
main
(
use_cuda
)
06.understand_sentiment/train_stacked_lstm.py
浏览文件 @
02690a1e
...
@@ -161,14 +161,60 @@ def infer(use_cuda, inference_program, params_dirname=None):
...
@@ -161,14 +161,60 @@ def infer(use_cuda, inference_program, params_dirname=None):
" to be negative for review
\'
"
,
reviews_str
[
i
],
"
\'
"
)
" to be negative for review
\'
"
,
reviews_str
[
i
],
"
\'
"
)
def
eval
(
use_cuda
,
inference_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
)
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
)
exe
.
run
(
fluid
.
default_startup_program
())
fluid
.
io
.
load_persistables
(
exe
,
params_dirname
)
total_acc
=
[]
for
index
,
batch_data
in
enumerate
(
test_reader
()):
print
(
index
)
sent
=
map
(
lambda
x
:
x
[
0
],
batch_data
)
label
=
np
.
array
(
map
(
lambda
x
:
[
x
[
1
]],
batch_data
)).
astype
(
"int64"
)
seq_lens
=
[[
len
(
seq
)
for
seq
in
sent
]]
words
=
fluid
.
create_lod_tensor
(
sent
,
seq_lens
,
place
)
predict
,
acc
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
'words'
:
words
,
'label'
:
label
},
fetch_list
=
[
prediction
,
accuracy
])
total_acc
.
append
(
acc
)
print
(
predict
)
num_right
=
0
for
n
,
data
in
enumerate
(
batch_data
):
predicted
=
predict
[
n
][
1
]
>=
0.5
labeled
=
label
[
n
][
0
]
print
(
"%d, predicted: %d, labeled: %d, right: %d"
%
(
n
,
predicted
,
labeled
,
(
predicted
==
labeled
)))
num_right
+=
(
predicted
==
labeled
)
print
(
"exe acc %f, manual acc %f
\n
"
%
(
acc
,
float
(
num_right
)
/
len
(
batch_data
)))
print
(
"total mean acc = %f"
%
np
.
mean
(
total_acc
))
def
main
(
use_cuda
):
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
return
params_dirname
=
"understand_sentiment_stacked_lstm.inference.model"
params_dirname
=
"understand_sentiment_stacked_lstm.inference.model"
train
(
use_cuda
,
train_program
,
params_dirname
)
train
(
use_cuda
,
train_program
,
params_dirname
)
infer
(
use_cuda
,
inference_program
,
params_dirname
)
infer
(
use_cuda
,
inference_program
,
params_dirname
)
eval
(
use_cuda
,
inference_program
,
params_dirname
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
use_cuda
=
False
# set to True if training with GPU
use_cuda
=
True
# set to True if training with GPU
main
(
use_cuda
)
main
(
use_cuda
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录