Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
f41449e7
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看板
未验证
提交
f41449e7
编写于
2月 18, 2020
作者:
S
songyouwei
提交者:
GitHub
2月 18, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cherry-pick #22418 (#22642)
test=release/1.7, test=develop
上级
1cb19bf1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
53 addition
and
25 deletion
+53
-25
python/paddle/fluid/dygraph/layers.py
python/paddle/fluid/dygraph/layers.py
+40
-25
python/paddle/fluid/tests/unittests/test_imperative_basic.py
python/paddle/fluid/tests/unittests/test_imperative_basic.py
+13
-0
未找到文件。
python/paddle/fluid/dygraph/layers.py
浏览文件 @
f41449e7
...
...
@@ -150,15 +150,11 @@ class Layer(core.Layer):
Returns:
list of :ref:`api_guide_Variable_en` : a list of Parameters.
"""
ret
=
[
p
for
p
in
self
.
_parameters
.
values
()]
parameters_set
=
set
(
ret
)
if
include_sublayers
:
for
l
in
self
.
_sub_layers
.
values
():
for
p
in
l
.
parameters
(
include_sublayers
):
if
p
in
parameters_set
:
continue
parameters_set
.
add
(
p
)
ret
.
append
(
p
)
ret
=
[
param
for
_
,
param
in
self
.
named_parameters
(
include_sublayers
=
include_sublayers
)
]
return
ret
def
sublayers
(
self
,
include_sublayers
=
True
):
...
...
@@ -170,11 +166,11 @@ class Layer(core.Layer):
Returns:
list of Layer : a list of sub layers.
"""
ret
=
[
l
for
l
in
self
.
_sub_layers
.
values
()]
if
include_sublayers
:
for
l
in
self
.
_sub_layers
.
values
():
for
sub_l
in
l
.
sublayers
(
include_sublayers
):
ret
.
append
(
sub_l
)
ret
=
[
layer
for
_
,
layer
in
self
.
named_sublayers
(
include_sublayers
=
include_sublayers
)
]
return
ret
def
named_parameters
(
self
,
prefix
=
''
,
include_sublayers
=
True
):
...
...
@@ -349,7 +345,12 @@ class Layer(core.Layer):
Returns:
Parameter: the parameter passed in.
"""
assert
isinstance
(
parameter
,
framework
.
Parameter
)
if
parameter
is
None
:
self
.
_parameters
[
name
]
=
None
elif
not
isinstance
(
parameter
,
framework
.
Parameter
):
raise
TypeError
(
"parameter assignment requires Parameter or None, but got '{}'"
.
format
(
type
(
parameter
).
__name__
))
if
len
(
self
.
_loaddict_holder
)
>
0
:
assert
parameter
.
name
in
self
.
_loaddict_holder
,
"Parameter not found, Can't not find [ {} ] in stat_dict"
.
format
(
...
...
@@ -376,8 +377,8 @@ class Layer(core.Layer):
if
isinstance
(
getattr
(
type
(
self
),
name
,
None
),
property
):
object
.
__setattr__
(
self
,
name
,
value
)
params
=
self
.
__dict__
.
get
(
'_parameters'
,
None
)
if
isinstance
(
value
,
framework
.
Parameter
):
params
=
self
.
__dict__
.
get
(
'_parameters'
,
None
)
if
params
is
None
:
raise
ValueError
(
"super(YourLayer, self).__init__() should be called first"
)
...
...
@@ -389,16 +390,30 @@ class Layer(core.Layer):
_remove_if_exist
(
self
.
__dict__
,
self
.
_sub_layers
)
params
[
name
]
=
value
elif
isinstance
(
value
,
core
.
Layer
):
layers
=
self
.
__dict__
.
get
(
'_sub_layers'
,
None
)
if
layers
is
None
:
raise
ValueError
(
"super(YourLayer, self).__init__() should be called first"
)
_remove_if_exist
(
self
.
__dict__
,
self
.
_parameters
)
layers
[
name
]
=
value
elif
params
is
not
None
and
name
in
params
:
if
value
is
not
None
:
raise
TypeError
(
"assignment to parameter '{}' should be of type Parameter or None, but got '{}'"
.
format
(
name
,
type
(
value
).
__name__
))
params
[
name
]
=
None
else
:
object
.
__setattr__
(
self
,
name
,
value
)
layers
=
self
.
__dict__
.
get
(
'_sub_layers'
,
None
)
if
isinstance
(
value
,
core
.
Layer
):
if
layers
is
None
:
raise
ValueError
(
"super(YourLayer, self).__init__() should be called first"
)
_remove_if_exist
(
self
.
__dict__
,
self
.
_parameters
)
layers
[
name
]
=
value
elif
layers
is
not
None
and
name
in
layers
:
if
value
is
not
None
:
raise
TypeError
(
"assignment to sublayer '{}' should be of type Layer or None, but got '{}'"
.
format
(
name
,
type
(
value
).
__name__
))
layers
[
name
]
=
None
else
:
object
.
__setattr__
(
self
,
name
,
value
)
def
__delattr__
(
self
,
name
):
if
name
in
self
.
_parameters
:
...
...
python/paddle/fluid/tests/unittests/test_imperative_basic.py
浏览文件 @
f41449e7
...
...
@@ -497,6 +497,19 @@ class TestImperative(unittest.TestCase):
self
.
assertTrue
(
hasattr
(
layer
,
"test_attr"
))
self
.
assertEqual
(
layer
.
test_attr
,
1
)
my_layer
=
MyLayer
()
my_layer
.
w1
=
my_layer
.
create_parameter
([
3
,
3
])
my_layer
.
add_parameter
(
'w2'
,
None
)
self
.
assertEqual
(
len
(
my_layer
.
parameters
()),
1
)
self
.
assertRaises
(
TypeError
,
my_layer
.
__setattr__
,
'w1'
,
'str'
)
my_layer
.
w1
=
None
self
.
assertEqual
(
len
(
my_layer
.
parameters
()),
0
)
my_layer
.
l1
=
fluid
.
dygraph
.
Linear
(
3
,
3
)
self
.
assertEqual
(
len
(
my_layer
.
sublayers
()),
1
)
self
.
assertRaises
(
TypeError
,
my_layer
.
__setattr__
,
'l1'
,
'str'
)
my_layer
.
l1
=
None
self
.
assertEqual
(
len
(
my_layer
.
sublayers
()),
0
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录