Uniform

class paddle.nn.initializer. Uniform ( low=- 1.0, high=1.0, name=None ) [源代码]

均匀分布初始化方法。

参数

  • low (float,可选) - 均匀分布的下界,默认值为 \(-1.0\)

  • high (float,可选) - 均匀分布的上界,默认值为 \(1.0\)

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

返回

由均匀分布初始化的参数。

代码示例

>>> import paddle
>>> paddle.seed(1)
>>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
>>> weight_attr = paddle.framework.ParamAttr(
...     name="linear_weight",
...     initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5))
>>> bias_attr = paddle.framework.ParamAttr(
...     name="linear_bias",
...     initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5))
>>> 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,
[[-0.48212373,  0.26492310],
 [ 0.17605734, -0.45379421]])

>>> print(linear.bias)
Parameter containing:
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
[-0.11236754,  0.46462214])

>>> res = linear(data)
>>> print(res)
Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[[-0.41843393,  0.27575102]],
 [[-0.41843393,  0.27575102]],
 [[-0.41843393,  0.27575102]]])

使用本API的教程文档