未验证 提交 36153898 编写于 作者: C Candy2Tang 提交者: GitHub

[xdoctest][task 113] Reformat example code with google style in...

[xdoctest][task 113] Reformat example code with google style in python/paddle/nn/layer/container.py (#56229)

* [xdoctest][task 113] test=docs_preview

* test=document_fix

* test=document_fix
上级 14abff89
......@@ -25,7 +25,7 @@ __all__ = []
class LayerDict(Layer):
"""
LayerDict holds sublayers in the ordered dictionary, and sublayers it contains are properly registered.
Holded sublayers can be accessed like a regular ordered python dictionary.
Held sublayers can be accessed like a regular ordered python dictionary.
Parameters:
sublayers (LayerDict|OrderedDict|list[(key,Layer)...], optional): iterable of key/value pairs, the type of value is 'paddle.nn.Layer' .
......@@ -33,37 +33,37 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
import numpy as np
from collections import OrderedDict
>>> import paddle
>>> import numpy as np
>>> from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
layers_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> layers_dict = paddle.nn.LayerDict(sublayers=sublayers)
l = layers_dict['conv1d']
>>> l = layers_dict['conv1d']
for k in layers_dict:
l = layers_dict[k]
>>> for k in layers_dict:
... l = layers_dict[k]
...
>>> print(len(layers_dict))
3
len(layers_dict)
#3
>>> del layers_dict['conv2d']
>>> print(len(layers_dict))
2
del layers_dict['conv2d']
len(layers_dict)
#2
>>> conv1d = layers_dict.pop('conv1d')
>>> print(len(layers_dict))
1
conv1d = layers_dict.pop('conv1d')
len(layers_dict)
#1
layers_dict.clear()
len(layers_dict)
#0
>>> layers_dict.clear()
>>> print(len(layers_dict))
0
"""
......@@ -100,22 +100,22 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
from collections import OrderedDict
>>> import paddle
>>> from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
len(layer_dict)
#3
>>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> len(layer_dict)
3
layer_dict.clear()
len(layer_dict)
#0
>>> layer_dict.clear()
>>> len(layer_dict)
0
"""
self._sub_layers.clear()
......@@ -130,22 +130,22 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
from collections import OrderedDict
>>> import paddle
>>> from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
len(layer_dict)
#3
>>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> len(layer_dict)
3
layer_dict.pop('conv2d')
len(layer_dict)
#2
>>> layer_dict.pop('conv2d')
>>> len(layer_dict)
2
"""
v = self[key]
......@@ -162,22 +162,21 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> import paddle
>>> from collections import OrderedDict
layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
for k in layer_dict.keys():
print(k)
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
#conv1d
#conv2d
#conv3d
>>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> for k in layer_dict.keys():
... print(k)
conv1d
conv2d
conv3d
"""
return self._sub_layers.keys()
......@@ -192,22 +191,21 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
from collections import OrderedDict
>>> import paddle
>>> from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
for k, v in layer_dict.items():
print(k, ":", v)
#conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
#conv2d : Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
#conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
>>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> for k, v in layer_dict.items():
... print(f"{k}:", v)
conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
conv2d : Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
"""
return self._sub_layers.items()
......@@ -222,22 +220,21 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> import paddle
>>> from collections import OrderedDict
layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
for v in layer_dict.values():
print(v)
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
#Conv1D(3, 2, kernel_size=[3], data_format=NCL)
#Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
#Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
>>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> for v in layer_dict.values():
... print(v)
Conv1D(3, 2, kernel_size=[3], data_format=NCL)
Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
"""
return self._sub_layers.values()
......@@ -252,29 +249,29 @@ class LayerDict(Layer):
Examples:
.. code-block:: python
import paddle
from collections import OrderedDict
>>> import paddle
>>> from collections import OrderedDict
sublayers = OrderedDict([
('conv1d', paddle.nn.Conv1D(3, 2, 3)),
('conv2d', paddle.nn.Conv2D(3, 2, 3)),
('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
])
>>> sublayers = OrderedDict([
... ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
... ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
... ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])
new_sublayers = OrderedDict([
('relu', paddle.nn.ReLU()),
('conv2d', paddle.nn.Conv2D(4, 2, 4)),
])
layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
>>> new_sublayers = OrderedDict([
... ('relu', paddle.nn.ReLU()),
... ('conv2d', paddle.nn.Conv2D(4, 2, 4)),
>>> ])
>>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
layer_dict.update(new_sublayers)
>>> layer_dict.update(new_sublayers)
for k, v in layer_dict.items():
print(k, ":", v)
#conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
#conv2d : Conv2D(4, 2, kernel_size=[4, 4], data_format=NCHW)
#conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
#relu : ReLU()
>>> for k, v in layer_dict.items():
... print(f"{k}:", v)
conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
conv2d : Conv2D(4, 2, kernel_size=[4, 4], data_format=NCHW)
conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
relu : ReLU()
"""
......@@ -311,44 +308,49 @@ class ParameterList(Layer):
Examples:
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self, num_stacked_param):
super().__init__()
# create ParameterList with iterable Parameters
self.params = paddle.nn.ParameterList(
[paddle.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
x = paddle.uniform(shape=[5, 2], dtype='float32')
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]
>>> import paddle
>>> class MyLayer(paddle.nn.Layer):
... def __init__(self, num_stacked_param):
... super().__init__()
... # create ParameterList with iterable Parameters
... self.params = paddle.nn.ParameterList(
... [paddle.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
...
>>> x = paddle.uniform(shape=[5, 2], dtype='float32')
>>> 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):
......@@ -395,19 +397,19 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self):
super().__init__()
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
for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
>>> import paddle
>>> class MyLayer(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... 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
... for i, l in enumerate(self.linears):
... x = self.linears[i // 2](x) + l(x)
... return x
"""
def __init__(self, sublayers=None):
......@@ -467,12 +469,13 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle
>>> 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
>>> 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
......@@ -488,15 +491,17 @@ class LayerList(Layer):
Examples:
.. code-block:: python
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
another = paddle.nn.Linear(10, 10)
linears.insert(-1, another)
print(linears[-2] 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
>>> another = paddle.nn.Linear(10, 10)
>>> linears.insert(-1, another)
>>> print(linears[-2] is another)
True
"""
assert isinstance(index, int) and -len(self._sub_layers) <= index < len(
self._sub_layers
......@@ -519,13 +524,15 @@ class LayerList(Layer):
Examples:
.. code-block:: python
import paddle
>>> 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
>>> 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):
......@@ -545,24 +552,24 @@ class Sequential(Layer):
Examples:
.. code-block:: python
import paddle
data = paddle.uniform(shape=[30, 10], dtype='float32')
# create Sequential with iterable Layers
model1 = paddle.nn.Sequential(
paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2)
)
model1[0] # access the first layer
res1 = model1(data) # sequential execution
# create Sequential with name Layer pairs
model2 = paddle.nn.Sequential(
('l1', paddle.nn.Linear(10, 2)),
('l2', paddle.nn.Linear(2, 3))
)
model2['l1'] # access l1 layer
model2.add_sublayer('l3', paddle.nn.Linear(3, 3)) # add sublayer
res2 = model2(data) # sequential execution
>>> import paddle
>>> data = paddle.uniform(shape=[30, 10], dtype='float32')
>>> # create Sequential with iterable Layers
>>> model1 = paddle.nn.Sequential(
... paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2)
>>> )
>>> model1[0] # access the first layer
>>> res1 = model1(data) # sequential execution
>>> # create Sequential with name Layer pairs
>>> model2 = paddle.nn.Sequential(
... ('l1', paddle.nn.Linear(10, 2)),
... ('l2', paddle.nn.Linear(2, 3))
>>> )
>>> model2['l1'] # access l1 layer
>>> model2.add_sublayer('l3', paddle.nn.Linear(3, 3)) # add sublayer
>>> res2 = model2(data) # sequential execution
"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册