Participate in the 12th day of The November Gwen Challenge, see the details of the event: 2021 Last Gwen Challenge
Pytorch rand* random number
torch.rand
The torch. Rand (* sizes, out = None) - > Tensor
Returns a tensor containing a set of random numbers drawn from a uniform distribution of the interval [0,1], whose shape is defined by the variable parameter sizes.
print(torch.rand(3))
print(torch.rand(3.2))
Copy the code
The output is:
> > tensor ([0.3445, 0.9567, 0.7707]) tensor ([[0.7644, 0.5776], [0.4379, 0.5460], [0.1860, 0.2946]])Copy the code
* Because it is random, it is normal for you and I to output different results (this explanation is a bit redundant…).
torch.randn
The torch. Randn (* sizes, out = None) - > Tensor
Returns a tensor containing a set of random numbers drawn from the standard normal distribution (mean 0, variance 1, i.e., Gaussian white noise), whose shape is defined by the variable parameter Sizes.
Parameters:
- sizes (int…) – Integer sequence that defines the output shape
- out (Tensor.optinal) – Result tensor
print(torch.randn(3))
print(torch.randn(3.2))
Copy the code
> > tensor ([0.7186, 1.4286, 0.6510]) tensor ([[1.1820, 0.4163], [1.2188, 1.3141], [0.9691, 0.2682]])Copy the code
torch.randperm
The torch. Randperm (n, out = None) - > LongTensor
Given n, returns a random permutation of integers from [0, n-1].
Parameters:
- N (int) — Upper boundary (not included)
print(torch.randperm(10))
Copy the code
>>
tensor([2, 4, 8, 5, 0, 9, 6, 1, 7, 3])
Copy the code
Think of range or arrange. Both return an ordered sequence and can set the step size. The official recommendation is to use the function torch. Arange ().
Randn and Normal
After the rand* function above, let’s introduce a torch. Normal ()
torch.normal()
torch.normal(means, std, out=None)
Returns a tensor containing a random number extracted from a discrete normal distribution of the given parameter means, STD. The mean means is a tensor containing the mean of the normal distribution associated with each output element. STD is a tensor containing the standard deviation of the normal distribution associated with each output element. The mean and standard deviation shapes do not have to match, but each tensor must have the same number of elements.
Parameters:
- Means — Tensor
- STD — standard deviation
- Out (Tensor) — Optional output Tensor
Although randn and Normal can generate random numbers that follow normal distribution, normal can set its own mean and standard deviation.
That’s the difference.