randn

paddle. randn ( shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None, *, out: paddle.Tensor | None = None, device: PlaceLike | None = None, pin_memory: bool = False, requires_grad: bool = False ) → Tensor [source]
paddle. randn ( *size: int, out: paddle.Tensor | None = None, dtype: DTypeLike | None = None, device: PlaceLike | None = None, pin_memory: bool = False, requires_grad: bool = False ) → Tensor

Returns a Tensor filled with random values sampled from a standard normal distribution with mean 0 and standard deviation 1, with shape and dtype.

Parameters
  • shape (tuple|list|Tensor|*shape) – Shape of the Tensor to be created. The data type is int32 or int64 . If shape is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. If shape is an Tensor, it should be an 1-D Tensor which represents a list. If shape is *shape, directly pass integers as variable-length arguments (e.g., randn(2, 3)). alias: size.

  • dtype (str|np.dtype|paddle.dtype|None, optional) – The data type of the output Tensor. Supported data types: float16, bfloat16, float32, float64, complex64, complex128. Default is None, use global default dtype (see get_default_dtype for details).

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.

  • out (Tensor, optional) – The output tensor.

  • device (PlaceLike|None, optional) – The desired device of returned tensor.

  • pin_memory (bool, optional) – If set, return tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns

Tensor, A Tensor filled with random values sampled from a standard normal distribution with mean 0 and standard deviation 1, with shape and dtype.

Examples

>>> import paddle

>>> # example 1: attr shape is a list which doesn't contain Tensor.
>>> out1 = paddle.randn(shape=[2, 3])
>>> print(out1)
>>> 
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[-0.29270014, -0.02925120, -1.07807338],
 [ 1.19966674, -0.46673676, -0.18050613]])
>>> 

>>> # example 2: attr shape is a list which contains Tensor.
>>> dim1 = paddle.to_tensor(2, 'int64')
>>> dim2 = paddle.to_tensor(3, 'int32')
>>> out2 = paddle.randn(shape=[dim1, dim2, 2])
>>> print(out2)
>>> 
Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[-0.26019713,  0.54994684],
  [ 0.46403214, -1.41178775],
  [-0.15682915, -0.26639181]],
 [[ 0.01364388, -2.81676364],
  [ 0.86996621,  0.07524570],
  [ 0.21443737,  0.90938759]]])
>>> 

>>> # example 3: attr shape is a Tensor, the data type must be int64 or int32.
>>> shape_tensor = paddle.to_tensor([2, 3])
>>> out3 = paddle.randn(shape_tensor)
>>> print(out3)
>>> 
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.57575506, -1.60349274, -0.27124876],
 [ 1.08381045,  0.81270242, -0.26763600]])
>>> 

>>> # example 4: attr dtype is complex64.
>>> paddle.seed(200)
>>> shape_tensor = paddle.to_tensor([2, 3])
>>> out4 = paddle.randn(shape_tensor, dtype='complex64')
>>> print(out4)
Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
 [[ (0.13755313+0.09320746j),  (0.79550129-0.41801897j),
    (-0.67300206-0.09163689j)],
  [ (0.17453042-0.90028328j),  (0.16270922-1.30863023j),
    (0.94287461+0.06869461j)]])

>>> # example 5: attr shape is *shape (integers as variable-length arguments).
>>> paddle.seed(200)
>>> out5 = paddle.randn(2, 3)
>>> print(out5)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.19452949,  0.13181525,  1.12500870],
 [-0.59116811, -0.95176864, -0.12959413]])