paddle.fluid.layers.randn(shape, out=None, dtype=None, device=None, stop_gradient=True, name=None)[source]

This function returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).

  • shape (list|tuple) – Shape of the generated random tensor.

  • out (Variable, optional) – Optional output which can be any created Variable that meets the requirements to store the result of operation. If the out is None, a new Variable wiil be returned to store the result. Default is None.

  • dtype (np.dtype|core.VarDesc.VarType|str, optional) – Data type of the output tensor, which can be float32, float64. if dtype is None , the data type of output tensor is float32 . Default is None.

  • device (str, optional) – Specific the output variable to be saved in cpu or gpu memory. Supported None, ‘cpu’, ‘gpu’. If it is None, the output variable will be automatically assigned devices. Default: None.

  • stop_gradient (bool, optional) – Indicating if we stop gradient from current(out) Variable. Default is True.

  • name (str, optional) – Normally there is no need for user to set this property. For more information, please refer to Name . Default is None.


Random tensor whose data is drawn from a Gaussian distribution, dtype: flaot32 or float64 as specified.

Return type:


  • TypeError – If the type of shape is not list or tuple.

  • TypeError – If the data type of dtype is not float32 or float64.

  • ValueError – If the length of shape is not bigger than 0.


# declarative mode
import paddle.fluid as fluid

data = fluid.layers.randn([2, 4])
place = fluid.CPUPlace()
exe = fluid.Executor(place)
res, = exe.run(fluid.default_main_program(), feed={}, fetch_list=[data])
# [[-1.4187592   0.7368311  -0.53748125 -0.0146909 ]
#  [-0.66294265 -1.3090698   0.1898754  -0.14065823]]
# imperative mode
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg

place = fluid.CPUPlace()
with dg.guard(place) as g:
    x = fluid.layers.randn([2, 4])
    x_np = x.numpy()
    # [[ 1.5149173  -0.26234224 -0.592486    1.4523455 ]
    #  [ 0.04581212 -0.85345626  1.1687907  -0.02512913]]