未验证 提交 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): ...@@ -98,15 +98,15 @@ class ParameterList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
class MyLayer(fluid.Layer): class MyLayer(paddle.nn.Layer):
def __init__(self, num_stacked_param): def __init__(self, num_stacked_param):
super(MyLayer, self).__init__() super(MyLayer, self).__init__()
# create ParameterList with iterable Parameters # create ParameterList with iterable Parameters
self.params = fluid.dygraph.ParameterList( self.params = paddle.nn.ParameterList(
[fluid.layers.create_parameter( [paddle.create_parameter(
shape=[2, 2], dtype='float32')] * num_stacked_param) shape=[2, 2], dtype='float32')] * num_stacked_param)
def forward(self, x): def forward(self, x):
...@@ -118,27 +118,26 @@ class ParameterList(Layer): ...@@ -118,27 +118,26 @@ class ParameterList(Layer):
"Y": p}, "Y": p},
outputs={"Out": tmp}, outputs={"Out": tmp},
attrs={"x_num_col_dims": 1, attrs={"x_num_col_dims": 1,
"y_num_col_dims": 1}) "y_num_col_dims": 1})
x = tmp x = tmp
return x return x
data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32') data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32')
with fluid.dygraph.guard(): x = paddle.to_tensor(data_np)
x = fluid.dygraph.to_variable(data_np) num_stacked_param = 4
num_stacked_param = 4 model = MyLayer(num_stacked_param)
model = MyLayer(num_stacked_param) print(len(model.params)) # 4
print(len(model.params)) # 4 res = model(x)
res = model(x) print(res.shape) # [5, 2]
print(res.shape) # [5, 2]
replaced_param = paddle.create_parameter(shape=[2, 3], dtype='float32')
replaced_param = fluid.layers.create_parameter(shape=[2, 3], dtype='float32') model.params[num_stacked_param - 1] = replaced_param # replace last param
model.params[num_stacked_param - 1] = replaced_param # replace last param res = model(x)
res = model(x) print(res.shape) # [5, 3]
print(res.shape) # [5, 3] model.params.append(paddle.create_parameter(shape=[3, 4], dtype='float32')) # append param
model.params.append(fluid.layers.create_parameter(shape=[3, 4], dtype='float32')) # append param print(len(model.params)) # 5
print(len(model.params)) # 5 res = model(x)
res = model(x) print(res.shape) # [5, 4]
print(res.shape) # [5, 4]
""" """
def __init__(self, parameters=None): def __init__(self, parameters=None):
...@@ -182,14 +181,15 @@ class LayerList(Layer): ...@@ -182,14 +181,15 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid
import paddle
import numpy as np import numpy as np
class MyLayer(fluid.Layer): class MyLayer(paddle.nn.Layer):
def __init__(self): def __init__(self):
super(MyLayer, self).__init__() super(MyLayer, self).__init__()
self.linears = fluid.dygraph.LayerList( self.linears = paddle.nn.LayerList(
[fluid.dygraph.Linear(10, 10) for i in range(10)]) [paddle.nn.Linear(10, 10) for i in range(10)])
def forward(self, x): def forward(self, x):
# LayerList can act as an iterable, or be indexed using ints # LayerList can act as an iterable, or be indexed using ints
...@@ -238,13 +238,13 @@ class LayerList(Layer): ...@@ -238,13 +238,13 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid
with fluid.dygraph.guard(): import paddle
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
another = fluid.dygraph.Linear(10, 10) linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
linears.append(another) another = paddle.nn.Linear(10, 10)
print(len(linears)) # 11 linears.append(another)
print(len(linears)) # 11
""" """
self.add_sublayer(str(len(self)), sublayer) self.add_sublayer(str(len(self)), sublayer)
return self return self
...@@ -259,13 +259,13 @@ class LayerList(Layer): ...@@ -259,13 +259,13 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid
with fluid.dygraph.guard(): import paddle
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
another = fluid.dygraph.Linear(10, 10) linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
linears.insert(3, another) another = paddle.nn.Linear(10, 10)
print(linears[3] is another) # True linears.insert(3, another)
print(linears[3] is another) # True
""" """
assert isinstance(index, int) and \ assert isinstance(index, int) and \
0 <= index < len(self._sub_layers), \ 0 <= index < len(self._sub_layers), \
...@@ -283,14 +283,14 @@ class LayerList(Layer): ...@@ -283,14 +283,14 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. 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)]) linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another_list = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(5)]) another_list = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(5)])
linears.extend(another_list) linears.extend(another_list)
print(len(linears)) # 15 print(len(linears)) # 15
print(another_list[0] is linears[10]) # True print(another_list[0] is linears[10]) # True
""" """
offset = len(self) offset = len(self)
for i, sublayer in enumerate(sublayers): for i, sublayer in enumerate(sublayers):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册