Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
006c32f9
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
提交
006c32f9
编写于
2月 19, 2019
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
polish parameter names
parameters within a Layer instance should be unique. test=develop
上级
e3dd6970
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
161 addition
and
98 deletion
+161
-98
python/paddle/fluid/imperative/layers.py
python/paddle/fluid/imperative/layers.py
+23
-4
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+18
-19
python/paddle/fluid/layer_helper.py
python/paddle/fluid/layer_helper.py
+3
-0
python/paddle/fluid/tests/unittests/test_base_layer.py
python/paddle/fluid/tests/unittests/test_base_layer.py
+22
-15
python/paddle/fluid/tests/unittests/test_imperative.py
python/paddle/fluid/tests/unittests/test_imperative.py
+25
-22
python/paddle/fluid/tests/unittests/test_imperative_gan.py
python/paddle/fluid/tests/unittests/test_imperative_gan.py
+15
-15
python/paddle/fluid/tests/unittests/test_imperative_optimizer.py
...paddle/fluid/tests/unittests/test_imperative_optimizer.py
+12
-8
python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
...n/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
+8
-2
python/paddle/fluid/tests/unittests/test_imperative_resnet.py
...on/paddle/fluid/tests/unittests/test_imperative_resnet.py
+35
-13
未找到文件。
python/paddle/fluid/imperative/layers.py
浏览文件 @
006c32f9
...
...
@@ -17,7 +17,7 @@ import contextlib
import
sys
import
numpy
as
np
import
collections
from
..
import
unique_name
from
paddle.fluid
import
core
from
paddle.fluid
import
framework
from
paddle.fluid.imperative
import
base
...
...
@@ -26,14 +26,33 @@ __all__ = ['Layer', 'PyLayer']
class
Layer
(
core
.
Layer
):
"""Layers composed of operators."""
"""Layers composed of operators.
Args:
name_scope: prefix name used by the layer to name parameters.
If prefix is "my_model/layer_1", parameter name in MyLayer
can be "my_model/layer_1/MyLayer/w_n", where w is the parameter
base name and n is an unique suffix auto-generated.
dtype: data type for the variables in the layer.
"""
def
__init__
(
self
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
,
name
=
None
):
def
__init__
(
self
,
name_scope
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
self
.
_full_name
=
unique_name
.
generate
(
name_scope
+
"/"
+
self
.
__class__
.
__name__
)
self
.
_built
=
False
self
.
_dtype
=
dtype
self
.
_parameters
=
collections
.
OrderedDict
()
self
.
_sub_layers
=
collections
.
OrderedDict
()
def
full_name
(
self
):
"""Full name for this layers.
Full name is composed by name_scope + "/" + MyLayer.__class__.__name__
Returns full name of this name.
"""
return
self
.
_full_name
def
parameters
(
self
,
include_sublayers
=
True
):
"""Returns a list of Parameters from current and sub-layers.
...
...
python/paddle/fluid/imperative/nn.py
浏览文件 @
006c32f9
...
...
@@ -27,6 +27,7 @@ __all__ = ['Conv2D', 'Pool2D', 'FC', 'BatchNorm', 'Embedding']
class
Conv2D
(
layers
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
filter_size
,
...
...
@@ -38,19 +39,17 @@ class Conv2D(layers.Layer):
act
=
None
,
param_attr
=
None
,
bias_attr
=
None
,
name
=
None
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
assert
param_attr
is
not
False
,
"param_attr should not be False here."
super
(
Conv2D
,
self
).
__init__
(
name
=
nam
e
,
dtype
=
dtype
)
super
(
Conv2D
,
self
).
__init__
(
name
_scop
e
,
dtype
=
dtype
)
# TODO(minqiyang): Move this to the top.
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
type
(
self
).
__name__
,
self
.
full_name
()
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
dtype
=
dtype
,
name
=
name
,
act
=
act
)
self
.
_groups
=
groups
...
...
@@ -143,6 +142,7 @@ class Conv2D(layers.Layer):
class
Pool2D
(
layers
.
Layer
):
def
__init__
(
self
,
name_scope
,
pool_size
=-
1
,
pool_type
=
"max"
,
pool_stride
=
1
,
...
...
@@ -151,7 +151,6 @@ class Pool2D(layers.Layer):
use_cudnn
=
True
,
ceil_mode
=
False
,
exclusive
=
True
,
name
=
None
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
if
pool_type
not
in
[
"max"
,
"avg"
]:
raise
ValueError
(
...
...
@@ -166,10 +165,10 @@ class Pool2D(layers.Layer):
if
not
isinstance
(
use_cudnn
,
bool
):
raise
ValueError
(
"use_cudnn should be True or False"
)
super
(
Pool2D
,
self
).
__init__
(
name
=
nam
e
,
dtype
=
dtype
)
super
(
Pool2D
,
self
).
__init__
(
name
_scop
e
,
dtype
=
dtype
)
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
type
(
self
).
__name__
,
dtype
=
dtype
,
name
=
nam
e
)
self
.
_helper
=
LayerHelper
(
self
.
full_name
(),
dtype
=
dtyp
e
)
self
.
_pool_type
=
pool_type
self
.
_pool_size
=
utils
.
convert_to_list
(
pool_size
,
2
,
'pool_size'
)
...
...
@@ -205,25 +204,24 @@ class Pool2D(layers.Layer):
class
FC
(
layers
.
Layer
):
def
__init__
(
self
,
name_scope
,
size
,
param_attr
=
None
,
bias_attr
=
None
,
num_flatten_dims
=
1
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
,
act
=
None
,
name
=
None
):
super
(
FC
,
self
).
__init__
()
act
=
None
):
super
(
FC
,
self
).
__init__
(
name_scope
)
self
.
_size
=
size
self
.
_num_flatten_dims
=
num_flatten_dims
self
.
_dtype
=
dtype
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'FC'
,
self
.
full_name
()
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
act
=
act
,
name
=
name
)
act
=
act
)
def
_build_once
(
self
,
input
):
input_shape
=
input
.
shape
...
...
@@ -282,6 +280,7 @@ class FC(layers.Layer):
class
BatchNorm
(
layers
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
act
=
None
,
is_test
=
False
,
...
...
@@ -292,22 +291,20 @@ class BatchNorm(layers.Layer):
dtype
=
core
.
VarDesc
.
VarType
.
FP32
,
data_layout
=
'NCHW'
,
in_place
=
False
,
name
=
None
,
moving_mean_name
=
None
,
moving_variance_name
=
None
,
do_model_average_for_mean_and_var
=
False
,
fuse_with_relu
=
False
,
use_global_stats
=
False
):
super
(
BatchNorm
,
self
).
__init__
()
super
(
BatchNorm
,
self
).
__init__
(
name_scope
)
assert
bias_attr
is
not
False
,
"bias_attr should not be False in batch_norm."
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'batch_norm'
,
self
.
full_name
()
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
name
=
name
,
act
=
act
)
if
dtype
==
core
.
VarDesc
.
VarType
.
FP16
:
...
...
@@ -419,6 +416,7 @@ class Embedding(layers.Layer):
constructor.
Args:
name_scope: See base class.
size(tuple|list): The shape of the look up table parameter. It should
have two elements which indicate the size of the dictionary of
embeddings and the size of each embedding vector respectively.
...
...
@@ -446,6 +444,7 @@ class Embedding(layers.Layer):
"""
def
__init__
(
self
,
name_scope
,
size
,
is_sparse
=
False
,
is_distributed
=
False
,
...
...
@@ -453,7 +452,7 @@ class Embedding(layers.Layer):
param_attr
=
None
,
dtype
=
'float32'
):
super
(
Embedding
,
self
).
__init__
()
super
(
Embedding
,
self
).
__init__
(
name_scope
)
self
.
_size
=
size
self
.
_is_sparse
=
is_sparse
self
.
_is_distributed
=
is_distributed
...
...
@@ -468,7 +467,7 @@ class Embedding(layers.Layer):
assert
self
.
_is_sparse
is
True
and
self
.
_is_distributed
is
False
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'embedding'
,
param_attr
=
param_attr
)
self
.
_helper
=
LayerHelper
(
self
.
full_name
()
,
param_attr
=
param_attr
)
self
.
_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_param_attr
,
shape
=
self
.
_size
,
...
...
python/paddle/fluid/layer_helper.py
浏览文件 @
006c32f9
...
...
@@ -34,6 +34,9 @@ class LayerHelper(object):
self
.
kwargs
=
kwargs
self
.
layer_type
=
layer_type
name
=
self
.
kwargs
.
get
(
'name'
,
None
)
# TODO(panyx0718, minqiyang): imperative mode
# can not use both `layer_type` and `name`. Deprecate LayerHelper
# and write a Helper for imperative mode.
if
name
is
None
:
self
.
kwargs
[
'name'
]
=
unique_name
.
generate
(
self
.
layer_type
)
...
...
python/paddle/fluid/tests/unittests/test_base_layer.py
浏览文件 @
006c32f9
...
...
@@ -20,10 +20,10 @@ from paddle.fluid.layer_helper import LayerHelper
class
L1
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
L1
,
self
).
__init__
()
def
__init__
(
self
,
prefix
):
super
(
L1
,
self
).
__init__
(
prefix
)
self
.
_helper
=
LayerHelper
(
'MyLayer'
,
self
.
full_name
()
,
param_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
...
...
@@ -43,20 +43,20 @@ class L1(fluid.imperative.Layer):
class
L2
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
L2
,
self
).
__init__
()
self
.
layer1
=
L1
()
self
.
layer2
=
L1
()
def
__init__
(
self
,
prefix
):
super
(
L2
,
self
).
__init__
(
prefix
)
self
.
layer1
=
L1
(
self
.
full_name
()
)
self
.
layer2
=
L1
(
self
.
full_name
()
)
def
forward
(
self
):
return
self
.
layer1
()
+
self
.
layer2
()
class
L3
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
L3
,
self
).
__init__
()
self
.
layer1
=
L2
()
self
.
layer2
=
L2
()
def
__init__
(
self
,
prefix
):
super
(
L3
,
self
).
__init__
(
prefix
)
self
.
layer1
=
L2
(
self
.
full_name
()
)
self
.
layer2
=
L2
(
self
.
full_name
()
)
def
forward
(
self
):
return
self
.
layer1
()
+
self
.
layer2
()
...
...
@@ -65,16 +65,23 @@ class L3(fluid.imperative.Layer):
class
TestBaseLayer
(
unittest
.
TestCase
):
def
test_one_level
(
self
):
with
fluid
.
imperative
.
guard
():
l
=
L1
()
l
=
L1
(
'test_one_level'
)
ret
=
l
()
self
.
assertEqual
(
l
.
w1
.
name
,
"
MyLayer
_0.w_0"
)
self
.
assertEqual
(
l
.
w2
.
name
,
"
MyLayer
_0.w_1"
)
self
.
assertEqual
(
l
.
w1
.
name
,
"
test_one_level/L1_0
_0.w_0"
)
self
.
assertEqual
(
l
.
w2
.
name
,
"
test_one_level/L1_0
_0.w_1"
)
self
.
assertTrue
(
np
.
allclose
(
ret
.
_numpy
(),
0.2
*
np
.
ones
([
2
,
2
])))
def
test_three_level
(
self
):
with
fluid
.
imperative
.
guard
():
l
=
L3
()
l
=
L3
(
'test_three_level'
)
names
=
[
p
.
name
for
p
in
l
.
parameters
()]
ret
=
l
()
self
.
assertEqual
(
names
[
0
],
"test_three_level/L3_0/L2_0/L1_0_0.w_0"
)
self
.
assertEqual
(
names
[
1
],
"test_three_level/L3_0/L2_0/L1_0_0.w_1"
)
self
.
assertEqual
(
names
[
2
],
"test_three_level/L3_0/L2_0/L1_1_0.w_0"
)
self
.
assertEqual
(
names
[
3
],
"test_three_level/L3_0/L2_0/L1_1_0.w_1"
)
self
.
assertEqual
(
names
[
4
],
"test_three_level/L3_0/L2_1/L1_0_0.w_0"
)
self
.
assertEqual
(
names
[
5
],
"test_three_level/L3_0/L2_1/L1_0_0.w_1"
)
self
.
assertTrue
(
np
.
allclose
(
ret
.
_numpy
(),
0.8
*
np
.
ones
([
2
,
2
])))
...
...
python/paddle/fluid/tests/unittests/test_imperative.py
浏览文件 @
006c32f9
...
...
@@ -15,7 +15,6 @@
import
contextlib
import
unittest
import
numpy
as
np
import
sys
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
...
...
@@ -24,8 +23,8 @@ from test_imperative_base import new_program_scope
class
MyLayer
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
MyLayer
,
self
).
__init__
()
def
__init__
(
self
,
name_scope
):
super
(
MyLayer
,
self
).
__init__
(
name_scope
)
def
forward
(
self
,
inputs
):
x
=
fluid
.
layers
.
relu
(
inputs
)
...
...
@@ -50,12 +49,14 @@ class MyPyLayer(fluid.imperative.PyLayer):
class
MLP
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
MLP
,
self
).
__init__
()
self
.
_fc1
=
FC
(
3
,
def
__init__
(
self
,
name_scope
):
super
(
MLP
,
self
).
__init__
(
name_scope
)
self
.
_fc1
=
FC
(
self
.
full_name
(),
3
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
self
.
_fc2
=
FC
(
4
,
self
.
_fc2
=
FC
(
self
.
full_name
(),
4
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
...
...
@@ -67,8 +68,9 @@ class MLP(fluid.imperative.Layer):
class
SimpleRNNCell
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
step_input_size
,
hidden_size
,
output_size
,
param_attr
):
super
(
SimpleRNNCell
,
self
).
__init__
()
def
__init__
(
self
,
name_scope
,
step_input_size
,
hidden_size
,
output_size
,
param_attr
):
super
(
SimpleRNNCell
,
self
).
__init__
(
name_scope
)
self
.
step_input_size
=
step_input_size
self
.
hidden_size
=
hidden_size
self
.
output_size
=
output_size
...
...
@@ -158,10 +160,11 @@ class SimpleRNNCell(fluid.imperative.Layer):
class
SimpleRNN
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
SimpleRNN
,
self
).
__init__
()
def
__init__
(
self
,
name_scope
):
super
(
SimpleRNN
,
self
).
__init__
(
name_scope
)
self
.
seq_len
=
4
self
.
_cell
=
SimpleRNNCell
(
self
.
full_name
(),
3
,
3
,
3
,
...
...
@@ -205,7 +208,7 @@ class TestImperative(unittest.TestCase):
with
fluid
.
imperative
.
guard
():
cl
=
core
.
Layer
()
cl
.
forward
([])
l
=
fluid
.
imperative
.
Layer
()
l
=
fluid
.
imperative
.
Layer
(
"l"
)
self
.
assertRaises
(
NotImplementedError
,
l
.
forward
,
[])
def
test_pylayer_func_id
(
self
):
...
...
@@ -281,7 +284,7 @@ class TestImperative(unittest.TestCase):
np_inp
=
np
.
array
([
1.0
,
2.0
,
-
1.0
],
dtype
=
np
.
float32
)
with
fluid
.
imperative
.
guard
():
var_inp
=
fluid
.
imperative
.
base
.
to_variable
(
np_inp
)
l
=
MyLayer
()
l
=
MyLayer
(
"my_layer"
)
x
=
l
(
var_inp
)[
0
]
self
.
assertIsNotNone
(
x
)
dy_out
=
x
.
_numpy
()
...
...
@@ -291,7 +294,7 @@ class TestImperative(unittest.TestCase):
with
new_program_scope
():
inp
=
fluid
.
layers
.
data
(
name
=
"inp"
,
shape
=
[
3
],
append_batch_size
=
False
)
l
=
MyLayer
()
l
=
MyLayer
(
"my_layer"
)
x
=
l
(
inp
)[
0
]
param_grads
=
fluid
.
backward
.
append_backward
(
x
,
parameter_list
=
[
l
.
_x_for_debug
.
name
])[
0
]
...
...
@@ -309,7 +312,7 @@ class TestImperative(unittest.TestCase):
np_inp
=
np
.
array
([[
1.0
,
2.0
],
[
3.0
,
4.0
]],
dtype
=
np
.
float32
)
with
fluid
.
imperative
.
guard
():
var_inp
=
fluid
.
imperative
.
base
.
to_variable
(
np_inp
)
mlp
=
MLP
()
mlp
=
MLP
(
"mlp"
)
out
=
mlp
(
var_inp
)
dy_out
=
out
.
_numpy
()
out
.
_backward
()
...
...
@@ -318,7 +321,7 @@ class TestImperative(unittest.TestCase):
with
new_program_scope
():
inp
=
fluid
.
layers
.
data
(
name
=
"inp"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
mlp
=
MLP
()
mlp
=
MLP
(
"mlp"
)
out
=
mlp
(
inp
)
param_grads
=
fluid
.
backward
.
append_backward
(
out
,
parameter_list
=
[
mlp
.
_fc1
.
_w
.
name
])[
0
]
...
...
@@ -334,10 +337,10 @@ class TestImperative(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
dy_grad
,
static_grad
))
params
=
mlp
.
parameters
(
True
)
self
.
assertEqual
(
"
FC
_0.w_0"
,
params
[
0
].
name
)
self
.
assertEqual
(
"
FC
_0.b_0"
,
params
[
1
].
name
)
self
.
assertEqual
(
"
FC_1
.w_0"
,
params
[
2
].
name
)
self
.
assertEqual
(
"
FC_1
.b_0"
,
params
[
3
].
name
)
self
.
assertEqual
(
"
mlp/MLP_0/FC_0
_0.w_0"
,
params
[
0
].
name
)
self
.
assertEqual
(
"
mlp/MLP_0/FC_0
_0.b_0"
,
params
[
1
].
name
)
self
.
assertEqual
(
"
mlp/MLP_0/FC_1_0
.w_0"
,
params
[
2
].
name
)
self
.
assertEqual
(
"
mlp/MLP_0/FC_1_0
.b_0"
,
params
[
3
].
name
)
self
.
assertEqual
(
len
(
params
),
4
)
sublayers
=
mlp
.
sublayers
(
True
)
...
...
@@ -353,7 +356,7 @@ class TestImperative(unittest.TestCase):
with
fluid
.
imperative
.
guard
():
var_inp
=
fluid
.
imperative
.
base
.
to_variable
(
np_inp
)
var_inp
=
fluid
.
layers
.
reshape
(
var_inp
,
shape
=
[
1
,
4
,
3
])
simple_rnn
=
SimpleRNN
()
simple_rnn
=
SimpleRNN
(
"simple_rnn"
)
outs
,
pre_hiddens
=
simple_rnn
.
forward
(
var_inp
)
dy_out
=
outs
[
3
].
_numpy
()
outs
[
3
].
_backward
()
...
...
@@ -364,7 +367,7 @@ class TestImperative(unittest.TestCase):
with
new_program_scope
():
inp
=
fluid
.
layers
.
data
(
name
=
"inp"
,
shape
=
[
1
,
4
,
3
],
append_batch_size
=
False
)
simple_rnn
=
SimpleRNN
()
simple_rnn
=
SimpleRNN
(
"simple_rnn"
)
outs
,
pre_hiddens
=
simple_rnn
(
inp
)
param_grads
=
fluid
.
backward
.
append_backward
(
outs
[
3
])
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
...
...
python/paddle/fluid/tests/unittests/test_imperative_gan.py
浏览文件 @
006c32f9
...
...
@@ -28,10 +28,10 @@ from paddle.fluid.imperative.base import to_variable
class
Discriminator
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
Discriminator
,
self
).
__init__
()
self
.
_fc1
=
FC
(
s
ize
=
32
,
act
=
'elu'
,
name
=
"d_fc1"
)
self
.
_fc2
=
FC
(
s
ize
=
1
,
name
=
"d_fc2"
)
def
__init__
(
self
,
name_scope
):
super
(
Discriminator
,
self
).
__init__
(
name_scope
)
self
.
_fc1
=
FC
(
s
elf
.
full_name
(),
size
=
32
,
act
=
'elu'
)
self
.
_fc2
=
FC
(
s
elf
.
full_name
(),
size
=
1
)
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
...
...
@@ -39,11 +39,11 @@ class Discriminator(fluid.imperative.Layer):
class
Generator
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
Generator
,
self
).
__init__
()
self
.
_fc1
=
FC
(
s
ize
=
64
,
act
=
'elu'
,
name
=
"g_fc1"
)
self
.
_fc2
=
FC
(
s
ize
=
64
,
act
=
'elu'
,
name
=
"g_fc2"
)
self
.
_fc3
=
FC
(
s
ize
=
1
,
name
=
"g_fc3"
)
def
__init__
(
self
,
name_scope
):
super
(
Generator
,
self
).
__init__
(
name_scope
)
self
.
_fc1
=
FC
(
s
elf
.
full_name
(),
size
=
64
,
act
=
'elu'
)
self
.
_fc2
=
FC
(
s
elf
.
full_name
(),
size
=
64
,
act
=
'elu'
)
self
.
_fc3
=
FC
(
s
elf
.
full_name
(),
size
=
1
)
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
...
...
@@ -65,8 +65,8 @@ class TestImperativeMnist(unittest.TestCase):
scope
=
fluid
.
core
.
Scope
()
with
new_program_scope
(
main
=
discriminate_p
,
startup
=
startup
,
scope
=
scope
):
discriminator
=
Discriminator
()
generator
=
Generator
()
discriminator
=
Discriminator
(
"d"
)
generator
=
Generator
(
"g"
)
img
=
fluid
.
layers
.
data
(
name
=
"img"
,
shape
=
[
2
,
1
],
append_batch_size
=
False
)
...
...
@@ -93,8 +93,8 @@ class TestImperativeMnist(unittest.TestCase):
sgd
.
minimize
(
d_loss
)
with
new_program_scope
(
main
=
generate_p
,
startup
=
startup
,
scope
=
scope
):
discriminator
=
Discriminator
()
generator
=
Generator
()
discriminator
=
Discriminator
(
"d"
)
generator
=
Generator
(
"g"
)
noise
=
fluid
.
layers
.
data
(
name
=
"noise"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
...
...
@@ -134,8 +134,8 @@ class TestImperativeMnist(unittest.TestCase):
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
discriminator
=
Discriminator
()
generator
=
Generator
()
discriminator
=
Discriminator
(
"d"
)
generator
=
Generator
(
"g"
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
d_real
=
discriminator
(
to_variable
(
np
.
ones
([
2
,
1
],
np
.
float32
)))
...
...
python/paddle/fluid/tests/unittests/test_imperative_optimizer.py
浏览文件 @
006c32f9
...
...
@@ -28,6 +28,7 @@ from test_imperative_base import new_program_scope
class
SimpleImgConvPool
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
filter_size
,
...
...
@@ -44,9 +45,10 @@ class SimpleImgConvPool(fluid.imperative.Layer):
use_cudnn
=
False
,
param_attr
=
None
,
bias_attr
=
None
):
super
(
SimpleImgConvPool
,
self
).
__init__
()
super
(
SimpleImgConvPool
,
self
).
__init__
(
name_scope
)
self
.
_conv2d
=
Conv2D
(
self
.
full_name
(),
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
...
...
@@ -59,6 +61,7 @@ class SimpleImgConvPool(fluid.imperative.Layer):
use_cudnn
=
use_cudnn
)
self
.
_pool2d
=
Pool2D
(
self
.
full_name
(),
pool_size
=
pool_size
,
pool_type
=
pool_type
,
pool_stride
=
pool_stride
,
...
...
@@ -73,19 +76,20 @@ class SimpleImgConvPool(fluid.imperative.Layer):
class
MNIST
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
param_attr
=
None
,
bias_attr
=
None
):
super
(
MNIST
,
self
).
__init__
()
def
__init__
(
self
,
name_scope
,
param_attr
=
None
,
bias_attr
=
None
):
super
(
MNIST
,
self
).
__init__
(
name_scope
)
self
.
_simple_img_conv_pool_1
=
SimpleImgConvPool
(
1
,
20
,
5
,
2
,
2
,
act
=
"relu"
)
self
.
full_name
(),
1
,
20
,
5
,
2
,
2
,
act
=
"relu"
)
self
.
_simple_img_conv_pool_2
=
SimpleImgConvPool
(
20
,
50
,
5
,
2
,
2
,
act
=
"relu"
)
self
.
full_name
(),
20
,
50
,
5
,
2
,
2
,
act
=
"relu"
)
pool_2_shape
=
50
*
4
*
4
SIZE
=
10
scale
=
(
2.0
/
(
pool_2_shape
**
2
*
SIZE
))
**
0.5
self
.
_fc
=
FC
(
10
,
self
.
_fc
=
FC
(
self
.
full_name
(),
10
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
NormalInitializer
(
loc
=
0.0
,
scale
=
scale
)),
...
...
@@ -106,7 +110,7 @@ class TestImperativeMnist(unittest.TestCase):
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
mnist
=
MNIST
()
mnist
=
MNIST
(
"mnist"
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
128
)
...
...
@@ -150,7 +154,7 @@ class TestImperativeMnist(unittest.TestCase):
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
(
)
if
not
core
.
is_compiled_with_cuda
()
else
fluid
.
CUDAPlace
(
0
))
mnist
=
MNIST
()
mnist
=
MNIST
(
"mnist"
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
128
)
...
...
python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
浏览文件 @
006c32f9
...
...
@@ -28,12 +28,13 @@ from paddle.fluid.backward import append_backward
class
SimpleLSTMRNN
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
name_scope
,
hidden_size
,
num_steps
,
num_layers
=
2
,
init_scale
=
0.1
,
dropout
=
None
):
super
(
SimpleLSTMRNN
,
self
).
__init__
()
super
(
SimpleLSTMRNN
,
self
).
__init__
(
name_scope
)
self
.
_hidden_size
=
hidden_size
self
.
_num_layers
=
num_layers
self
.
_init_scale
=
init_scale
...
...
@@ -130,13 +131,14 @@ class SimpleLSTMRNN(fluid.imperative.Layer):
class
PtbModel
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
name_scope
,
hidden_size
,
vocab_size
,
num_layers
=
2
,
num_steps
=
20
,
init_scale
=
0.1
,
dropout
=
None
):
super
(
PtbModel
,
self
).
__init__
()
super
(
PtbModel
,
self
).
__init__
(
name_scope
)
self
.
hidden_size
=
hidden_size
self
.
vocab_size
=
vocab_size
self
.
init_scale
=
init_scale
...
...
@@ -146,12 +148,14 @@ class PtbModel(fluid.imperative.Layer):
from
paddle.fluid.layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'PtbModel'
,
act
=
"tanh"
)
self
.
simple_lstm_rnn
=
SimpleLSTMRNN
(
self
.
full_name
(),
hidden_size
,
num_steps
,
num_layers
=
num_layers
,
init_scale
=
init_scale
,
dropout
=
dropout
)
self
.
embedding
=
Embedding
(
self
.
full_name
(),
size
=
[
vocab_size
,
hidden_size
],
dtype
=
'float32'
,
is_sparse
=
False
,
...
...
@@ -226,6 +230,7 @@ class TestImperativePtbRnn(unittest.TestCase):
fluid
.
default_main_program
().
random_seed
=
seed
# TODO: marsyang1993 Change seed to
ptb_model
=
PtbModel
(
"ptb_model"
,
hidden_size
=
hidden_size
,
vocab_size
=
vocab_size
,
num_layers
=
num_layers
,
...
...
@@ -265,6 +270,7 @@ class TestImperativePtbRnn(unittest.TestCase):
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
ptb_model
=
PtbModel
(
"ptb_model"
,
hidden_size
=
hidden_size
,
vocab_size
=
vocab_size
,
num_layers
=
num_layers
,
...
...
python/paddle/fluid/tests/unittests/test_imperative_resnet.py
浏览文件 @
006c32f9
...
...
@@ -70,15 +70,17 @@ def optimizer_setting(params):
class
ConvBNLayer
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
filter_size
,
stride
=
1
,
groups
=
1
,
act
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
(
name_scope
)
self
.
_conv
=
Conv2D
(
self
.
full_name
(),
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
...
...
@@ -88,7 +90,7 @@ class ConvBNLayer(fluid.imperative.Layer):
act
=
None
,
bias_attr
=
None
)
self
.
_batch_norm
=
BatchNorm
(
num_filters
,
act
=
act
)
self
.
_batch_norm
=
BatchNorm
(
self
.
full_name
(),
num_filters
,
act
=
act
)
def
forward
(
self
,
inputs
):
y
=
self
.
_conv
(
inputs
)
...
...
@@ -98,21 +100,29 @@ class ConvBNLayer(fluid.imperative.Layer):
class
BottleneckBlock
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
,
stride
,
shortcut
=
True
):
super
(
BottleneckBlock
,
self
).
__init__
()
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
stride
,
shortcut
=
True
):
super
(
BottleneckBlock
,
self
).
__init__
(
name_scope
)
self
.
conv0
=
ConvBNLayer
(
self
.
full_name
(),
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
1
,
act
=
'relu'
)
self
.
conv1
=
ConvBNLayer
(
self
.
full_name
(),
num_channels
=
num_filters
,
num_filters
=
num_filters
,
filter_size
=
3
,
stride
=
stride
,
act
=
'relu'
)
self
.
conv2
=
ConvBNLayer
(
self
.
full_name
(),
num_channels
=
num_filters
,
num_filters
=
num_filters
*
4
,
filter_size
=
1
,
...
...
@@ -120,6 +130,7 @@ class BottleneckBlock(fluid.imperative.Layer):
if
not
shortcut
:
self
.
short
=
ConvBNLayer
(
self
.
full_name
(),
num_channels
=
num_channels
,
num_filters
=
num_filters
*
4
,
filter_size
=
1
,
...
...
@@ -141,13 +152,13 @@ class BottleneckBlock(fluid.imperative.Layer):
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
)
layer_helper
=
LayerHelper
(
'elementwise_add_activation'
,
act
=
'relu'
)
layer_helper
=
LayerHelper
(
self
.
full_name
()
,
act
=
'relu'
)
return
layer_helper
.
append_activation
(
y
)
class
ResNet
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
102
):
super
(
ResNet
,
self
).
__init__
()
def
__init__
(
self
,
name_scope
,
layers
=
50
,
class_dim
=
102
):
super
(
ResNet
,
self
).
__init__
(
name_scope
)
self
.
layers
=
layers
supported_layers
=
[
50
,
101
,
152
]
...
...
@@ -163,9 +174,18 @@ class ResNet(fluid.imperative.Layer):
num_filters
=
[
64
,
128
,
256
,
512
]
self
.
conv
=
ConvBNLayer
(
num_channels
=
3
,
num_filters
=
64
,
filter_size
=
7
,
stride
=
2
,
act
=
'relu'
)
self
.
full_name
(),
num_channels
=
3
,
num_filters
=
64
,
filter_size
=
7
,
stride
=
2
,
act
=
'relu'
)
self
.
pool2d_max
=
Pool2D
(
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
full_name
(),
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
bottleneck_block_list
=
[]
num_channels
=
64
...
...
@@ -175,6 +195,7 @@ class ResNet(fluid.imperative.Layer):
bottleneck_block
=
self
.
add_sublayer
(
'bb_%d_%d'
%
(
block
,
i
),
BottleneckBlock
(
self
.
full_name
(),
num_channels
=
num_channels
,
num_filters
=
num_filters
[
block
],
stride
=
2
if
i
==
0
and
block
!=
0
else
1
,
...
...
@@ -184,12 +205,13 @@ class ResNet(fluid.imperative.Layer):
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
full_name
(),
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
import
math
stdv
=
1.0
/
math
.
sqrt
(
2048
*
1.0
)
self
.
out
=
FC
(
size
=
class_dim
,
self
.
out
=
FC
(
self
.
full_name
(),
size
=
class_dim
,
act
=
'softmax'
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)))
...
...
@@ -214,7 +236,7 @@ class TestImperativeResnet(unittest.TestCase):
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
resnet
=
ResNet
()
resnet
=
ResNet
(
"resnet"
)
optimizer
=
optimizer_setting
(
train_parameters
)
np
.
random
.
seed
(
seed
)
import
random
...
...
@@ -275,7 +297,7 @@ class TestImperativeResnet(unittest.TestCase):
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
(
)
if
not
core
.
is_compiled_with_cuda
()
else
fluid
.
CUDAPlace
(
0
))
resnet
=
ResNet
()
resnet
=
ResNet
(
"resnet"
)
optimizer
=
optimizer_setting
(
train_parameters
)
np
.
random
.
seed
(
seed
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录