TruncatedNormal

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

The truncated normal distribution (Gaussian distribution) initializer.

Parameters
  • mean (float, optional) – Mean of the normal distribution. Default is \(0.0\).

  • std (float, optional) – Standard deviation of the normal distribution. Default is \(1.0\).

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

A parameter initialized by truncated normal distribution (Gaussian distribution).

Examples

>>> 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 ]]])