QBoard » Artificial Intelligence & ML » AI and ML - PyTorch » How to create a normal distribution in pytorch

How to create a normal distribution in pytorch

  • 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?
      September 17, 2020 5:02 PM IST
    0
  • 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])
      September 17, 2020 5:19 PM IST
    0
  • 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])
      September 17, 2020 5:21 PM IST
    0
  • 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.

    This post was edited by Raji Reddy A at September 17, 2020 5:23 PM IST
      September 17, 2020 5:22 PM IST
    0
  • 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.

      September 17, 2020 5:25 PM IST
    0
  • 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]])
     
     
      September 17, 2020 5:30 PM IST
    0
  • 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

      January 13, 2022 1:16 PM IST
    0