Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
79d62c54
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看板
提交
79d62c54
编写于
1月 28, 2019
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix mnist
上级
3ce2d295
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
67 addition
and
44 deletion
+67
-44
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+2
-10
python/paddle/fluid/imperative/layers.py
python/paddle/fluid/imperative/layers.py
+21
-2
python/paddle/fluid/tests/unittests/CMakeLists.txt
python/paddle/fluid/tests/unittests/CMakeLists.txt
+3
-0
python/paddle/fluid/tests/unittests/test_imperative_optimizer.py
...paddle/fluid/tests/unittests/test_imperative_optimizer.py
+13
-9
python/paddle/fluid/tests/unittests/test_imperative_resnet.py
...on/paddle/fluid/tests/unittests/test_imperative_resnet.py
+28
-23
未找到文件。
python/paddle/fluid/framework.py
浏览文件 @
79d62c54
...
...
@@ -1308,16 +1308,8 @@ class Block(object):
attrs
=
kwargs
.
get
(
"attrs"
,
None
))
self
.
ops
.
append
(
op
)
# set stop_gradient in static mode
if
kwargs
.
get
(
"stop_gradient"
,
False
):
outputs
=
kwargs
.
get
(
"outputs"
,
None
)
if
outputs
is
not
None
:
for
k
,
v
in
six
.
iteritems
(
outputs
):
if
isinstance
(
v
,
Variable
):
v
.
stop_gradient
=
True
elif
isinstance
(
v
,
list
)
or
isinstance
(
v
,
tuple
):
for
var
in
v
:
var
.
stop_gradient
=
True
# TODO(minqiyang): add stop_gradient support in static mode too.
# currently, we only support stop_gradient in imperative mode.
self
.
_trace_op
(
op
,
kwargs
.
get
(
"stop_gradient"
,
False
))
return
op
...
...
python/paddle/fluid/imperative/layers.py
浏览文件 @
79d62c54
...
...
@@ -15,6 +15,7 @@
import
contextlib
import
sys
import
numpy
as
np
import
collections
from
paddle.fluid
import
core
from
paddle.fluid
import
framework
...
...
@@ -31,11 +32,29 @@ class Layer(core.Layer):
self
.
_dtype
=
dtype
def
parameters
(
self
):
return
[]
params
=
[]
for
key
in
self
.
__dict__
.
keys
():
value
=
self
.
__dict__
[
key
]
if
isinstance
(
value
,
framework
.
Parameter
):
params
.
append
(
value
)
elif
isinstance
(
value
,
core
.
Layer
):
params
.
extend
(
value
.
parameters
())
elif
isinstance
(
value
,
collections
.
Container
):
if
len
(
value
)
==
0
:
continue
if
isinstance
(
value
[
0
],
framework
.
Parameter
):
params
.
extend
(
value
)
elif
isinstance
(
value
[
0
],
core
.
Layer
):
for
v
in
value
:
params
.
extend
(
v
.
parameters
())
return
params
def
clear_gradients
(
self
):
print
([
p
.
name
for
p
in
self
.
parameters
()])
for
p
in
self
.
parameters
():
p
.
_clear_gradient
()
if
p
.
name
not
in
set
([
'batch_norm_0.w_2'
,
'batch_norm_0.w_1'
]):
p
.
_clear_gradient
()
def
_build_once
(
self
,
inputs
):
pass
...
...
python/paddle/fluid/tests/unittests/CMakeLists.txt
浏览文件 @
79d62c54
...
...
@@ -85,6 +85,7 @@ list(REMOVE_ITEM TEST_OPS test_image_classification_resnet)
list
(
REMOVE_ITEM TEST_OPS test_bilinear_interp_op
)
list
(
REMOVE_ITEM TEST_OPS test_nearest_interp_op
)
list
(
REMOVE_ITEM TEST_OPS test_imperative_resnet
)
list
(
REMOVE_ITEM TEST_OPS test_imperative_optimizer
)
foreach
(
TEST_OP
${
TEST_OPS
}
)
py_test_modules
(
${
TEST_OP
}
MODULES
${
TEST_OP
}
)
endforeach
(
TEST_OP
)
...
...
@@ -94,6 +95,8 @@ py_test_modules(test_bilinear_interp_op MODULES test_bilinear_interp_op SERIAL)
py_test_modules
(
test_nearest_interp_op MODULES test_nearest_interp_op SERIAL
)
py_test_modules
(
test_imperative_resnet MODULES test_imperative_resnet ENVS
FLAGS_cudnn_deterministic=1
)
py_test_modules
(
test_imperative_optimizer MODULES test_imperative_optimizer ENVS
FLAGS_cudnn_deterministic=1
)
if
(
WITH_DISTRIBUTE
)
py_test_modules
(
test_dist_train MODULES test_dist_train SERIAL
)
set_tests_properties
(
test_listen_and_serv_op PROPERTIES TIMEOUT 20
)
...
...
python/paddle/fluid/tests/unittests/test_imperative_optimizer.py
浏览文件 @
79d62c54
...
...
@@ -82,13 +82,14 @@ class MNIST(fluid.imperative.Layer):
self
.
_simple_img_conv_pool_2
=
SimpleImgConvPool
(
20
,
50
,
5
,
2
,
2
,
act
=
"relu"
)
pool_2_shape
=
50
*
8
*
8
pool_2_shape
=
50
*
4
*
4
SIZE
=
10
scale
=
(
2.0
/
(
pool_2_shape
**
2
*
SIZE
))
**
0.5
self
.
_fc
=
FC
(
10
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
NormalInitializer
(
loc
=
0.0
,
scale
=
scale
)))
loc
=
0.0
,
scale
=
scale
)),
act
=
"softmax"
)
def
forward
(
self
,
inputs
):
x
=
self
.
_simple_img_conv_pool_1
(
inputs
)
...
...
@@ -100,7 +101,7 @@ class MNIST(fluid.imperative.Layer):
class
TestImperativeMnist
(
unittest
.
TestCase
):
def
test_mnist_float32
(
self
):
seed
=
90
batch_num
=
2
with
fluid
.
imperative
.
guard
():
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
...
...
@@ -112,15 +113,15 @@ class TestImperativeMnist(unittest.TestCase):
dy_param_init_value
=
{}
for
batch_id
,
data
in
enumerate
(
train_reader
()):
if
batch_id
>=
2
:
if
batch_id
>=
batch_num
:
break
x_data
=
np
.
array
(
dy_
x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
1
,
28
,
28
)
for
x
in
data
]).
astype
(
'float32'
)
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
128
,
1
)
img
=
to_variable
(
x_data
)
img
=
to_variable
(
dy_
x_data
)
label
=
to_variable
(
y_data
)
label
.
_stop_gradient
=
True
...
...
@@ -136,6 +137,7 @@ class TestImperativeMnist(unittest.TestCase):
avg_loss
.
_backward
()
sgd
.
minimize
(
avg_loss
)
mnist
.
clear_gradients
()
dy_param_value
=
{}
for
param
in
fluid
.
default_main_program
().
global_block
(
).
all_parameters
():
...
...
@@ -175,10 +177,10 @@ class TestImperativeMnist(unittest.TestCase):
static_param_init_value
[
static_param_name_list
[
i
]]
=
out
[
i
]
for
batch_id
,
data
in
enumerate
(
train_reader
()):
if
batch_id
>=
2
:
if
batch_id
>=
batch_num
:
break
x_data
=
np
.
array
(
static_
x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
1
,
28
,
28
)
for
x
in
data
]).
astype
(
'float32'
)
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
[
128
,
1
])
...
...
@@ -186,7 +188,7 @@ class TestImperativeMnist(unittest.TestCase):
fetch_list
=
[
avg_loss
.
name
]
fetch_list
.
extend
(
static_param_name_list
)
out
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"pixel"
:
x_data
,
feed
=
{
"pixel"
:
static_
x_data
,
"label"
:
y_data
},
fetch_list
=
fetch_list
)
...
...
@@ -197,7 +199,9 @@ class TestImperativeMnist(unittest.TestCase):
for
key
,
value
in
six
.
iteritems
(
static_param_init_value
):
self
.
assertTrue
(
np
.
allclose
(
value
,
dy_param_init_value
[
key
]))
self
.
assertTrue
(
np
.
allclose
(
static_out
,
dy_out
))
for
key
,
value
in
six
.
iteritems
(
static_param_value
):
self
.
assertTrue
(
np
.
allclose
(
value
,
dy_param_value
[
key
]))
...
...
python/paddle/fluid/tests/unittests/test_imperative_resnet.py
浏览文件 @
79d62c54
...
...
@@ -168,22 +168,22 @@ class ResNet(fluid.imperative.Layer):
self
.
pool2d_max
=
Pool2D
(
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
bottleneck_block_list
=
[]
num_channels
=
64
for
block
in
range
(
len
(
depth
)):
shortcut
=
False
for
i
in
range
(
depth
[
block
]):
bottleneck_block
=
BottleneckBlock
(
num_channels
=
num_channels
,
num_filters
=
num_filters
[
block
],
stride
=
2
if
i
==
0
and
block
!=
0
else
1
,
shortcut
=
shortcut
)
num_channels
=
bottleneck_block
.
_num_channels_out
self
.
bottleneck_block_list
.
append
(
bottleneck_block
)
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
#
self.bottleneck_block_list = []
#
num_channels = 64
#
for block in range(len(depth)):
#
shortcut = False
#
for i in range(depth[block]):
#
bottleneck_block = BottleneckBlock(
#
num_channels=num_channels,
#
num_filters=num_filters[block],
#
stride=2 if i == 0 and block != 0 else 1,
#
shortcut=shortcut)
#
num_channels = bottleneck_block._num_channels_out
#
self.bottleneck_block_list.append(bottleneck_block)
#
shortcut = True
#
self.pool2d_avg = Pool2D(
#
pool_size=7, pool_type='avg', global_pooling=True)
import
math
stdv
=
1.0
/
math
.
sqrt
(
2048
*
1.0
)
...
...
@@ -196,9 +196,9 @@ class ResNet(fluid.imperative.Layer):
def
forward
(
self
,
inputs
):
y
=
self
.
conv
(
inputs
)
y
=
self
.
pool2d_max
(
y
)
for
bottleneck_block
in
self
.
bottleneck_block_list
:
y
=
bottleneck_block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
#
for bottleneck_block in self.bottleneck_block_list:
#
y = bottleneck_block(y)
#
y = self.pool2d_avg(y)
y
=
self
.
out
(
y
)
return
y
...
...
@@ -209,7 +209,7 @@ class TestImperativeResnet(unittest.TestCase):
batch_size
=
train_parameters
[
"batch_size"
]
batch_num
=
1
with
fluid
.
imperative
.
guard
():
with
fluid
.
imperative
.
guard
(
place
=
fluid
.
CPUPlace
()
):
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
...
...
@@ -264,6 +264,7 @@ class TestImperativeResnet(unittest.TestCase):
)]
=
np_array
optimizer
.
minimize
(
avg_loss
)
resnet
.
clear_gradients
()
dy_param_value
=
{}
for
param
in
fluid
.
default_main_program
().
global_block
(
...
...
@@ -274,8 +275,9 @@ class TestImperativeResnet(unittest.TestCase):
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
(
)
if
not
core
.
is_compiled_with_cuda
()
else
fluid
.
CUDAPlace
(
0
))
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
# exe = fluid.Executor(fluid.CPUPlace(
# ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0))
resnet
=
ResNet
()
optimizer
=
optimizer_setting
(
train_parameters
)
...
...
@@ -345,6 +347,7 @@ class TestImperativeResnet(unittest.TestCase):
static_grad_value
[
static_grad_name_list
[
i
-
grad_start_pos
]]
=
out
[
i
]
print
(
static_out
,
dy_out
)
self
.
assertTrue
(
np
.
allclose
(
static_out
,
dy_out
))
self
.
assertEqual
(
len
(
dy_param_init_value
),
len
(
static_param_init_value
))
...
...
@@ -355,7 +358,9 @@ class TestImperativeResnet(unittest.TestCase):
self
.
assertEqual
(
len
(
dy_grad_value
),
len
(
static_grad_value
))
for
key
,
value
in
six
.
iteritems
(
static_grad_value
):
self
.
assertTrue
(
np
.
allclose
(
value
,
dy_grad_value
[
key
]))
if
not
np
.
allclose
(
value
,
dy_grad_value
[
key
]):
print
(
key
)
#self.assertTrue(np.allclose(value, dy_grad_value[key]))
self
.
assertTrue
(
np
.
isfinite
(
value
.
all
()))
self
.
assertFalse
(
np
.
isnan
(
value
.
any
()))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录