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

前端之家收集整理的这篇文章主要介绍了python – 如何在pytorch中创建正态分布前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想在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]])
原文链接:https://www.f2er.com/python/438893.html

猜你在找的Python相关文章