当前位置:   article > 正文

详解pytorch中的torch.rand、torch.randn、torch.normal、torch.linespace

torch.randn

区分上述四个函数:

1. torch.rand

#均匀分布采样
torch.rand(*sizes, out=None) → Tensor
#*sizes指定张量的形状、out指定数据存储的位置

import torch
a=torch.rand([3,4])
print(a)
'''
tensor([[0.7305, 0.5913, 0.0737, 0.5401],
        [0.9628, 0.0018, 0.7095, 0.7052],
        [0.2917, 0.1240, 0.6389, 0.2412]])
'''
torch.rand([3,4],out=a)
print(a)
'''
tensor([[0.3986, 0.1947, 0.7191, 0.2240],
        [0.8659, 0.5979, 0.0932, 0.6231],
        [0.6283, 0.3945, 0.1363, 0.0065]])
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2. torch.randn

#采样标准正态分布
torch.randn(*sizes, out=None) → Tensor
#参数含义同上
  • 1
  • 2
  • 3

3. torch.normal

#采样离散正态分布,相比于torch.randn,每一个数字都可以来自不同均值和方差的正态分布
#有以下三种形式
torch.normal(mean=0.0, std, out=None) → Tensor  #mean为标量(共用),std为tensor指定shape和每个位置的方差
torch.normal(mean, std=1.0, out=None) → Tensor #std为标量(共用),mean为tensor指定shape和每个位置的均值
torch.normal(mean, std, size, out=None) → Tensor #std和mean都是标量,此时需要size指定shape
  • 1
  • 2
  • 3
  • 4
  • 5

4. torch.linespace

#线性间距采样
torch.linspace(start, end, steps=100, out=None) → Tensor
#start起点,end终点,steps点数
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/358167
推荐阅读
相关标签
  

闽ICP备14008679号