'How do I create a normal distribution in pytorch?

I want to create a random normal distribution with a given mean and std.



Solution 1:[1]

You can easily use torch.Tensor.normal_() method.

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.

torch.empty(5).normal_(mean=4,std=0.5)

Result:

tensor([4.1450, 4.0104, 4.0228, 4.4689, 3.7810])

Solution 2:[2]

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 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)


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]])

Solution 3:[3]

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.

import torch
torch.randn(5) * 0.5 + 4 # tensor([4.1029, 4.5351, 2.8797, 3.1883, 4.3868])

Solution 4:[4]

You can create your distribution like described here in the docs. In your case this should be the correct call, including sampling from the created distribution:

from torch.distributions import normal

m = normal.Normal(4.0, 0.5)
s = m.sample()

If you want to get a sample of a certain size/shape, you can pass it to sample(), for example

s = m.sample([5, 5])

for a 5x5-Tensor.

Solution 5:[5]

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

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.

Solution 6:[6]

For all distribution see: https://pytorch.org/docs/stable/distributions.html#

click on right menu to jump to normal (or search in the docs).

An example code:

import torch

num_samples = 3
Din = 1
mu, std = 0, 1
x = torch.distributions.normal.Normal(loc=mu, scale=std).sample((num_samples, Din))

print(x)

For details on torch distributions (with emphasis on uniform) see my SO answer here: https://stackoverflow.com/a/62919760/1601580

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Furkan
Solution 2
Solution 3 gui11aume
Solution 4
Solution 5 Pankaj Mishra
Solution 6