当前位置:   article > 正文

torch.normal函数用法

torch.normal函数

官方文档给的用法:

torch.normal(means, std, out=None)

意思为means给定tensor的均值范围和形状,std给出每个均值服从的标准差

官方示例:

  1. torch.normal(means=torch.arange(1, 11), std=torch.arange(1, 0, -0.1))
  2. 1.5104
  3. 1.6955
  4. 2.4895
  5. 4.9185
  6. 4.9895
  7. 6.9155
  8. 7.3683
  9. 8.1836
  10. 8.7164
  11. 9.8916
  12. [torch.FloatTensor of size 10]

实际情况:

  1. >>> torch.normal(means=torch.arange(1, 11), std=torch.arange(1, 0, -0.1))
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. TypeError: normal() received an invalid combination of arguments - got (means=Tensor, std=Tensor, ), but expected one of:
  5. * (Tensor mean, Tensor std, *, torch.Generator generator, Tensor out)
  6. * (Tensor mean, float std, *, torch.Generator generator, Tensor out)
  7. * (float mean, Tensor std, *, torch.Generator generator, Tensor out)
  8. * (float mean, float std, tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

从错误类型中看出现在torch中已经不再使用means而是使用mean,即只能生成固定均值的正态分布。

正确的打开方式:

采用第四种:

torch.normal(mean, std, size)

三个参数分别为均值,标准差和size

  1. >>> torch.normal(3, 0.1, (3, 4))
  2. tensor([[2.9425, 3.1877, 2.9735, 3.0982],
  3. [3.0061, 2.9918, 2.7953, 3.0066],
  4. [2.8219, 2.9578, 2.8813, 2.9014]])

采用第三种:

torch.normal(mean, stds)

两个参数分别为:均值和标准差,使用标准差来确定范围size

  1. >>> torch.normal(3, torch.ones(3, 4)/10)
  2. tensor([[2.8491, 3.0263, 3.0888, 3.0818],
  3. [3.1101, 2.7490, 3.1847, 3.0861],
  4. [2.8530, 2.8666, 2.9634, 3.1875]])

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/358183
推荐阅读
相关标签
  

闽ICP备14008679号