Python 如何在pytorch中创建正态分布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51136581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a normal distribution in pytorch
提问by dq.shen
I want to create a random normal distribution in pytorch and mean and std are 4, 0.5 respectively. I didn't find a API for it. Anyone knows? Thanks very much.
我想在 pytorch 中创建一个随机正态分布,均值和标准差分别为 4、0.5。我没有找到它的 API。有谁知道?非常感谢。
回答by Furkan
You can easily use torch.Tensor.normal_()method.
您可以轻松使用torch.Tensor.normal_()方法。
Let's create a matrix Z (a 1d tensor) of dimension 1 × 5
, filled with random elements samples from the normal distribution parameterized by mean = 4
and std = 0.5
.
让我们创建一个维度为 的矩阵 Z(一维张量)1 × 5
,填充来自正态分布的随机元素样本,由mean = 4
和参数化std = 0.5
。
torch.empty(5).normal_(mean=4,std=0.5)
Result:
结果:
tensor([4.1450, 4.0104, 4.0228, 4.4689, 3.7810])
回答by kmario23
For a standard normal distribution (i.e. mean=0
and variance=1
), you can use torch.randn()
对于标准正态分布(即mean=0
和variance=1
),您可以使用torch.randn()
For your case of custom mean
and std
, you can use torch.distributions.Normal()
对于您的自定义mean
和情况std
,您可以使用torch.distributions.Normal()
Init signature:
tdist.Normal(loc, scale, validate_args=None)Docstring:
Creates a normal (also called Gaussian) distribution parameterized byloc
andscale
.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)
初始化签名:
tdist.Normal(loc, scale, validate_args=None)Docstring:
创建由loc
和参数化的正态(也称为高斯)分布scale
。args:
loc (float or Tensor): 分布的均值 (常被称为 mu)
scale (float or Tensor): 分布的标准差 (常被称为 sigma)
Here's an example:
下面是一个例子:
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]])
回答by gui11aume
A simple option is to use the randn
function from the base module. It creates a random sample from the standard Gaussian distribution. To change the mean and the standard deviation you just use addition and multiplication. Below I create sample of size 5 from your requested distribution.
一个简单的选择是使用randn
基本模块中的函数。它从标准高斯分布中创建一个随机样本。要更改平均值和标准偏差,您只需使用加法和乘法。下面我根据您要求的分布创建了大小为 5 的样本。
import torch
torch.randn(5) * 0.5 + 4 # tensor([4.1029, 4.5351, 2.8797, 3.1883, 4.3868])
回答by M. Deckers
回答by Pankaj Mishra
It depends on what you want to generate.
这取决于您要生成的内容。
For generating standard normal distribution use -
用于生成标准正态分布使用 -
torch.randn()
for all all distribution (say normal, poisson or uniform etc) use
torch.distributions.Normal()
or torch.distribution.Uniform()
.
A detail of all these methods can be seen here - https://pytorch.org/docs/stable/distributions.html#normal
对于所有分布(比如正态分布、泊松分布或均匀分布等)使用
torch.distributions.Normal()
or torch.distribution.Uniform()
。可以在此处查看所有这些方法的详细信息 - https://pytorch.org/docs/stable/distributions.html#normal
Once you define these methods you can use .sample method to generate the number of instances. It also allows you to generates a sample_shape shaped sample or sample_shape shaped batch of samples if the distribution parameters are batched.
一旦你定义了这些方法,你就可以使用 .sample 方法来生成实例的数量。如果分布参数是批处理的,它还允许您生成 sample_shape 形状的样本或 sample_shape 形状的样本批次。