randn_like

paddle. randn_like ( x, dtype=None, name=None ) [源代码]

返回一个与输入张量尺寸相同的张量,其元素服从均值为 0、方差为 1 的标准正态分布。

参数

  • x (Tensor) – 输入的多维 Tensor,数据类型可以是 float16,bfloat16,float32,float64,complex64,complex128。输出 Tensor 的形状和 x 相同。如果 dtype 为 None,则输出 Tensor 的数据类型与 x 相同。

  • dtype (str|np.dtype,可选) - 输出 Tensor 的数据类型,支持 float16,bfloat16,float32,float64,complex64,complex128。当该参数值为 None 时,输出 Tensor 的数据类型与输入 Tensor 的数据类型一致。默认值为 None。

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

返回

Tensor:服从均值为 0、方差为 1 的标准正态分布 Tensor,形状为 x.shape,数据类型为 dtype

代码示例

>>> import paddle

>>> # example 1:
>>> # dtype is None and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out1 = paddle.randn_like(x)
>>> print(out1)
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.51785558, -0.10632933]])
>>> print(out1.dtype)
paddle.float32

>>> # example 2:
>>> # dtype is None and the dtype of x is float64
>>> x = paddle.zeros((1,2)).astype("float64")
>>> out2 = paddle.randn_like(x)
>>> print(out2)
Tensor(shape=[1, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
[[ 0.64437317, -1.26898670]])
>>> print(out2.dtype)
paddle.float64

>>> # example 3:
>>> # dtype is float64 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out3 = paddle.randn_like(x, dtype="float64")
>>> print(out3)
Tensor(shape=[1, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
[[ 1.45264642, -1.33133914]])
>>> print(out3.dtype)
paddle.float64