提交 d6b028b2 编写于 作者: S songyouwei 提交者: zhongpu

Add Sequential and ParameterList containers (#1676)

* add Sequential and ParameterList containers
test=develop

* update sample code of Sequential
test=develop

* update Sequential doc
test=develop
上级 d88bf1b7
......@@ -30,6 +30,7 @@ fluid.dygraph
dygraph_cn/NaturalExpDecay_cn.rst
dygraph_cn/NCE_cn.rst
dygraph_cn/NoamDecay_cn.rst
dygraph_cn/ParameterList_cn.rst
dygraph_cn/no_grad_cn.rst
dygraph_cn/PiecewiseDecay_cn.rst
dygraph_cn/PolynomialDecay_cn.rst
......@@ -37,6 +38,7 @@ fluid.dygraph
dygraph_cn/PRelu_cn.rst
dygraph_cn/prepare_context_cn.rst
dygraph_cn/save_dygraph_cn.rst
dygraph_cn/Sequential_cn.rst
dygraph_cn/SpectralNorm_cn.rst
dygraph_cn/to_variable_cn.rst
dygraph_cn/Tracer_cn.rst
......
.. _cn_api_fluid_dygraph_ParameterList:
ParameterList
-------------------------------
.. py:class:: paddle.fluid.dygraph.ParameterList(parameters=None)
参数列表容器。此容器的行为类似于Python列表,但它包含的参数将被正确地注册和添加。
参数:
- **parameters** (iterable,可选) - 可迭代的Parameters
返回:无
**代码示例**
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
class MyLayer(fluid.Layer):
def __init__(self, num_stacked_param):
super(MyLayer, self).__init__()
# 使用可迭代的 Parameters 创建 ParameterList
self.params = fluid.dygraph.ParameterList(
[fluid.layers.create_parameter(
shape=[2, 2], dtype='float32')] * num_stacked_param)
def forward(self, x):
for i, p in enumerate(self.params):
tmp = self._helper.create_variable_for_type_inference('float32')
self._helper.append_op(
type="mul",
inputs={"X": x,
"Y": p},
outputs={"Out": tmp},
attrs={"x_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 # 替换最后一个参数
res = model(x)
print(res.shape) # [5, 3]
model.params.append(fluid.layers.create_parameter(shape=[3, 4], dtype='float32')) # 添加参数
print(len(model.params)) # 5
res = model(x)
print(res.shape) # [5, 4]
.. _cn_api_fluid_dygraph_Sequential:
Sequential
-------------------------------
.. py:class:: paddle.fluid.dygraph.Sequential(*layers)
顺序容器。子Layer将按构造函数参数的顺序添加到此容器中。传递给构造函数的参数可以Layers或可迭代的name Layer元组。
参数:
- **layers** (tuple) - Layers或可迭代的name Layer对。
返回:无
**代码示例**
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
data = np.random.uniform(-1, 1, [30, 10]).astype('float32')
with fluid.dygraph.guard():
data = fluid.dygraph.to_variable(data)
# 使用 iterable Layers 创建 Sequential 容器
model1 = fluid.dygraph.Sequential(
fluid.Linear(10, 1), fluid.Linear(1, 2)
)
model1[0] # 访问第一个子层
res1 = model1(data) # 顺序执行
# 使用 iterable name Layer 对创建 Sequential 容器
model2 = fluid.dygraph.Sequential(
('l1', fluid.Linear(10, 2)),
('l2', fluid.Linear(2, 3))
)
model2['l1'] # 访问 l1 子层
model2.add_sublayer('l3', fluid.Linear(3, 3)) # 添加子层
res2 = model2(data) # 顺序执行
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册