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.
For a standard normal distribution (i.e. mean=0
and variance=1
), you can use torch.randn()
For your case of custom mean
and std
, you can use 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)
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]])