TruncatedNormal¶
- class paddle.nn.initializer. TruncatedNormal ( mean=0.0, std=1.0, a=- 2.0, b=2.0, name=None ) [source]
-
The truncated normal distribution (Gaussian distribution) initializer.
Note
It is better to set a <= mean <= b. If mean < a - 2*std or mean > b + 2*std, the distribution of values may be incorrect.
- 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\).
a (float, optional) – The minimum cutoff value. Default is -2.0.
b (float, optional) – The maximum cutoff value. Default is 2.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 ]]])