TruncatedNormal

class paddle.nn.initializer. TruncatedNormal ( mean=0.0, std=1.0, name=None ) [源代码]

截断正态分布(高斯分布)初始化方法。

参数
  • mean (float,可选) - 正态分布的均值,默认值为 \(0.0\)

  • std (float,可选) - 正态分布的标准差,默认值为 \(1.0\)

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

由截断正态分布(高斯分布)初始化的参数。

代码示例

>>> import paddle

>>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
>>> weight_attr = paddle.framework.ParamAttr(
...     name="linear_weight",
...     initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
>>> bias_attr = paddle.framework.ParamAttr(
...     name="linear_bias",
...     initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
>>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr)
>>> print(linear.weight)
Parameter containing:
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[-1.0981836  1.4140984],
 [ 3.1390522 -2.8266568]])
>>> print(linear.bias)
Parameter containing:
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
[ -2.1546738  -1.6570673])
>>> res = linear(data)
>>> print(res)
Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[[-0.11380529 -3.0696259 ]],
 [[-0.11380529 -3.0696259 ]],
 [[-0.11380529 -3.0696259 ]]])