- paddle.nn.initializer. set_global_initializer ( weight_init, bias_init=None )
This API is used to set up global model parameter initializer in framework.
After this API is invoked, the global initializer will takes effect in subsequent code.
The model parameters include
bias. In the framework, they correspond to
paddle.ParamAttr, which is inherited from
paddle.Tensor, and is a persistable Variable. This API only takes effect for model parameters, not for variables created through apis such as api_fluid_layers_create_global_var , api_fluid_layers_create_tensor.
If the initializer is also set up by
bias_attrwhen creating a network layer, the global initializer setting here will not take effect because it has a lower priority.
If you want to cancel the global initializer in framework, please set global initializer to
weight_init (Initializer) – set the global initializer for
weightof model parameters.
bias_init (Initializer, optional) – set the global initializer for
biasof model parameters. Default: None.
import paddle import paddle.nn as nn nn.initializer.set_global_initializer(nn.initializer.Uniform(), nn.initializer.Constant()) x_var = paddle.uniform((2, 4, 8, 8), dtype='float32', min=-1., max=1.) # The weight of conv1 is initialized by Uniform # The bias of conv1 is initialized by Constant conv1 = nn.Conv2D(4, 6, (3, 3)) y_var1 = conv1(x_var) # If set param_attr/bias_attr too, global initializer will not take effect # The weight of conv2 is initialized by Xavier # The bias of conv2 is initialized by Normal conv2 = nn.Conv2D(4, 6, (3, 3), weight_attr=nn.initializer.XavierUniform(), bias_attr=nn.initializer.Normal()) y_var2 = conv2(x_var) # Cancel the global initializer in framework, it will takes effect in subsequent code nn.initializer.set_global_initializer(None)