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

modify doc for paddle.nn.Layer (#27624)

* modify doc for Layer, test=develop

* modify doc for Layer2, test=develop

* set dtype default value to float32, test=develop

* add example code for paddle.nn.Layer, test=develop

* set create_parameter and create_variable dtype default value to None, test=develop

* modify some example code, tet=develop

* refine, test=develop

* del no ues code, test=develop

* modify doc, example code, args, test=develop

* modify doc, test=develop
上级 7793fd28
...@@ -62,10 +62,6 @@ class HookRemoveHelper(object): ...@@ -62,10 +62,6 @@ class HookRemoveHelper(object):
class Layer(core.Layer): class Layer(core.Layer):
""" """
:alias_main: paddle.nn.Layer
:alias: paddle.nn.Layer
:old_api: paddle.fluid.dygraph.layers.Layer
Dynamic graph Layer based on OOD, includes the parameters of the layer, the structure of the forward graph and so on. Dynamic graph Layer based on OOD, includes the parameters of the layer, the structure of the forward graph and so on.
Parameters: Parameters:
...@@ -74,16 +70,16 @@ class Layer(core.Layer): ...@@ -74,16 +70,16 @@ class Layer(core.Layer):
can be "my_layer_0.w_n", where "w" is the parameter can be "my_layer_0.w_n", where "w" is the parameter
base name and "n" is an unique suffix auto-generated. base name and "n" is an unique suffix auto-generated.
If None, prefix name will be snake cased class name. Default: None. If None, prefix name will be snake cased class name. Default: None.
dtype(str or core.VarDesc.VarType, optional): data type of this parameter. dtype(str, optional): data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64", If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16". "int8", "int16", "int32", "int64", "uint8" or "uint16".
Default: ``core.VarDesc.VarType.FP32`` Default: "float32"
Returns: Returns:
None None
""" """
def __init__(self, name_scope=None, dtype=core.VarDesc.VarType.FP32): def __init__(self, name_scope=None, dtype="float32"):
self.training = True self.training = True
if name_scope is None: if name_scope is None:
name_scope = _convert_camel_to_snake(self.__class__.__name__) name_scope = _convert_camel_to_snake(self.__class__.__name__)
...@@ -110,6 +106,30 @@ class Layer(core.Layer): ...@@ -110,6 +106,30 @@ class Layer(core.Layer):
Returns: Returns:
None None
Example::
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self._linear = paddle.nn.Linear(1, 1)
self._dropout = paddle.nn.Dropout(p=0.5)
def forward(self, input):
temp = self._linear(input)
temp = self._dropout(temp)
return temp
x = paddle.randn([10, 1], 'float32')
mylayer = MyLayer()
mylayer.eval() # set mylayer._dropout to eval mode
out = mylayer(x)
mylayer.train() # set mylayer._dropout to train mode
out = mylayer(x)
""" """
# global setting # global setting
framework._dygraph_tracer().train_mode() framework._dygraph_tracer().train_mode()
...@@ -125,6 +145,29 @@ class Layer(core.Layer): ...@@ -125,6 +145,29 @@ class Layer(core.Layer):
Returns: Returns:
None None
Example::
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self._linear = paddle.nn.Linear(1, 1)
self._dropout = paddle.nn.Dropout(p=0.5)
def forward(self, input):
temp = self._linear(input)
temp = self._dropout(temp)
return temp
x = paddle.randn([10, 1], 'float32')
mylayer = MyLayer()
mylayer.eval() # set mylayer._dropout to eval mode
out = mylayer(x)
print(out)
""" """
# global setting # global setting
framework._dygraph_tracer().eval_mode() framework._dygraph_tracer().eval_mode()
...@@ -150,14 +193,12 @@ class Layer(core.Layer): ...@@ -150,14 +193,12 @@ class Layer(core.Layer):
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
paddle.disable_static()
net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
def init_weights(layer): def init_weights(layer):
if type(layer) == nn.Linear: if type(layer) == nn.Linear:
print('before init weight:', layer.weight.numpy()) print('before init weight:', layer.weight.numpy())
new_weight = paddle.fill_constant(layer.weight.shape, layer.weight.dtype, value=0.9) new_weight = paddle.full(shape=layer.weight.shape, dtype=layer.weight.dtype, fill_value=0.9)
layer.weight.set_value(new_weight) layer.weight.set_value(new_weight)
print('after init weight:', layer.weight.numpy()) print('after init weight:', layer.weight.numpy())
...@@ -177,6 +218,23 @@ class Layer(core.Layer): ...@@ -177,6 +218,23 @@ class Layer(core.Layer):
Returns: Returns:
str: full name of this layer. str: full name of this layer.
Example::
.. code-block:: python
import paddle
class LinearNet(paddle.nn.Layer):
def __init__(self):
super(LinearNet, self).__init__(name_scope = "demo_linear_net")
self._linear = paddle.nn.Linear(1, 1)
def forward(self, x):
return self._linear(x)
linear_net = LinearNet()
print(linear_net.full_name()) # demo_linear_net_0
""" """
return self._full_name return self._full_name
...@@ -197,7 +255,7 @@ class Layer(core.Layer): ...@@ -197,7 +255,7 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
# the forward_post_hook change the output of the layer: output = output * 2 # the forward_post_hook change the output of the layer: output = output * 2
...@@ -207,14 +265,13 @@ class Layer(core.Layer): ...@@ -207,14 +265,13 @@ class Layer(core.Layer):
# change the output # change the output
return output * 2 return output * 2
with fluid.dygraph.guard(): linear = paddle.nn.Linear(13, 5)
linear = fluid.Linear(13, 5, dtype="float32")
# register the hook # register the hook
forward_post_hook_handle = linear.register_forward_post_hook(forward_post_hook) forward_post_hook_handle = linear.register_forward_post_hook(forward_post_hook)
value1 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(26).reshape(2, 13).astype("float32")
in1 = fluid.dygraph.to_variable(value1) in1 = paddle.to_tensor(value1)
out0 = linear(in1) out0 = linear(in1)
...@@ -249,7 +306,7 @@ class Layer(core.Layer): ...@@ -249,7 +306,7 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
# the forward_post_hook change the input of the layer: input = input * 2 # the forward_post_hook change the input of the layer: input = input * 2
...@@ -260,21 +317,20 @@ class Layer(core.Layer): ...@@ -260,21 +317,20 @@ class Layer(core.Layer):
input_return = (input[0] * 2) input_return = (input[0] * 2)
return input_return return input_return
with fluid.dygraph.guard(): linear = paddle.nn.Linear(13, 5)
linear = fluid.Linear(13, 5, dtype="float32")
# register the hook # register the hook
forward_pre_hook_handle = linear.register_forward_pre_hook(forward_pre_hook) forward_pre_hook_handle = linear.register_forward_pre_hook(forward_pre_hook)
value0 = np.arange(26).reshape(2, 13).astype("float32") value0 = np.arange(26).reshape(2, 13).astype("float32")
in0 = fluid.dygraph.to_variable(value0) in0 = paddle.to_tensor(value0)
out0 = linear(in0) out0 = linear(in0)
# remove the hook # remove the hook
forward_pre_hook_handle.remove() forward_pre_hook_handle.remove()
value1 = value0 * 2 value1 = value0 * 2
in1 = fluid.dygraph.to_variable(value1) in1 = paddle.to_tensor(value1)
out1 = linear(in1) out1 = linear(in1)
# hook change the linear's input to input * 2, so out0 is equal to out1. # hook change the linear's input to input * 2, so out0 is equal to out1.
...@@ -294,17 +350,37 @@ class Layer(core.Layer): ...@@ -294,17 +350,37 @@ class Layer(core.Layer):
Parameters: Parameters:
shape(list): Shape of the parameter. shape(list): Shape of the parameter.
attr(ParamAttr, optional): Parameter attribute of weight. Please refer to :ref:`api_fluid_ParamAttr`. Default: None. attr(ParamAttr, optional): Parameter attribute of weight. Please refer to :ref:`api_paddle_ParamAttr`. Default: None.
dtype(str or core.VarDesc.VarType or str, optional): Data type of this parameter. dtype(str, optional): Data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64", If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16". Default: "float32". "int8", "int16", "int32", "int64", "uint8" or "uint16". Default: "float32".
is_bias(bool, optional): if this is a bias parameter. Default: False. is_bias(bool, optional): if this is a bias parameter. Default: False.
default_initializer(Initializer, optional): the default initializer for this parameter. default_initializer(Initializer, optional): the default initializer for this parameter.
If set None, default initializer will be set to :ref:`api_fluid_initializer_XavierInitializer` and :ref:`api_fluid_initializer_ConstantInitializer` If set None, default initializer will be set to paddle.nn.initializer.Xavier and paddle.nn.initializer.Constant
for non-bias and bias parameter, respectively. Default: None. for non-bias and bias parameter, respectively. Default: None.
Returns: Returns:
:ref:`api_guide_Variable_en` : created parameter. :Tensor, created parameter.
Examples:
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self._linear = paddle.nn.Linear(1, 1)
w_tmp = self.create_parameter([1,1])
self.add_parameter("w_tmp", w_tmp)
def forward(self, input):
return self._linear(input)
mylayer = MyLayer()
for name, param in mylayer.named_parameters():
print(name, param) # will print w_tmp,_linear.weight,_linear.bias
""" """
temp_attr = copy.deepcopy(attr) temp_attr = copy.deepcopy(attr)
if isinstance(temp_attr, six.string_types) and temp_attr == "": if isinstance(temp_attr, six.string_types) and temp_attr == "":
...@@ -313,24 +389,40 @@ class Layer(core.Layer): ...@@ -313,24 +389,40 @@ class Layer(core.Layer):
default_initializer) default_initializer)
# TODO: Add more parameter list when we need them # TODO: Add more parameter list when we need them
def create_variable(self, def create_variable(self, name=None, persistable=None, dtype=None):
name=None,
persistable=None,
dtype=None,
type=core.VarDesc.VarType.LOD_TENSOR):
"""Create Variable for this layer. """Create Variable for this layer.
Parameters: Parameters:
name(str, optional): name of the variable. Please refer to :ref:`api_guide_Name` . Default: None name(str, optional): name of the variable. Please refer to :ref:`api_guide_Name` . Default: None
persistable(bool, optional): if set this variable persistable. Default: False persistable(bool, optional): if set this variable persistable. Default: False
dtype(str or core.VarDesc.VarType, optional): data type of this parameter. dtype(str, optional): data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64", If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16". "int8", "int16", "int32", "int64", "uint8" or "uint16".
If set None, it will be ``core.VarDesc.VarType.FP32``. Default: None If set None, it will be "float32". Default: None
type(core.VarDesc.VarType, optional): type of the variable. No need to set this parameter. Default: ``core.VarDesc.VarType.LOD_TENSOR``
Returns: Returns:
:ref:`api_guide_Variable_en` : created Variable. Tensor, created Variable.
Examples:
.. code-block:: python
import paddle
class MyLinear(paddle.nn.Layer):
def __init__(self,
in_features,
out_features):
super(MyLinear, self).__init__()
self.linear = paddle.nn.Linear( 10, 10)
self.back_var = self.create_variable(name = "linear_tmp_0", dtype=self._dtype)
def forward(self, input):
out = self.linear(input)
paddle.assign( out, self.back_var)
return out
""" """
if name is not None: if name is not None:
var_name = ".".join([self._full_name, name]) var_name = ".".join([self._full_name, name])
...@@ -339,7 +431,10 @@ class Layer(core.Layer): ...@@ -339,7 +431,10 @@ class Layer(core.Layer):
[self._full_name, "_generated_var"])) [self._full_name, "_generated_var"]))
return self._helper.main_program.current_block().create_var( return self._helper.main_program.current_block().create_var(
name=var_name, persistable=persistable, dtype=dtype, type=type) name=var_name,
persistable=persistable,
dtype=dtype,
type=core.VarDesc.VarType.LOD_TENSOR)
def parameters(self, include_sublayers=True): def parameters(self, include_sublayers=True):
"""Returns a list of all Parameters from current layer and its sub-layers. """Returns a list of all Parameters from current layer and its sub-layers.
...@@ -348,7 +443,16 @@ class Layer(core.Layer): ...@@ -348,7 +443,16 @@ class Layer(core.Layer):
include_sublayers(bool, optional): Whether include the parameters of sublayers. If True, also include the parameters from sublayers. Default: True include_sublayers(bool, optional): Whether include the parameters of sublayers. If True, also include the parameters from sublayers. Default: True
Returns: Returns:
list of :ref:`api_guide_Variable_en` : a list of Parameters. list of Tensor : a list of Parameters.
Examples:
.. code-block:: python
import paddle
linear = paddle.nn.Linear(1,1)
print(linear.parameters()) # print linear_0.w_0 and linear_0.b_0
""" """
ret = [ ret = [
param param
...@@ -366,16 +470,15 @@ class Layer(core.Layer): ...@@ -366,16 +470,15 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linear1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) linear2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(linear1, linear2)
model = fluid.dygraph.Sequential(fc1, fc2)
layer_list = list(model.children()) layer_list = list(model.children())
print(layer_list) print(layer_list) # [<paddle.nn.layer.common.Linear object at 0x7f7b8113f830>, <paddle.nn.layer.common.Linear object at 0x7f7b8113f950>]
""" """
for _, layer in self.named_children(): for _, layer in self.named_children():
...@@ -391,14 +494,15 @@ class Layer(core.Layer): ...@@ -391,14 +494,15 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linear1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) linear2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(linear1, linear2)
model = fluid.dygraph.Sequential(fc1, fc2)
for prefix, layer in model.named_children(): for prefix, layer in model.named_children():
print(prefix, layer) print(prefix, layer)
# ('0', <paddle.nn.layer.common.Linear object at 0x7fb61ed85830>)
# ('1', <paddle.nn.layer.common.Linear object at 0x7fb61ed85950>)
""" """
memo = set() memo = set()
...@@ -415,6 +519,26 @@ class Layer(core.Layer): ...@@ -415,6 +519,26 @@ class Layer(core.Layer):
Returns: Returns:
list of Layer : a list of sub layers. list of Layer : a list of sub layers.
Examples:
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self._linear = paddle.nn.Linear(1, 1)
self._dropout = paddle.nn.Dropout(p=0.5)
def forward(self, input):
temp = self._linear(input)
temp = self._dropout(temp)
return temp
mylayer = MyLayer()
print(mylayer.sublayers()) # [<paddle.nn.layer.common.Linear object at 0x7f44b58977d0>, <paddle.nn.layer.common.Dropout object at 0x7f44b58978f0>]
""" """
ret = [ ret = [
layer layer
...@@ -438,12 +562,11 @@ class Layer(core.Layer): ...@@ -438,12 +562,11 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(fc1, fc2)
model = fluid.dygraph.Sequential(fc1, fc2)
for name, param in model.named_parameters(): for name, param in model.named_parameters():
print(name, param) print(name, param)
...@@ -483,12 +606,11 @@ class Layer(core.Layer): ...@@ -483,12 +606,11 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(fc1, fc2)
model = fluid.dygraph.Sequential(fc1, fc2)
for prefix, layer in model.named_sublayers(): for prefix, layer in model.named_sublayers():
print(prefix, layer) print(prefix, layer)
...@@ -510,11 +632,11 @@ class Layer(core.Layer): ...@@ -510,11 +632,11 @@ class Layer(core.Layer):
layers_set=layers_set): layers_set=layers_set):
yield p, l yield p, l
def register_buffer(self, name, variable, persistable=True): def register_buffer(self, name, tensor, persistable=True):
""" """
Registers a variable as buffer into the layer. Registers a tensor as buffer into the layer.
`buffer` is a non-parameteric variable and will not be updated by optimizer, `buffer` is a non-trainable tensor and will not be updated by optimizer,
but is necessary for evaluation and inference. For example, the mean and variance in BatchNorm layers. but is necessary for evaluation and inference. For example, the mean and variance in BatchNorm layers.
The registered buffer is persistable by default, and will be saved into The registered buffer is persistable by default, and will be saved into
`state_dict` alongside parameters. If set persistable=False, it registers `state_dict` alongside parameters. If set persistable=False, it registers
...@@ -525,7 +647,7 @@ class Layer(core.Layer): ...@@ -525,7 +647,7 @@ class Layer(core.Layer):
Parameters: Parameters:
name (string): name of the buffer. The buffer can be accessed name (string): name of the buffer. The buffer can be accessed
from this layer using the given name from this layer using the given name
variable (Variable): the variable to be registered as buffer. tensor (Tensor): the tensor to be registered as buffer.
persistable (bool): whether the buffer is part of this layer's persistable (bool): whether the buffer is part of this layer's
state_dict. state_dict.
...@@ -536,12 +658,11 @@ class Layer(core.Layer): ...@@ -536,12 +658,11 @@ class Layer(core.Layer):
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linear = paddle.nn.Linear(10, 3)
linear = fluid.Linear(10, 3)
value = np.array([0]).astype("float32") value = np.array([0]).astype("float32")
buffer = fluid.dygraph.to_variable(value) buffer = paddle.to_tensor(value)
linear.register_buffer("buf_name", buffer, persistable=True) linear.register_buffer("buf_name", buffer, persistable=True)
# get the buffer by attribute. # get the buffer by attribute.
...@@ -565,12 +686,12 @@ class Layer(core.Layer): ...@@ -565,12 +686,12 @@ class Layer(core.Layer):
raise KeyError("The name of buffer can not be empty.") raise KeyError("The name of buffer can not be empty.")
elif hasattr(self, name) and name not in self._buffers: elif hasattr(self, name) and name not in self._buffers:
raise KeyError("attribute '{}' already exists.".format(name)) raise KeyError("attribute '{}' already exists.".format(name))
elif variable is not None and not type(variable) == core.VarBase: elif tensor is not None and not type(tensor) == core.VarBase:
raise TypeError( raise TypeError(
"The registered buffer should be a core.VarBase, but received {}.". "The registered buffer should be a core.VarBase, but received {}.".
format(type(variable).__name__)) format(type(tensor).__name__))
else: else:
self._buffers[name] = variable self._buffers[name] = tensor
if persistable: if persistable:
self._non_persistable_buffer_names_set.discard(name) self._non_persistable_buffer_names_set.discard(name)
else: else:
...@@ -584,7 +705,21 @@ class Layer(core.Layer): ...@@ -584,7 +705,21 @@ class Layer(core.Layer):
include_sublayers(bool, optional): Whether include the buffers of sublayers. If True, also include the buffers from sublayers. Default: True include_sublayers(bool, optional): Whether include the buffers of sublayers. If True, also include the buffers from sublayers. Default: True
Returns: Returns:
list of :ref:`api_guide_Variable_en` : a list of buffers. list of Tensor : a list of buffers.
Examples:
.. code-block:: python
import numpy as np
import paddle
linear = paddle.nn.Linear(10, 3)
value = np.array([0]).astype("float32")
buffer = paddle.to_tensor(value)
linear.register_buffer("buf_name", buffer, persistable=True)
print(linear.buffers()) # == print([linear.buf_name])
""" """
ret = [ ret = [
buffer buffer
...@@ -595,7 +730,7 @@ class Layer(core.Layer): ...@@ -595,7 +730,7 @@ class Layer(core.Layer):
def named_buffers(self, prefix='', include_sublayers=True): def named_buffers(self, prefix='', include_sublayers=True):
""" """
Returns an iterator over all buffers in the Layer, yielding tuple of name and Variable. Returns an iterator over all buffers in the Layer, yielding tuple of name and Tensor.
Parameters: Parameters:
prefix(str, optional): Prefix to prepend to all buffer names. Default: ''. prefix(str, optional): Prefix to prepend to all buffer names. Default: ''.
...@@ -603,27 +738,26 @@ class Layer(core.Layer): ...@@ -603,27 +738,26 @@ class Layer(core.Layer):
If True, also include the named buffers from sublayers. Default: True. If True, also include the named buffers from sublayers. Default: True.
Yields: Yields:
(string, Variable): Tuple of name and Variable (string, Tensor): Tuple of name and tensor
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) buffer1 = paddle.to_tensor(np.array([0]).astype("float32"))
buffer1 = fluid.dygraph.to_variable(np.array([0]).astype("float32")) # register a tensor as buffer by specific `persistable`
# register a variable as buffer by specific `persistable`
fc1.register_buffer("buf_name_1", buffer1, persistable=True) fc1.register_buffer("buf_name_1", buffer1, persistable=True)
fc2 = fluid.Linear(3, 10) fc2 = paddle.nn.Linear(3, 10)
buffer2 = fluid.dygraph.to_variable(np.array([1]).astype("float32")) buffer2 = paddle.to_tensor(np.array([1]).astype("float32"))
# register a buffer by assigning an attribute with Variable. # register a buffer by assigning an attribute with Tensor.
# The `persistable` can only be False by this way. # The `persistable` can only be False by this way.
fc2.buf_name_2 = buffer2 fc2.buf_name_2 = buffer2
model = fluid.dygraph.Sequential(fc1, fc2) model = paddle.nn.Sequential(fc1, fc2)
# get all named buffers # get all named buffers
for name, buffer in model.named_buffers(): for name, buffer in model.named_buffers():
...@@ -654,18 +788,17 @@ class Layer(core.Layer): ...@@ -654,18 +788,17 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
with fluid.dygraph.guard():
value = np.arange(26).reshape(2, 13).astype("float32") value = np.arange(26).reshape(2, 13).astype("float32")
a = fluid.dygraph.to_variable(value) a = paddle.to_tensor(value)
linear = fluid.Linear(13, 5, dtype="float32") linear = paddle.nn.Linear(13, 5)
adam = fluid.optimizer.Adam(learning_rate=0.01, adam = paddle.optimizer.Adam(learning_rate=0.01,
parameter_list=linear.parameters()) parameters=linear.parameters())
out = linear(a) out = linear(a)
out.backward() out.backward()
adam.minimize(out) adam.step()
linear.clear_gradients() linear.clear_gradients()
""" """
...@@ -726,6 +859,32 @@ class Layer(core.Layer): ...@@ -726,6 +859,32 @@ class Layer(core.Layer):
sublayer(Layer): an instance of Layer. sublayer(Layer): an instance of Layer.
Returns: Returns:
Layer: the sublayer passed in. Layer: the sublayer passed in.
Examples:
.. code-block:: python
import paddle
class MySequential(paddle.nn.Layer):
def __init__(self, *layers):
super(MySequential, self).__init__()
if len(layers) > 0 and isinstance(layers[0], tuple):
for name, layer in layers:
self.add_sublayer(name, layer)
else:
for idx, layer in enumerate(layers):
self.add_sublayer(str(idx), layer)
def forward(self, input):
for layer in self._sub_layers.values():
input = layer(input)
return input
fc1 = paddle.nn.Linear(10, 3)
fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
model = MySequential(fc1, fc2)
for prefix, layer in model.named_sublayers():
print(prefix, layer)
""" """
assert isinstance(sublayer, core.Layer) assert isinstance(sublayer, core.Layer)
...@@ -742,6 +901,25 @@ class Layer(core.Layer): ...@@ -742,6 +901,25 @@ class Layer(core.Layer):
parameter(Parameter): an instance of Parameter. parameter(Parameter): an instance of Parameter.
Returns: Returns:
Parameter: the parameter passed in. Parameter: the parameter passed in.
Examples:
.. code-block:: python
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self._linear = paddle.nn.Linear(1, 1)
w_tmp = self.create_parameter([1,1])
self.add_parameter("w_tmp", w_tmp)
def forward(self, input):
return self._linear(input)
mylayer = MyLayer()
for name, param in mylayer.named_parameters():
print(name, param) # will print w_tmp,_linear.weight,_linear.bias
""" """
if '_parameters' not in self.__dict__: if '_parameters' not in self.__dict__:
raise RuntimeError( raise RuntimeError(
...@@ -871,19 +1049,18 @@ class Layer(core.Layer): ...@@ -871,19 +1049,18 @@ class Layer(core.Layer):
Return a list. Get all parameters, buffers(non-parameter variables), sublayers, method and attr of Layer. Return a list. Get all parameters, buffers(non-parameter variables), sublayers, method and attr of Layer.
Examples: Examples:
import paddle.fluid as fluid .. code-block:: python
import paddle
import numpy as np import numpy as np
fluid.dygraph.enable_dygraph() class Mylayer(paddle.nn.Layer):
class Mylayer(fluid.dygraph.Layer):
def __init__(self): def __init__(self):
super(Mylayer, self).__init__() super(Mylayer, self).__init__()
self.linear1 = fluid.dygraph.Linear(10, 10) self.linear1 = paddle.nn.Linear(10, 10)
self.linear2 = fluid.dygraph.Linear(5, 5) self.linear2 = paddle.nn.Linear(5, 5)
self.conv2d = fluid.dygraph.Conv2D(3, 2, 3) self.conv2d = paddle.nn.Conv2d(3, 2, 3)
self.embedding = fluid.dygraph.Embedding(size=[128, 16]) self.embedding = paddle.nn.Embedding(128, 16)
self.h_0 = fluid.dygraph.to_variable(np.zeros([10, 10]).astype('float32')) self.h_0 = paddle.to_tensor(np.zeros([10, 10]).astype('float32'))
mylayer = Mylayer() mylayer = Mylayer()
print(dir(mylayer)) print(dir(mylayer))
...@@ -918,12 +1095,12 @@ class Layer(core.Layer): ...@@ -918,12 +1095,12 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding([10, 10]) emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict() state_dict = emb.state_dict()
fluid.save_dygraph( state_dict, "paddle_dy") paddle.save( state_dict, "paddle_dy.pdparams")
''' '''
...@@ -968,15 +1145,11 @@ class Layer(core.Layer): ...@@ -968,15 +1145,11 @@ class Layer(core.Layer):
import paddle import paddle
paddle.disable_static()
emb = paddle.nn.Embedding(10, 10) emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict() state_dict = emb.state_dict()
paddle.save(state_dict, "paddle_dy.pdparams") paddle.save(state_dict, "paddle_dy.pdparams")
para_state_dict = paddle.load("paddle_dy.pdparams") para_state_dict = paddle.load("paddle_dy.pdparams")
emb.set_state_dict(para_state_dict) emb.set_state_dict(para_state_dict)
''' '''
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册