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

前端 未结 8 1631
深忆病人
深忆病人 2021-02-05 01:49

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]

相关标签:
8条回答
  • 2021-02-05 02:14

    See this for all distributions: https://pytorch.org/docs/stable/distributions.html#torch.distributions.uniform.Uniform

    This is the way I found works:

    # generating uniform variables
    
    import numpy as np
    
    num_samples = 3
    Din = 1
    lb, ub = -1, 1
    
    xn = np.random.uniform(low=lb, high=ub, size=(num_samples,Din))
    print(xn)
    
    import torch
    
    sampler = torch.distributions.Uniform(low=lb, high=ub)
    r = sampler.sample((num_samples,Din))
    
    print(r)
    
    r2 = torch.torch.distributions.Uniform(low=lb, high=ub).sample((num_samples,Din))
    
    print(r2)
    
    # process input
    f = nn.Sequential(OrderedDict([
        ('f1', nn.Linear(Din,Dout)),
        ('out', nn.SELU())
    ]))
    Y = f(r2)
    print(Y)
    

    but I have to admit I don't know what the point of generating sampler is and why not just call it directly as I do in the one liner (last line of code).

    Comments:

    • sampler are good for it's so you can transform/compose/cache/etc distributions. see https://arxiv.org/abs/1711.10604, and the top of the docs of https://pytorch.org/docs/stable/distributions.html# and https://arxiv.org/abs/1506.05254
    • you can feed in tensors to uniform to let it know the high dimensional interval (hypercube) to generate the uniform samples (that's why it receives tensors as input rather than simply numbers)

    Reference:

    • How to get a uniform distribution in a range [r1,r2] in PyTorch?
    • https://discuss.pytorch.org/t/generating-random-tensors-according-to-the-uniform-distribution-pytorch/53030/8
    • https://github.com/pytorch/pytorch/issues/24162
    0 讨论(0)
  • 2021-02-05 02:16

    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
    
    0 讨论(0)
  • 2021-02-05 02:20
    torch.FloatTensor(a, b).uniform_(r1, r2)
    
    0 讨论(0)
  • 2021-02-05 02:27

    Please Can you try something like:

    import torch as pt
    pt.empty(2,3).uniform_(5,10).type(pt.FloatTensor)
    
    0 讨论(0)
  • 2021-02-05 02:28

    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]) 
    
    0 讨论(0)
  • 2021-02-05 02:32

    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)))
    
    0 讨论(0)
提交回复
热议问题