加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何在pytorch中创建正态分布

发布时间:2020-12-16 22:27:01 所属栏目:Python 来源:网络整理
导读:我想在pytorch中创建一个随机正态分布,mean和std分别为4,0.5.我找不到它的API.有谁知道?非常感谢. 最佳答案 对于标准正态分布(即mean = 0和variance = 1),您可以使用torch.randn() 对于自定义均值和标准的情况,您可以使用 torch.distributions.Normal() Ini

我想在pytorch中创建一个随机正态分布,mean和std分别为4,0.5.我找不到它的API.有谁知道?非常感谢.

最佳答案
对于标准正态分布(即mean = 0和variance = 1),您可以使用torch.randn()

对于自定义均值和标准的情况,您可以使用torch.distributions.Normal()

Init signature:
tdist.Normal(loc,scale,validate_args=None)

Docstring:
Creates a normal (also called Gaussian) distribution parameterized by
loc and scale.

Args:
loc (float or Tensor): mean of the distribution (often referred to as mu)
scale (float or Tensor): standard deviation of the distribution
(often referred to as sigma)

这是一个例子:

In [32]: import torch.distributions as tdist

In [33]: n = tdist.Normal(torch.tensor([4.0]),torch.tensor([0.5]))

In [34]: n.sample((2,))
Out[34]: 
tensor([[ 3.6577],[ 4.7001]])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读