randn¶
返回符合标准正态分布(均值为 0,标准差为 1 的正态随机分布)的随机 Tensor,形状为 shape
,数据类型为 dtype
。
参数¶
shape (list|tuple|Tensor) - 生成的随机 Tensor 的形状。如果
shape
是 list、tuple,则其中的元素可以是 int,或者是形状为[]且数据类型为 int32、int64 的 0-D Tensor。如果shape
是 Tensor,则是数据类型为 int32、int64 的 1-D Tensor。dtype (str|np.dtype,可选) - 输出 Tensor 的数据类型,支持 float32、float64、complex64、complex128。当该参数值为 None 时,输出 Tensor 的数据类型为 float32。默认值为 None。
name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。
返回¶
Tensor:符合标准正态分布的随机 Tensor,形状为
shape
,数据类型为dtype
。
示例代码¶
>>> 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.1375531256198883+0.0932074561715126j) ,
(0.7955012917518616-0.41801896691322327j),
(-0.6730020642280579-0.09163688868284225j)],
[ (0.17453041672706604-0.9002832770347595j),
(0.16270922124385834-1.3086302280426025j),
(0.9428746104240417+0.06869460642337799j)]])
使用本API的教程文档¶
- 使用样例
- 案例解析
- Limitations
- 自定义 C++算子
- 自定义 C++ 扩展
- 使用 paddle.nn.Layer 自定义网络
- 梯度裁剪方式介绍
- 模型性能分析(Profiler)
- 附录: 飞桨框架 2.x
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- PyTorch 写法
- pytorch
- pytorch
- torch 写法
- PyTorch 写法
- pytorch
- pytorch
- pytorch
- pytorch
- pytorch
- 兼容载入旧格式模型
- 升级指南
- 自动混合精度
- CINN 神经网络编译器
- 高阶自动微分功能支持科学计算
- 动转静 SOT 原理及使用