QBoard » Artificial Intelligence & ML » AI and ML - PyTorch » How to get a uniform distribution in a range [r1,r2] in PyTorch?

How to get a uniform distribution in a range [r1,r2] in PyTorch?

  • The question says it all. I want to get a 2-D torch.Tensor with size [a,b] filled with values from a uniform distribution (in range [r1,r2]) in PyTorch.
      September 17, 2020 2:36 PM IST
    0
  • Please Can you try something like:

    import torch as pt
    pt.empty(2,3).uniform_(5,10).type(pt.FloatTensor)
      September 18, 2020 10:43 AM IST
    0
  • This answer uses NumPy to first produce a random matrix and then converts the matrix to a PyTorch tensor. I find the NumPy API to be easier to understand.

    import numpy as np
    
    torch.from_numpy(np.random.uniform(low=r1, high=r2, size=(a, b)))
      September 18, 2020 10:44 AM IST
    0
  • To get a uniform random distribution, you can use

    torch.distributions.uniform.Uniform()

    example,

    import torch
    from torch.distributions import uniform
    
    distribution = uniform.Uniform(torch.Tensor([0.0]),torch.Tensor([5.0]))
    distribution.sample(torch.Size([2,3])

    This will give the output, tensor of size [2, 3].

      September 18, 2020 11:24 AM IST
    0

  • If U is a random variable uniformly distributed on [0, 1], then (r1 - r2) * U + r2 is uniformly distributed on [r1, r2].

    Thus, you just need:

    (r1 - r2) * torch.rand(a, b) + r2​

    Alternatively, you can simply use:

    torch.FloatTensor(a, b).uniform_(r1, r2)​

    To fully explain this formulation, let's look at some concrete numbers:

    r1 = 2 # Create uniform random numbers in half-open interval [2.0, 5.0)
    r2 = 5
    
    a = 1 # Create tensor shape 1 x 7
    b = 7​

    We can break down the expression (r1 - r2) * torch.rand(a, b) + r2 as follows:

    1.torch.rand(a, b) produces an a x b (1x7) tensor with numbers uniformly distributed in the range [0.0, 1.0).
    x = torch.rand(a, b)
    print(x)
    # tensor([[0.5671, 0.9814, 0.8324, 0.0241, 0.2072, 0.6192, 0.4704]])​

    2.(r1 - r2) * torch.rand(a, b) produces numbers distributed in the uniform range [0.0, -3.0)
    print((r1 - r2) * x)
    tensor([[-1.7014, -2.9441, -2.4972, -0.0722, -0.6216, -1.8577, -1.4112]])​

    3.(r1 - r2) * torch.rand(a, b) + r2 produces numbers in the uniform range [5.0, 2.0)
    print((r1 - r2) * x + r2)
    tensor([[3.2986, 2.0559, 2.5028, 4.9278, 4.3784, 3.1423, 3.5888]])​
      September 18, 2020 11:31 AM IST
    0
  • For those who are frustratingly bashing their keyboard yelling "why isn't this working." as I was... note the underscore behind the word uniform.

    torch.FloatTensor(a, b).uniform_(r1, r2)
                                   ^ here
      December 22, 2020 3:04 PM IST
    0
  • Utilize the torch.distributions package to generate samples from different distributions.

    For example to sample a 2d PyTorch tensor of size [a,b] from a uniform distribution of range(low, high) try the following sample code

    import torch
    a,b = 2,3   #dimension of the pytorch tensor to be generated
    low,high = 0,1 #range of uniform distribution
    
    x = torch.distributions.uniform.Uniform(low,high).sample([a,b]) 
      December 22, 2020 3:06 PM IST
    0