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

前端 未结 8 1597
深忆病人
深忆病人 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:33

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

提交回复
热议问题