Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
7770e2df
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看板
提交
7770e2df
编写于
9月 27, 2020
作者:
W
Wang Huan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify doc for ParameterList and LayerList, test=develop
上级
09f19532
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
41 addition
and
45 deletion
+41
-45
python/paddle/fluid/dygraph/container.py
python/paddle/fluid/dygraph/container.py
+41
-45
未找到文件。
python/paddle/fluid/dygraph/container.py
浏览文件 @
7770e2df
...
...
@@ -99,15 +99,15 @@ class ParameterList(Layer):
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import paddle
import numpy as np
class MyLayer(
fluid
.Layer):
class MyLayer(
paddle.nn
.Layer):
def __init__(self, num_stacked_param):
super(MyLayer, self).__init__()
# create ParameterList with iterable Parameters
self.params =
fluid.dygraph
.ParameterList(
[
fluid.layers
.create_parameter(
self.params =
paddle.nn
.ParameterList(
[
paddle
.create_parameter(
shape=[2, 2], dtype='float32')] * num_stacked_param)
def forward(self, x):
...
...
@@ -119,27 +119,26 @@ class ParameterList(Layer):
"Y": p},
outputs={"Out": tmp},
attrs={"x_num_col_dims": 1,
"y_num_col_dims": 1})
"y_num_col_dims": 1})
x = tmp
return x
data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32')
with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(data_np)
num_stacked_param = 4
model = MyLayer(num_stacked_param)
print(len(model.params)) # 4
res = model(x)
print(res.shape) # [5, 2]
replaced_param = fluid.layers.create_parameter(shape=[2, 3], dtype='float32')
model.params[num_stacked_param - 1] = replaced_param # replace last param
res = model(x)
print(res.shape) # [5, 3]
model.params.append(fluid.layers.create_parameter(shape=[3, 4], dtype='float32')) # append param
print(len(model.params)) # 5
res = model(x)
print(res.shape) # [5, 4]
x = paddle.to_tensor(data_np)
num_stacked_param = 4
model = MyLayer(num_stacked_param)
print(len(model.params)) # 4
res = model(x)
print(res.shape) # [5, 2]
replaced_param = paddle.create_parameter(shape=[2, 3], dtype='float32')
model.params[num_stacked_param - 1] = replaced_param # replace last param
res = model(x)
print(res.shape) # [5, 3]
model.params.append(paddle.create_parameter(shape=[3, 4], dtype='float32')) # append param
print(len(model.params)) # 5
res = model(x)
print(res.shape) # [5, 4]
"""
def
__init__
(
self
,
parameters
=
None
):
...
...
@@ -183,14 +182,14 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import paddle
import numpy as np
class MyLayer(
fluid
.Layer):
class MyLayer(
paddle.nn
.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self.linears =
fluid.dygraph
.LayerList(
[
fluid.dygraph
.Linear(10, 10) for i in range(10)])
self.linears =
paddle.nn
.LayerList(
[
paddle.nn
.Linear(10, 10) for i in range(10)])
def forward(self, x):
# LayerList can act as an iterable, or be indexed using ints
...
...
@@ -239,13 +238,12 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import paddle
with fluid.dygraph.guard():
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
another = fluid.dygraph.Linear(10, 10)
linears.append(another)
print(len(linears)) # 11
linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another = paddle.nn.Linear(10, 10)
linears.append(another)
print(len(linears)) # 11
"""
self
.
add_sublayer
(
str
(
len
(
self
)),
sublayer
)
return
self
...
...
@@ -260,13 +258,12 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import paddle
with fluid.dygraph.guard():
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
another = fluid.dygraph.Linear(10, 10)
linears.insert(3, another)
print(linears[3] is another) # True
linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another = paddle.nn.Linear(10, 10)
linears.insert(3, another)
print(linears[3] is another) # True
"""
assert
isinstance
(
index
,
int
)
and
\
0
<=
index
<
len
(
self
.
_sub_layers
),
\
...
...
@@ -284,14 +281,13 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle.fluid as fluid
with fluid.dygraph.guard():
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
another_list = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(5)])
linears.extend(another_list)
print(len(linears)) # 15
print(another_list[0] is linears[10]) # True
import paddle
linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another_list = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(5)])
linears.extend(another_list)
print(len(linears)) # 15
print(another_list[0] is linears[10]) # True
"""
offset
=
len
(
self
)
for
i
,
sublayer
in
enumerate
(
sublayers
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录