Please Can you try something like:
import torch as pt
pt.empty(2,3).uniform_(5,10).type(pt.FloatTensor)
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)))
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].
(r1 - r2) * torch.rand(a, b) + r2
torch.FloatTensor(a, b).uniform_(r1, r2)
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
x = torch.rand(a, b)
print(x)
# tensor([[0.5671, 0.9814, 0.8324, 0.0241, 0.2072, 0.6192, 0.4704]])
print((r1 - r2) * x)
tensor([[-1.7014, -2.9441, -2.4972, -0.0722, -0.6216, -1.8577, -1.4112]])
print((r1 - r2) * x + r2)
tensor([[3.2986, 2.0559, 2.5028, 4.9278, 4.3784, 3.1423, 3.5888]])
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
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])