gaussian_random¶
- paddle.fluid.layers. gaussian_random ( shape, mean=0.0, std=1.0, seed=0, dtype='float32', name=None ) [源代码] ¶
该OP返回数值符合高斯随机分布的Tensor,形状为 shape
,数据类型为 dtype
。
参数¶
shape (list|tuple|Tensor) - 生成的随机Tensor的形状。如果
shape
是list、tuple,则其中的元素可以是int,或者是形状为[1]且数据类型为int32、int64的Tensor。如果shape
是Tensor,则是数据类型为int32、int64的1-D Tensor。mean (float|int,可选) - 输出Tensor的均值,支持的数据类型:float、int。默认值为0.0。
std (float|int,可选) - 输出Tensor的标准差,支持的数据类型:float、int。默认值为1.0。
seed (int,可选) - 随机数种子,默认值为 0。注:seed 设置为 0 表示使用系统的随机数种子。注意如果 seed 不为 0,则此算子每次将始终生成相同的随机数。
dtype (str|np.dtype|core.VarDesc.VarType,可选) - 输出Tensor的数据类型,支持float32、float64。默认值为float32。
name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。
返回¶
Tensor:符合高斯随机分布的Tensor,形状为
shape
,数据类型为dtype
。
抛出异常¶
TypeError
- 如果shape
的类型不是list、tuple、Tensor。
TypeError
- 如果dtype
不是float32、float64。
代码示例¶
# 静态图使用
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)
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]]