gaussian_random¶
-
paddle.fluid.layers.
gaussian_random
(shape, mean=0.0, std=1.0, seed=0, dtype='float32')[source] Generate a random tensor whose data is drawn from a Gaussian distribution.
- Parameters
shape (tuple[int] | list[int] | Variable | list[Variable]) – Shape of the generated random tensor.
mean (float) – Mean of the random tensor, defaults to 0.0.
std (float) – Standard deviation of the random tensor, defaults to 1.0.
seed (int) – (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 (np.dtype | core.VarDesc.VarType | str) – Output data type, float32 or float64.
- Returns
Random tensor whose data is drawn from a Gaussian distribution, dtype: flaot32 or float64 as specified.
- Return type
Variable
Examples
import paddle.fluid as fluid # example 1: # attr shape is a list which doesn't contain tensor Variable. result_1 = fluid.layers.gaussian_random(shape=[3, 4]) # example 2: # attr shape is a list which contains tensor Variable. dim_1 = fluid.layers.fill_constant([1],"int64",3) dim_2 = fluid.layers.fill_constant([1],"int32",5) result_2 = fluid.layers.gaussian_random(shape=[dim_1, dim_2]) # example 3: # attr shape is a Variable, 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) var_shape_int32 = fluid.data(name='var_shape_int32', shape=[2], dtype="int32") result_4 = fluid.layers.gaussian_random(var_shape_int32)
# declarative mode 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)