未验证 提交 c826bcb2 编写于 作者: W wanghuancoder 提交者: GitHub

modify doc for ParameterList and LayerList (#27609)

* modify doc for ParameterList and LayerList, test=develop

* add empty line after code-block, test=develop
上级 aaa3ee65
......@@ -98,15 +98,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):
......@@ -118,27 +118,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):
......@@ -182,14 +181,15 @@ 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
......@@ -238,13 +238,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 = fluid.dygraph.Linear(10, 10)
linears.append(another)
print(len(linears)) # 11
import paddle
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
......@@ -259,13 +259,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 = fluid.dygraph.Linear(10, 10)
linears.insert(3, another)
print(linears[3] is another) # True
import paddle
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), \
......@@ -283,14 +283,14 @@ 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.
先完成此消息的编辑!
想要评论请 注册