提交 7770e2df 编写于 作者: W Wang Huan

modify doc for ParameterList and LayerList, test=develop

上级 09f19532
...@@ -99,15 +99,15 @@ class ParameterList(Layer): ...@@ -99,15 +99,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):
...@@ -124,19 +124,18 @@ class ParameterList(Layer): ...@@ -124,19 +124,18 @@ class ParameterList(Layer):
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 = fluid.layers.create_parameter(shape=[2, 3], dtype='float32') replaced_param = paddle.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(fluid.layers.create_parameter(shape=[3, 4], dtype='float32')) # append param model.params.append(paddle.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]
...@@ -183,14 +182,14 @@ class LayerList(Layer): ...@@ -183,14 +182,14 @@ 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
...@@ -239,11 +238,10 @@ class LayerList(Layer): ...@@ -239,11 +238,10 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)]) another = paddle.nn.Linear(10, 10)
another = fluid.dygraph.Linear(10, 10)
linears.append(another) linears.append(another)
print(len(linears)) # 11 print(len(linears)) # 11
""" """
...@@ -260,11 +258,10 @@ class LayerList(Layer): ...@@ -260,11 +258,10 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)]) another = paddle.nn.Linear(10, 10)
another = fluid.dygraph.Linear(10, 10)
linears.insert(3, another) linears.insert(3, another)
print(linears[3] is another) # True print(linears[3] is another) # True
""" """
...@@ -284,11 +281,10 @@ class LayerList(Layer): ...@@ -284,11 +281,10 @@ class LayerList(Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)]) another_list = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(5)])
another_list = fluid.dygraph.LayerList([fluid.dygraph.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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册