Suppose you are given a tensor t
(you can give it the size you want).
Create a tensor having the same shape as t
such that: all its elements are sampled from a uniform distribution between -1
and 2
.
Your code should be robust to the dimensionality of t
: if you change the shape of t
, the shape of your random tensor should adapt!
Sample solution:
Here is a possible solution.
Remember that if X
is a random variable following a uniform distribution between 0 and 1,
then:
- (upper_bound - lower_bound) * X
follows a uniform distribution between 0 and (upper_bound - lower_bound)
- lower_bound + (upper_bound - lower_bound) * X
follows a uniform distribution between lower_bound
and upper_bound
t = torch.randn(size=(1, 6, 2)) # example of tensor t
lower_bound = -1
upper_bound = 2
tensor = lower_bound + torch.rand_like(t) * (upper_bound - lower_bound)