Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
迷途归一
Paddle
提交
d5be1d4d
P
Paddle
项目概览
迷途归一
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d5be1d4d
编写于
11月 20, 2017
作者:
F
fengjiayi
提交者:
GitHub
11月 20, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use Evaluator in book tests (#5778)
上级
d2e30a2c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
45 addition
and
25 deletion
+45
-25
python/paddle/v2/fluid/tests/book/test_image_classification_train.py
...le/v2/fluid/tests/book/test_image_classification_train.py
+8
-3
python/paddle/v2/fluid/tests/book/test_recognize_digits_conv.py
.../paddle/v2/fluid/tests/book/test_recognize_digits_conv.py
+4
-4
python/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py
...n/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py
+11
-4
python/paddle/v2/fluid/tests/book/test_understand_sentiment_conv.py
...dle/v2/fluid/tests/book/test_understand_sentiment_conv.py
+11
-7
python/paddle/v2/fluid/tests/book/test_understand_sentiment_dynamic_lstm.py
...luid/tests/book/test_understand_sentiment_dynamic_lstm.py
+11
-7
未找到文件。
python/paddle/v2/fluid/tests/book/test_image_classification_train.py
浏览文件 @
d5be1d4d
...
...
@@ -4,6 +4,7 @@ import paddle.v2.fluid.core as core
import
paddle.v2.fluid.framework
as
framework
import
paddle.v2.fluid.layers
as
layers
import
paddle.v2.fluid.nets
as
nets
import
paddle.v2.fluid.evaluator
as
evaluator
from
paddle.v2.fluid.executor
import
Executor
from
paddle.v2.fluid.initializer
import
XavierInitializer
from
paddle.v2.fluid.optimizer
import
AdamOptimizer
...
...
@@ -103,12 +104,13 @@ net = vgg16_bn_drop(images)
predict
=
layers
.
fc
(
input
=
net
,
size
=
classdim
,
act
=
'softmax'
)
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
)
avg_cost
=
layers
.
mean
(
x
=
cost
)
accuracy
=
layers
.
accuracy
(
input
=
predict
,
label
=
label
)
# optimizer = SGDOptimizer(learning_rate=0.001)
optimizer
=
AdamOptimizer
(
learning_rate
=
0.001
)
opts
=
optimizer
.
minimize
(
avg_cost
)
accuracy
,
acc_out
=
evaluator
.
accuracy
(
input
=
predict
,
label
=
label
)
BATCH_SIZE
=
128
PASS_NUM
=
1
...
...
@@ -124,6 +126,7 @@ exe.run(framework.default_startup_program())
for
pass_id
in
range
(
PASS_NUM
):
batch_id
=
0
accuracy
.
reset
(
exe
)
for
data
in
train_reader
():
img_data
=
np
.
array
(
map
(
lambda
x
:
x
[
0
].
reshape
(
data_shape
),
data
)).
astype
(
"float32"
)
...
...
@@ -141,12 +144,14 @@ for pass_id in range(PASS_NUM):
outs
=
exe
.
run
(
framework
.
default_main_program
(),
feed
=
{
"pixel"
:
tensor_img
,
"label"
:
tensor_y
},
fetch_list
=
[
avg_cost
,
acc
uracy
])
fetch_list
=
[
avg_cost
,
acc
_out
])
loss
=
np
.
array
(
outs
[
0
])
acc
=
np
.
array
(
outs
[
1
])
pass_acc
=
accuracy
.
eval
(
exe
)
print
(
"pass_id:"
+
str
(
pass_id
)
+
" batch_id:"
+
str
(
batch_id
)
+
" loss:"
+
str
(
loss
)
+
" acc:"
+
str
(
acc
))
" loss:"
+
str
(
loss
)
+
" acc:"
+
str
(
acc
)
+
" pass_acc:"
+
str
(
pass_acc
))
batch_id
=
batch_id
+
1
if
batch_id
>
1
:
...
...
python/paddle/v2/fluid/tests/book/test_recognize_digits_conv.py
浏览文件 @
d5be1d4d
...
...
@@ -46,7 +46,6 @@ exe = Executor(place)
exe
.
run
(
framework
.
default_startup_program
())
for
pass_id
in
range
(
PASS_NUM
):
count
=
0
accuracy
.
reset
(
exe
)
for
data
in
train_reader
():
img_data
=
np
.
array
(
map
(
lambda
x
:
x
[
0
].
reshape
([
1
,
28
,
28
]),
...
...
@@ -66,13 +65,14 @@ for pass_id in range(PASS_NUM):
loss
=
np
.
array
(
outs
[
0
])
acc
=
np
.
array
(
outs
[
1
])
pass_acc
=
accuracy
.
eval
(
exe
)
print
"pass id : "
,
pass_id
,
pass_acc
print
(
"pass_id="
+
str
(
pass_id
)
+
" acc="
+
str
(
acc
)
+
" pass_acc="
+
str
(
pass_acc
))
# print loss, acc
if
loss
<
10.0
and
acc
>
0.9
:
if
loss
<
10.0
and
pass_
acc
>
0.9
:
# if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good.
exit
(
0
)
pass_acc
=
accuracy
.
eval
(
exe
)
print
"pass id : "
,
pass_id
,
pass_acc
print
(
"pass_id="
+
str
(
pass_id
)
+
" pass_acc="
+
str
(
pass_acc
))
exit
(
1
)
python/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py
浏览文件 @
d5be1d4d
...
...
@@ -3,6 +3,7 @@ import paddle.v2 as paddle
import
paddle.v2.fluid.core
as
core
import
paddle.v2.fluid.framework
as
framework
import
paddle.v2.fluid.layers
as
layers
import
paddle.v2.fluid.evaluator
as
evaluator
from
paddle.v2.fluid.executor
import
Executor
from
paddle.v2.fluid.initializer
import
UniformInitializer
from
paddle.v2.fluid.optimizer
import
MomentumOptimizer
...
...
@@ -30,11 +31,12 @@ label = layers.data(name='y', shape=[1], data_type='int64')
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
)
avg_cost
=
layers
.
mean
(
x
=
cost
)
accuracy
=
layers
.
accuracy
(
input
=
predict
,
label
=
label
)
optimizer
=
MomentumOptimizer
(
learning_rate
=
0.001
,
momentum
=
0.9
)
opts
=
optimizer
.
minimize
(
avg_cost
)
accuracy
,
acc_out
=
evaluator
.
accuracy
(
input
=
predict
,
label
=
label
)
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
8192
),
...
...
@@ -47,6 +49,7 @@ exe.run(framework.default_startup_program())
PASS_NUM
=
100
for
pass_id
in
range
(
PASS_NUM
):
accuracy
.
reset
(
exe
)
for
data
in
train_reader
():
x_data
=
np
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
y_data
=
np
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"int64"
)
...
...
@@ -61,9 +64,13 @@ for pass_id in range(PASS_NUM):
outs
=
exe
.
run
(
framework
.
default_main_program
(),
feed
=
{
'x'
:
tensor_x
,
'y'
:
tensor_y
},
fetch_list
=
[
avg_cost
,
acc
uracy
])
fetch_list
=
[
avg_cost
,
acc
_out
])
out
=
np
.
array
(
outs
[
0
])
acc
=
np
.
array
(
outs
[
1
])
if
out
[
0
]
<
5.0
:
exit
(
0
)
# if avg cost less than 5.0, we think our code is good.
pass_acc
=
accuracy
.
eval
(
exe
)
if
pass_acc
>
0.7
:
exit
(
0
)
# print("pass_id=" + str(pass_id) + " auc=" +
# str(acc) + " pass_acc=" + str(pass_acc))
exit
(
1
)
python/paddle/v2/fluid/tests/book/test_understand_sentiment_conv.py
浏览文件 @
d5be1d4d
import
numpy
as
np
import
paddle.v2
as
paddle
import
paddle.v2.fluid.core
as
core
import
paddle.v2.fluid.evaluator
as
evaluator
import
paddle.v2.fluid.framework
as
framework
import
paddle.v2.fluid.layers
as
layers
import
paddle.v2.fluid.nets
as
nets
...
...
@@ -32,8 +33,8 @@ def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32):
avg_cost
=
layers
.
mean
(
x
=
cost
)
adam_optimizer
=
AdamOptimizer
(
learning_rate
=
0.002
)
opts
=
adam_optimizer
.
minimize
(
avg_cost
)
acc
=
layers
.
accuracy
(
input
=
prediction
,
label
=
label
)
return
avg_cost
,
acc
acc
uracy
,
acc_out
=
evaluator
.
accuracy
(
input
=
prediction
,
label
=
label
)
return
avg_cost
,
acc
uracy
,
acc_out
def
to_lodtensor
(
data
,
place
):
...
...
@@ -59,7 +60,8 @@ def main():
dict_dim
=
len
(
word_dict
)
class_dim
=
2
cost
,
acc
=
convolution_net
(
input_dim
=
dict_dim
,
class_dim
=
class_dim
)
cost
,
accuracy
,
acc_out
=
convolution_net
(
input_dim
=
dict_dim
,
class_dim
=
class_dim
)
train_data
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
...
...
@@ -71,6 +73,7 @@ def main():
exe
.
run
(
framework
.
default_startup_program
())
for
pass_id
in
xrange
(
PASS_NUM
):
accuracy
.
reset
(
exe
)
for
data
in
train_data
():
tensor_words
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
),
place
)
...
...
@@ -83,12 +86,13 @@ def main():
outs
=
exe
.
run
(
framework
.
default_main_program
(),
feed
=
{
"words"
:
tensor_words
,
"label"
:
tensor_label
},
fetch_list
=
[
cost
,
acc
])
fetch_list
=
[
cost
,
acc
_out
])
cost_val
=
np
.
array
(
outs
[
0
])
acc_val
=
np
.
array
(
outs
[
1
])
print
(
"cost="
+
str
(
cost_val
)
+
" acc="
+
str
(
acc_val
))
if
cost_val
<
1.0
and
acc_val
>
0.7
:
pass_acc
=
accuracy
.
eval
(
exe
)
print
(
"cost="
+
str
(
cost_val
)
+
" acc="
+
str
(
acc_val
)
+
" pass_acc="
+
str
(
pass_acc
))
if
cost_val
<
1.0
and
pass_acc
>
0.8
:
exit
(
0
)
exit
(
1
)
...
...
python/paddle/v2/fluid/tests/book/test_understand_sentiment_dynamic_lstm.py
浏览文件 @
d5be1d4d
import
numpy
as
np
import
paddle.v2
as
paddle
import
paddle.v2.fluid.core
as
core
import
paddle.v2.fluid.evaluator
as
evaluator
import
paddle.v2.fluid.framework
as
framework
import
paddle.v2.fluid.layers
as
layers
from
paddle.v2.fluid.executor
import
Executor
...
...
@@ -41,8 +42,8 @@ def stacked_lstm_net(input_dim,
avg_cost
=
layers
.
mean
(
x
=
cost
)
adam_optimizer
=
AdamOptimizer
(
learning_rate
=
0.002
)
opts
=
adam_optimizer
.
minimize
(
avg_cost
)
acc
=
layers
.
accuracy
(
input
=
prediction
,
label
=
label
)
return
avg_cost
,
acc
acc
uracy
,
acc_out
=
evaluator
.
accuracy
(
input
=
prediction
,
label
=
label
)
return
avg_cost
,
acc
uracy
,
acc_out
def
to_lodtensor
(
data
,
place
):
...
...
@@ -69,7 +70,8 @@ def main():
dict_dim
=
len
(
word_dict
)
class_dim
=
2
cost
,
acc
=
stacked_lstm_net
(
input_dim
=
dict_dim
,
class_dim
=
class_dim
)
cost
,
accuracy
,
acc_out
=
stacked_lstm_net
(
input_dim
=
dict_dim
,
class_dim
=
class_dim
)
train_data
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
...
...
@@ -81,6 +83,7 @@ def main():
exe
.
run
(
framework
.
default_startup_program
())
for
pass_id
in
xrange
(
PASS_NUM
):
accuracy
.
reset
(
exe
)
for
data
in
train_data
():
tensor_words
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
),
place
)
...
...
@@ -93,12 +96,13 @@ def main():
outs
=
exe
.
run
(
framework
.
default_main_program
(),
feed
=
{
"words"
:
tensor_words
,
"label"
:
tensor_label
},
fetch_list
=
[
cost
,
acc
])
fetch_list
=
[
cost
,
acc
_out
])
cost_val
=
np
.
array
(
outs
[
0
])
acc_val
=
np
.
array
(
outs
[
1
])
print
(
"cost="
+
str
(
cost_val
)
+
" acc="
+
str
(
acc_val
))
if
cost_val
<
1.0
and
acc_val
>
0.7
:
pass_acc
=
accuracy
.
eval
(
exe
)
print
(
"cost="
+
str
(
cost_val
)
+
" acc="
+
str
(
acc_val
)
+
" pass_acc="
+
str
(
pass_acc
))
if
cost_val
<
1.0
and
acc_val
>
0.8
:
exit
(
0
)
exit
(
1
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录