gaussian_random

paddle.fluid.layers.nn. gaussian_random ( shape, mean=0.0, std=1.0, seed=0, dtype='float32', name=None ) [source]

This OP returns a Tensor filled with random values sampled from a Gaussian distribution, with shape and dtype.

Parameters
  • shape (list|tuple|Tensor) – The shape of the output Tensor. If shape is a list or tuple, the elements of it should be integers or Tensors (with the shape [1], and the data type int32 or int64). If shape is a Tensor, it should be a 1-D Tensor(with the data type int32 or int64).

  • mean (float|int, optional) – Mean of the output tensor, default is 0.0.

  • std (float|int, optional) – Standard deviation of the output tensor, default is 1.0.

  • seed (int, optional) – (int, default 0) Random seed of generator.0 means use system wide seed.Note that if seed is not 0, this operator will always generate the same random numbers every time

  • dtype (str|np.dtype|core.VarDesc.VarType, optional) – The data type of the output Tensor. Supported data types: float32, float64. Default is float32.

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

Returns

A Tensor filled with random values sampled from a Gaussian distribution, with shape and dtype.

Return type

Tensor

Examples

import paddle
import paddle.fluid as fluid
paddle.enable_static()

# example 1:
# attr shape is a list which doesn't contain Tensor.
result_1 = fluid.layers.gaussian_random(shape=[3, 4])
# [[-0.31261674,  1.8736548,  -0.6274357,   0.96988016],
#  [-0.12294637,  0.9554768,   1.5690808,  -1.2894802 ],
#  [-0.60082096, -0.61138713,  1.5345167,  -0.21834975]]

# example 2:
# attr shape is a list which contains Tensor.
dim_1 = fluid.layers.fill_constant([1], "int64", 2)
dim_2 = fluid.layers.fill_constant([1], "int32", 3)
result_2 = fluid.layers.gaussian_random(shape=[dim_1, dim_2])
# [[ 0.51398206, -0.3389769,   0.23597084],
#  [ 1.0388143,  -1.2015356,  -1.0499583 ]]

# example 3:
# attr shape is a Tensor, the data type must be int64 or int32.
var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64")
result_3 = fluid.layers.gaussian_random(var_shape)
# if var_shape's value is [2, 3]
# result_3 is:
# [[-0.12310527,  0.8187662,   1.923219  ]
#  [ 0.70721835,  0.5210541,  -0.03214082]]
# declarative mode
# required: skiptest
import numpy as np
from paddle import fluid

x = fluid.layers.gaussian_random((2, 3), std=2., seed=10)

place = fluid.CPUPlace()
exe = fluid.Executor(place)
start = fluid.default_startup_program()
main = fluid.default_main_program()

exe.run(start)
x_np, = exe.run(main, feed={}, fetch_list=[x])

x_np
# array([[2.3060477, 2.676496 , 3.9911983],
#        [0.9990833, 2.8675377, 2.2279181]], dtype=float32)
# imperative mode
import numpy as np
from paddle import fluid
import paddle.fluid.dygraph as dg

place = fluid.CPUPlace()
with dg.guard(place) as g:
    x = fluid.layers.gaussian_random((2, 4), mean=2., dtype="float32", seed=10)
    x_np = x.numpy()
x_np
# array([[2.3060477 , 2.676496  , 3.9911983 , 0.9990833 ],
#        [2.8675377 , 2.2279181 , 0.79029655, 2.8447366 ]], dtype=float32)