Created by: songyouwei
Motivation
In dygraph, most Parameter
s of Layer
s are created when the __call__
is first invoked, thus part of the shape of the Parameter
s can be derived from the input, which is similar to paddle's static graph API.
This design pattern introduces many problems. The model depends on data, when a complex model (a subclass of layer) is inited, there are no parameters in it, and we can't load the previously saved checkpoint before a __call__
. It also brings troubles to the initialization of custom Parameters.
The creation of Layer
also requires the name_scope
parameter, which is unnecessary and troublesome for users.
This PR removes build_once
function and name_scope
parameter.
API changes
fluid.dygraph.Layer
-
name_scope
parameter is not required inLayer
's__init__
- simplify
create_parameter
api, onlyshape
is required
__init__
of fluid.dygraph.nn.*
- For all subclasses of
Layer
:name_scope
s are removed, all missing optionaldtype
parameters are added. -
Conv2D
,Conv2DTranspose
,Conv3D
andConv3DTranspose
: addnum_channels
(int): The number of channels in the input image. -
LayerNorm
: removebegin_norm_axis
, addnormalized_shape
(int or list or tuple): Input shape from an expected input of size :math:[*, normalized_shape[0], normalized_shape[1], ..., normalized_shape[-1]]
. If it is a single integer, this module will normalize over the last dimension which is expected to be of that specific size. -
NCE
: adddim
(int): Dimension of input (possibly embedding dim). -
PRelu
: addinput_shape
(list or tuple, optional): The shape of input. This parameter is required when mode is not "all". Default: None. -
BilinearTensorProduct
: removesize
. addinput1_dim
(int): The dimension of each first input. addinput2_dim
(int): The dimension of each second input. -
GroupNorm
: addchannels
(int): The number of channels of input. -
SpectralNorm
: addweight_shape
(list or tuple): The shape of weight parameter. -
TreeConv
: addfeature_size
(int): last dimension of nodes_vector.
Migrating
Layer
:
For existing subclasses of - remove
build_once
function, and moveParameter
creations into__init__
. - avoid positional arguments for
Layer.create_parameter
function, and onlyshape
is required.
Layer
's subclasses in dygraph models:
For constructions of - remove
name_scope
parameter of__init__
- add possible new parameters of
__init__
(these parameters are input shape related)
Example (Conv2D)
before
import paddle.fluid as fluid
import numpy as np
data = np.random.uniform(-1, 1, [10, 3, 32, 32]).astype('float32')
with fluid.dygraph.guard():
data = fluid.to_variable(data)
conv2d = fluid.Conv2D("conv2d", 2, 3)
conv = conv2d(data)
after
import paddle.fluid as fluid
import numpy as np
data = np.random.uniform(-1, 1, [10, 3, 32, 32]).astype('float32')
with fluid.dygraph.guard():
data = fluid.to_variable(data)
conv2d = fluid.Conv2D(3, 2, 3) # "conv2d" removed, and 3 is num_channels
conv = conv2d(data)