So there isn’t something default like torch.uniform?
If U is a random variable uniformly distributed on [0, 1], then (r1 - r2) * U + r2 is uniformly distributed on [r1, r2].
Thus, you just need:
(r1 - r2) * torch.rand(a, b) + r2
Alternatively, you can simply use:
torch.FloatTensor(a, b).uniform_(r1, r2)
yes this is true. According to the PyTorch documentation:
torch.rand returns a tensor filled with random numbers from a uniform distribution on the interval [0,1) https://pytorch.org/docs/stable/torch.html#torch.rand
I would appreciate if you can explain the following. I am trying to figure out how the authors of the papers generate normal and uniform distribution:
“Gaussian Noise. The synthetic Gaussian noise dataset consists of 10,000 random 2D Gaussian
noise images, where each RGB value of every pixel is sampled from an i.i.d Gaussian distribution
with mean 0.5 and unit variance. We further clip each pixel value into the range [0, 1]. Uniform Noise. The synthetic uniform noise dataset consists of 10,000 images where each RGB
value of every pixel is independently and identically sampled from a uniform distribution on [0, 1].”
In the code they seemed to use this for generating Gaussian dataset:
According to your explanation the generation of the Uniform dataset is being correct.
What I would like to know is how they used the rand for the Gaussian dataset, although this discussion aligns with what they did:
A simple option is to use the randn function from the base module. It creates a random sample from the standard Gaussian distribution. To change the mean and the standard deviation you just use addition and multiplication. Below I create sample of size 5 from your requested distribution.
a = torch.tensor([0., 10.])
b = torch.tensor([1., 11.])
>>> tensor([ 0.8583, 10.0226])
This example is confusing me. What does uniform do when receiving tensors of size larger than 1x1 mean? Whats the behavior when they are greater than 1 dimension?
I saw the docs but they weren't helpful nor any example or discussion of the case you provides is given: https://pytorch.org/docs/stable/distributions.html#torch.distributions.uniform.Uniform
What I want is to replicate this numpy code:
I think what puts me off is that the interface isn’t that similar to numpy. e.g. even if those names are the same there is a .random to call them in numpy but there is not in pytorch which is where my confusion and me searching for uniform came about.
For numpy it’s numpy.random.rand or randn while in pytorch you can just do torch.randn or rand and for uniform one has to do torch.distribution.uniform.Uniform or distribution.Distribution.
Anyway, thanks for clarifying.
Just curious, why doesn’t it match numpy more closely?
I think these were early design decisions, which were already introduced in Torch7 (Lua).
Once these functions are commonly used, it’s hard to change them due to backwards compatibility. Especially once you reach a non-beta version.