Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
cf515e4a
P
Paddle
项目概览
机器未来
/
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看板
提交
cf515e4a
编写于
8月 23, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimize code and name
上级
76677f25
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
27 addition
and
29 deletion
+27
-29
python/paddle/v2/framework/tests/mnist.py
python/paddle/v2/framework/tests/mnist.py
+27
-29
未找到文件。
python/paddle/v2/framework/tests/mnist.py
浏览文件 @
cf515e4a
...
@@ -134,7 +134,7 @@ def cross_entropy_layer(net, input, label):
...
@@ -134,7 +134,7 @@ def cross_entropy_layer(net, input, label):
return
cost_name
return
cost_name
def
get
_backward_net
(
forward_net
):
def
create
_backward_net
(
forward_net
):
net
=
core
.
Operator
.
backward
(
forward_net
,
set
())
net
=
core
.
Operator
.
backward
(
forward_net
,
set
())
for
input
in
net
.
inputs
()[
"all"
]:
for
input
in
net
.
inputs
()[
"all"
]:
var
=
scope
.
new_var
(
input
)
var
=
scope
.
new_var
(
input
)
...
@@ -145,29 +145,29 @@ def get_backward_net(forward_net):
...
@@ -145,29 +145,29 @@ def get_backward_net(forward_net):
return
net
return
net
def
print_inputs_outputs
(
op
):
def
debug_print_op
(
op
):
print
(
"==============="
+
op
.
type
()
+
"=============="
)
print
(
"==============="
+
op
.
type
()
+
"=============="
)
print
(
"***inputs:***"
)
print
(
"***inputs:***"
)
for
input
in
op
.
inputs
()[
"all"
]:
for
input
in
op
.
inputs
()[
"all"
]:
print
input
,
scope
.
find_var
(
input
).
get_tensor
().
get_dims
()
print
input
,
scope
.
find_var
(
input
).
get_tensor
().
get_dims
()
print
(
"***outputs:***"
)
print
(
"
\n
***outputs:***"
)
for
output
in
op
.
outputs
()[
"all"
]:
for
output
in
op
.
outputs
()[
"all"
]:
print
output
,
scope
.
find_var
(
output
).
get_tensor
().
get_dims
()
print
output
,
scope
.
find_var
(
output
).
get_tensor
().
get_dims
()
print
(
""
)
print
(
""
)
print
(
""
)
print
(
""
)
def
set_cost
():
def
set_cost
(
cost
):
cost_shape
=
numpy
.
array
(
scope
.
find_var
(
"cross_entropy_3"
).
get_tensor
(
cost_shape
=
numpy
.
array
(
scope
.
find_var
(
cost
).
get_tensor
()).
shape
)).
shape
cost_grad
=
\
cost_grad
=
scope
.
find_var
(
grad_var_name
(
"cross_entropy_3"
)).
get_tensor
()
scope
.
find_var
(
grad_var_name
(
cost
)).
get_tensor
()
cost_grad
.
set_dims
(
cost_shape
)
cost_grad
.
set_dims
(
cost_shape
)
cost_grad
.
alloc_float
(
place
)
cost_grad
.
alloc_float
(
place
)
cost_grad
.
set
(
numpy
.
ones
(
cost_shape
).
astype
(
"float32"
),
place
)
cost_grad
.
set
(
numpy
.
ones
(
cost_shape
).
astype
(
"float32"
),
place
)
def
mean_cost
():
def
mean_cost
(
cost
):
cost_data
=
numpy
.
array
(
scope
.
find_var
(
"cross_entropy_3"
).
get_tensor
())
cost_data
=
numpy
.
array
(
scope
.
find_var
(
cost
).
get_tensor
())
return
cost_data
.
sum
()
/
len
(
cost_data
)
return
cost_data
.
sum
()
/
len
(
cost_data
)
...
@@ -180,23 +180,23 @@ def error_rate(predict, label):
...
@@ -180,23 +180,23 @@ def error_rate(predict, label):
images
=
data_layer
(
name
=
'pixel'
,
dims
=
[
BATCH_SIZE
,
784
])
images
=
data_layer
(
name
=
'pixel'
,
dims
=
[
BATCH_SIZE
,
784
])
label
=
data_layer
(
name
=
'label'
,
dims
=
[
BATCH_SIZE
])
label
s
=
data_layer
(
name
=
'label'
,
dims
=
[
BATCH_SIZE
])
fc1
=
fc_layer
(
net
=
forward_network
,
input
=
images
,
size
=
100
,
act
=
"sigmoid"
)
fc1
=
fc_layer
(
net
=
forward_network
,
input
=
images
,
size
=
100
,
act
=
"sigmoid"
)
fc2
=
fc_layer
(
net
=
forward_network
,
input
=
fc1
,
size
=
100
,
act
=
"sigmoid"
)
fc2
=
fc_layer
(
net
=
forward_network
,
input
=
fc1
,
size
=
100
,
act
=
"sigmoid"
)
predict
=
fc_layer
(
net
=
forward_network
,
input
=
fc2
,
size
=
100
,
act
=
"softmax"
)
predict
=
fc_layer
(
net
=
forward_network
,
input
=
fc2
,
size
=
100
,
act
=
"softmax"
)
cost
=
cross_entropy_layer
(
net
=
forward_network
,
input
=
predict
,
label
=
label
)
cost
=
cross_entropy_layer
(
net
=
forward_network
,
input
=
predict
,
label
=
label
s
)
forward_network
.
complete_add_op
(
True
)
forward_network
.
complete_add_op
(
True
)
backward_net
=
get
_backward_net
(
forward_network
)
backward_net
=
create
_backward_net
(
forward_network
)
optimize_net
.
complete_add_op
(
True
)
optimize_net
.
complete_add_op
(
True
)
print
(
forward_network
)
print
(
forward_network
)
print
(
backward_net
)
print
(
backward_net
)
print
(
optimize_net
)
print
(
optimize_net
)
print_inputs_outputs
(
forward_network
)
debug_print_op
(
forward_network
)
print_inputs_outputs
(
backward_net
)
debug_print_op
(
backward_net
)
print_inputs_outputs
(
optimize_net
)
debug_print_op
(
optimize_net
)
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
reader
.
shuffle
(
...
@@ -204,19 +204,19 @@ train_reader = paddle.batch(
...
@@ -204,19 +204,19 @@ train_reader = paddle.batch(
batch_size
=
BATCH_SIZE
)
batch_size
=
BATCH_SIZE
)
def
test
():
def
test
(
cost_name
):
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
128
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
128
)
cost
=
[]
cost
=
[]
error
=
[]
error
=
[]
for
data
in
test_reader
():
for
data
in
test_reader
():
image
=
numpy
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
image
_data
=
numpy
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
label
=
numpy
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"int32"
)
label
_data
=
numpy
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"int32"
)
feed_data
(
"pixel"
,
image
)
feed_data
(
images
,
image_data
)
feed_data
(
"label"
,
label
)
feed_data
(
labels
,
label_data
)
forward_network
.
infer_shape
(
scope
)
forward_network
.
infer_shape
(
scope
)
forward_network
.
run
(
scope
,
dev_ctx
)
forward_network
.
run
(
scope
,
dev_ctx
)
cost
.
append
(
mean_cost
())
cost
.
append
(
mean_cost
(
cost_name
))
error
.
append
(
error_rate
(
predict
,
"label"
))
error
.
append
(
error_rate
(
predict
,
"label"
))
print
(
"cost="
+
str
(
sum
(
cost
)
/
float
(
len
(
cost
)))
+
" error_rate="
+
str
(
print
(
"cost="
+
str
(
sum
(
cost
)
/
float
(
len
(
cost
)))
+
" error_rate="
+
str
(
sum
(
error
)
/
float
(
len
(
error
))))
sum
(
error
)
/
float
(
len
(
error
))))
...
@@ -227,22 +227,20 @@ for pass_id in range(PASS_NUM):
...
@@ -227,22 +227,20 @@ for pass_id in range(PASS_NUM):
batch_id
=
0
batch_id
=
0
for
data
in
train_reader
():
for
data
in
train_reader
():
image
=
numpy
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
image
_data
=
numpy
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
label
=
numpy
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"int32"
)
label
_data
=
numpy
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"int32"
)
feed_data
(
"pixel"
,
image
)
feed_data
(
images
,
image_data
)
feed_data
(
"label"
,
label
)
feed_data
(
labels
,
label_data
)
forward_network
.
infer_shape
(
scope
)
forward_network
.
infer_shape
(
scope
)
forward_network
.
run
(
scope
,
dev_ctx
)
forward_network
.
run
(
scope
,
dev_ctx
)
set_cost
()
set_cost
(
cost
)
backward_net
.
infer_shape
(
scope
)
backward_net
.
infer_shape
(
scope
)
backward_net
.
run
(
scope
,
dev_ctx
)
backward_net
.
run
(
scope
,
dev_ctx
)
optimize_net
.
run
(
scope
,
dev_ctx
)
optimize_net
.
run
(
scope
,
dev_ctx
)
if
batch_id
%
100
==
0
:
if
batch_id
%
100
==
0
:
print
(
"pass["
+
str
(
pass_id
)
+
"] batch_id["
+
str
(
batch_id
)
+
"]"
)
print
(
"pass["
+
str
(
pass_id
)
+
"] batch_id["
+
str
(
batch_id
)
+
"]"
)
test
()
test
(
cost
)
# print(mean_cost())
# print(error_rate(predict, "label"))
batch_id
=
batch_id
+
1
batch_id
=
batch_id
+
1
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录