<p>See this for all distributions: <a href="https://pytorch.org/docs/stable/distributions.html#torch.distributions.uniform.Uniform" rel="nofollow noreferrer">https://pytorch.org/docs/stable/distributions.html#torch.distributions.uniform.Uniform</a></p>
<p>This is the way I found works:</p>
<pre><code># 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)
</code></pre>
<p>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).</p>
<p>Comments:</p>
<ul>
<li>sampler are good for it's so you can transform/compose/cache/etc distributions. see <a href="https://arxiv.org/abs/1711.10604" rel="nofollow noreferrer">https://arxiv.org/abs/1711.10604</a>, and the top of the docs of <a href="https://pytorch.org/docs/stable/distributions.html#" rel="nofollow noreferrer">https://pytorch.org/docs/stable/distributions.html#</a> and <a href="https://arxiv.org/abs/1506.05254" rel="nofollow noreferrer">https://arxiv.org/abs/1506.05254</a></li>
<li>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)</li>
</ul>
<hr />
<p>Reference:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/44328530/how-to-get-a-uniform-distribution-in-a-range-r1-r2-in-pytorch/62919760#62919760">How to get a uniform distribution in a range [r1,r2] in PyTorch?</a></li>
<li><a href="https://discuss.pytorch.org/t/generating-random-tensors-according-to-the-uniform-distribution-pytorch/53030/8" rel="nofollow noreferrer">https://discuss.pytorch.org/t/generating-random-tensors-according-to-the-uniform-distribution-pytorch/53030/8</a></li>
<li><a href="https://github.com/pytorch/pytorch/issues/24162" rel="nofollow noreferrer">https://github.com/pytorch/pytorch/issues/24162</a></li>
</ul>
How to get a uniform distribution in a range [r1,r2] in PyTorch?
<p>I want to get a 2-D <code>torch.Tensor</code> with size <code>[a,b]</code> filled with values from a uniform distribution (in range <code>[r1,r2]</code>) in PyTorch.</p>