Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
5394194e
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看板
未验证
提交
5394194e
编写于
3月 31, 2021
作者:
W
Wenyu
提交者:
GitHub
3月 31, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support minus-int idx to LayerList (#31750)
* support minus-int idx to LayerList * update layerlist test
上级
ef8323d4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
2 deletion
+32
-2
python/paddle/fluid/dygraph/container.py
python/paddle/fluid/dygraph/container.py
+20
-2
python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py
...id/tests/unittests/test_imperative_container_layerlist.py
+12
-0
未找到文件。
python/paddle/fluid/dygraph/container.py
浏览文件 @
5394194e
...
...
@@ -213,13 +213,25 @@ class LayerList(Layer):
for
idx
,
layer
in
enumerate
(
sublayers
):
self
.
add_sublayer
(
str
(
idx
),
layer
)
def
_get_abs_idx
(
self
,
idx
):
if
isinstance
(
idx
,
int
):
if
not
(
-
len
(
self
)
<=
idx
<
len
(
self
)):
raise
IndexError
(
'index {} is out of range, should be an integer in range [{}, {})'
.
format
(
idx
,
-
len
(
self
),
len
(
self
)))
if
idx
<
0
:
idx
+=
len
(
self
)
return
idx
def
__getitem__
(
self
,
idx
):
if
isinstance
(
idx
,
slice
):
return
self
.
__class__
(
list
(
self
.
_sub_layers
.
values
())[
idx
])
else
:
idx
=
self
.
_get_abs_idx
(
idx
)
return
self
.
_sub_layers
[
str
(
idx
)]
def
__setitem__
(
self
,
idx
,
sublayer
):
idx
=
self
.
_get_abs_idx
(
idx
)
return
setattr
(
self
,
str
(
idx
),
sublayer
)
def
__delitem__
(
self
,
idx
):
...
...
@@ -227,6 +239,7 @@ class LayerList(Layer):
for
k
in
range
(
len
(
self
.
_sub_layers
))[
idx
]:
delattr
(
self
,
str
(
k
))
else
:
idx
=
self
.
_get_abs_idx
(
idx
)
delattr
(
self
,
str
(
idx
))
str_indices
=
[
str
(
i
)
for
i
in
range
(
len
(
self
.
_sub_layers
))]
self
.
_sub_layers
=
OrderedDict
(
...
...
@@ -275,10 +288,15 @@ class LayerList(Layer):
another = paddle.nn.Linear(10, 10)
linears.insert(3, another)
print(linears[3] is another) # True
another = paddle.nn.Linear(10, 10)
linears.insert(-1, another)
print(linears[-2] is another) # True
"""
assert
isinstance
(
index
,
int
)
and
\
0
<=
index
<
len
(
self
.
_sub_layers
),
\
"index should be an integer in range [0, len(self))"
-
len
(
self
.
_sub_layers
)
<=
index
<
len
(
self
.
_sub_layers
),
\
"index should be an integer in range [{}, {})"
.
format
(
-
len
(
self
),
len
(
self
))
index
=
self
.
_get_abs_idx
(
index
)
for
i
in
range
(
len
(
self
.
_sub_layers
),
index
,
-
1
):
self
.
_sub_layers
[
str
(
i
)]
=
self
.
_sub_layers
[
str
(
i
-
1
)]
self
.
_sub_layers
[
str
(
index
)]
=
sublayer
...
...
python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py
浏览文件 @
5394194e
...
...
@@ -84,6 +84,18 @@ class TestImperativeContainer(unittest.TestCase):
self
.
assertListEqual
(
res8
.
shape
,
[
5
,
3
**
3
])
res8
.
backward
()
model4
=
MyLayer
(
layerlist
[:
3
])
model4
.
layerlist
[
-
1
]
=
fluid
.
dygraph
.
Linear
(
4
,
5
)
res9
=
model4
(
x
)
self
.
assertListEqual
(
res9
.
shape
,
[
5
,
5
])
del
model4
.
layerlist
[
-
1
]
res10
=
model4
(
x
)
self
.
assertListEqual
(
res10
.
shape
,
[
5
,
4
])
model4
.
layerlist
.
insert
(
-
1
,
fluid
.
dygraph
.
Linear
(
2
,
2
))
res11
=
model4
(
x
)
self
.
assertListEqual
(
res11
.
shape
,
[
5
,
4
])
res11
.
backward
()
def
test_layer_list
(
self
):
self
.
layer_list
(
True
)
self
.
layer_list
(
False
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录