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

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

提交回复
热议问题