Layer

class paddle.fluid.dygraph.Layer(name_scope, dtype=VarType.FP32)[source]

Dynamic graph Layer based on OOD, includes the parameters of the layer, the structure of the forward graph and so on.

Parameters
  • name_scope (str) – prefix name used by the layer to name parameters. If prefix is “my_model/layer_1”, parameter name in MyLayer can be “my_model/layer_1/MyLayer/w_n”, where w is the parameter base name and n is an unique suffix auto-generated.

  • dtype (str or core.VarDesc.VarType, optional) – data type of this parameter. If set str, it can be “bool”, “float16”, “float32”, “float64”, “int8”, “int16”, “int32”, “int64”, “uint8” or “uint16”. Default: core.VarDesc.VarType.FP32

Returns

None

full_name()

Full name for this layer, composed by name_scope + “/” + MyLayer.__class__.__name__

Returns

full name of this layer.

Return type

str

create_parameter(attr, shape, dtype, is_bias=False, default_initializer=None)

Create parameters for this layer.

Parameters
  • attr (ParamAttr) – Parameter attribute of weight. Please refer to ParamAttr

  • shape (list) – shape of the parameter

  • dtype (str or core.VarDesc.VarType) – data type of this parameter. If set str, it can be “bool”, “float16”, “float32”, “float64”, “int8”, “int16”, “int32”, “int64”, “uint8” or “uint16”.

  • is_bias (bool, optional) – if this is a bias parameter. Default: False

  • default_initializer (Initializer, optional) – the default initializer for this parameter. If set None, default initializer will be set to XavierInitializer and ConstantInitializer for non-bias and bias parameter, respectively. Default: None

Returns

created parameter.

Return type

Variable

create_variable(name=None, persistable=None, dtype=None, type=VarType.LOD_TENSOR)

Create Variable for this layer.

Parameters
  • name (str, optional) – name of the variable. Please refer to Name . Default: None

  • persistable (bool, optional) – if set this variable persistable. Default: False

  • dtype (str or core.VarDesc.VarType, optional) – data type of this parameter. If set str, it can be “bool”, “float16”, “float32”, “float64”, “int8”, “int16”, “int32”, “int64”, “uint8” or “uint16”. If set None, it will be core.VarDesc.VarType.FP32. Default: None

  • type (core.VarDesc.VarType, optional) – type of the variable. No need to set this parameter. Default: core.VarDesc.VarType.LOD_TENSOR

Returns

created Variable.

Return type

Variable

parameters(include_sublayers=True)

Returns a list of all Parameters from current layer and its sub-layers.

Parameters

include_sublayers (bool, optional) – Whether include the parameters of sublayers. If True, also include the parameters from sublayers. Default: True

Returns

a list of Parameters.

Return type

list of Variable

sublayers(include_sublayers=True)

Returns a list of sub layers.

Parameters

include_sublayers (bool, optional) – Whether return the sublayers of sublayers. If True, also include the sublayers of sublayers. Default: True

Returns

a list of sub layers.

Return type

list of Layer

forward(*inputs, **kwargs)

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments

add_sublayer(name, sublayer)

Adds a sub Layer instance.

Added sublayer can be accessed by self.name

Parameters
  • name (str) – name of this sublayer.

  • sublayer (Layer) – an instance of Layer.

Returns

the sublayer passed in.

Return type

Layer

add_parameter(name, parameter)

Adds a Parameter instance.

Added parameter can be accessed by self.name

Parameters
  • name (str) – name of this sublayer.

  • parameter (Parameter) – an instance of Parameter.

Returns

the parameter passed in.

Return type

Parameter

state_dict(destination=None, include_sublayers=True)

Get all parameters of current layer and its sub-layers. And set all the parameters into a dict

Parameters
  • destination (dict, optional) – If provide, all the parameters will set to this dict . Default: None

  • include_sublayers (bool, optional) – If true, also include the parameters from sublayers. Default: True

Retruns:

dict: a dict contains all the parameters

Examples

import paddle.fluid as fluid
with fluid.dygraph.guard():
    emb = fluid.dygraph.Embedding( "emb", [10, 10])

    state_dict = emb.state_dict()
    fluid.save_dygraph( state_dict, "paddle_dy")
set_dict(stat_dict, include_sublayers=True)

Set parameters from stat_dict. All the parameters will be reset by the tensor in the stat_dict

Parameters
  • state_dict (dict) – Dict contains all the parameters

  • include_sublayers (bool, optional) – If true, also include the parameters from sublayers. Default: True

Returns

None

Examples

import paddle.fluid as fluid
with fluid.dygraph.guard():
    emb = fluid.dygraph.Embedding( "emb", [10, 10])

    state_dict = emb.state_dict()
    fluid.save_dygraph( state_dict, "paddle_dy")

    para_state_dict, _ = fluid.load_dygraph( "paddle_dy")

    emb.set_dict( para_state_dict )
load_dict(stat_dict, include_sublayers=True)

Set parameters from stat_dict. All the parameters will be reset by the tensor in the stat_dict

This api will be Deprecated. Please use set_dict

Parameters
  • state_dict (dict) – Dict contains all the parameters

  • include_sublayers (bool, optional) – If true, also include the parameters from sublayers. Default: True

Returns

None

Examples

import paddle.fluid as fluid
with fluid.dygraph.guard():
    emb = fluid.dygraph.Embedding( "emb", [10, 10])

    state_dict = emb.state_dict()
    fluid.save_dygraph( state_dict, "paddle_dy")

    para_state_dict, _ = fluid.load_dygraph( "paddle_dy")

    emb.load_dict( para_state_dict )