standard_normal

paddle. standard_normal ( shape, dtype=None, name=None ) [源代码]

返回符合标准正态分布(均值为 0,标准差为 1 的正态随机分布)的随机 Tensor,形状为 shape,数据类型为 dtype

参数

  • shape (list|tuple|Tensor) - 生成的随机 Tensor 的形状。如果 shape 是 list、tuple,则其中的元素可以是 int,或者是形状为[]且数据类型为 int32、int64 的 0-D Tensor。如果 shape 是 Tensor,则是数据类型为 int32、int64 的 1-D Tensor。

  • dtype (str|np.dtype,可选) - 输出 Tensor 的数据类型,支持 float32、float64。当该参数值为 None 时,输出 Tensor 的数据类型为 float32。默认值为 None。

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

返回

Tensor:符合标准正态分布的随机 Tensor,形状为 shape,数据类型为 dtype

示例代码

>>> import paddle

>>> # example 1: attr shape is a list which doesn't contain Tensor.
>>> out1 = paddle.standard_normal(shape=[2, 3])
>>> print(out1)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[-0.33719197, -0.25688133, -0.42868865],
 [-0.27804616, -0.25058213, -0.28209466]])

>>> # example 2: attr shape is a list which contains Tensor.
>>> dim1 = paddle.to_tensor(2, 'int64')
>>> dim2 = paddle.to_tensor(3, 'int32')
>>> out2 = paddle.standard_normal(shape=[dim1, dim2, 2])
>>> print(out2)
Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[ 0.81888396, -0.64831746],
  [ 1.28911388, -1.88154876],
  [-0.03271919, -0.32410008]],
 [[-0.20224631,  0.46683890],
  [ 1.91947734,  0.71657443],
  [ 0.33410960, -0.64256823]]])

>>> # example 3: attr shape is a Tensor, the data type must be int64 or int32.
>>> shape_tensor = paddle.to_tensor([2, 3])
>>> out3 = paddle.standard_normal(shape_tensor)
>>> print(out3)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.01182475, -0.44895259, -1.79227340],
 [ 1.52022707, -0.83830303,  0.05261501]])