set_global_initializer¶
该 API 用于设置 Paddle 框架中全局的参数初始化方法。该 API 只对位于其后的代码生效。
模型参数为模型中的 weight 和 bias 统称,在框架中对应 paddle.ParamAttr 类,继承自 paddle.Tensor,是一种可持久化的 variable。 该 API 的设置仅对模型参数生效,对通过 create_global_var 、 cn_api_paddle_Tensor_create_tensor 等 API 创建的变量不会生效。
如果创建网络层时还通过 param_attr
、 bias_attr
设置了初始化方式,这里的全局设置将不会生效,因为其优先级更低。
参数¶
weight_init (Initializer) - 设置框架的全局的 weight 参数初始化方法。
bias_init (Initializer,可选) - 设置框架的全局的 bias 参数初始化方法。默认: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)