Top 5 Pytorch random functions you should have to know
PyTorch is an open-source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab. It is free and open-source software released under the Modified BSD license.
The PyTorch package provides two high-level features:
- Tensor computation (like NumPy) with strong GPU acceleration.
- Deep neural networks built on a tape-based autograd system.
In this article, we will focus on the top random functions used in PyTorch
The chosen functions are :
- torch.rand(): Returns a tensor filled with random numbers from a uniform distribution on the interval(0,1).
- torch.randint(): Returns a tensor filled with random integers generated uniformly between low and high.
- torch.randperm(): Returns a random permutation of integers from 0 to n — 1.
- torch.empty() : Returns a tensor filled with uninitialized data.
Function 1 — torch.rand()
torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)[0,1).
working
In the last example, the function filled the tensor with a tuple of a random 4 numbers on the interval 0,1.
working
In the last example, the function filled the tensor with 2*3 dim with random numbers on the interval 0,1.
breaking (to illustrate when it breaks)
In this example, the function will breaks because the shape of the tensor is defined by the variable argument size and it must be int.
Function 2 — torch.randint()
randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).
In the last example, the function filled the tensor with a tuple of a random 3 numbers between (3 -5).
Example 2 - working
In the last example, the function filled the tensor with 2*2 dim random numbers between 0 (default) and 10.
Example 3 - breaking (to illustrate when it breaks)
In this example, the function will breaks because the shape of the tensor is defined by the variable argument size and it must be int, not a float.
Function 3 — torch.randperm()
torch.randperm(n, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False) → LongTensor.
Returns a random permutation of integers from 0 to n — 1.
Example 1 - working
In the last example, the function filled the tensor with a tuple of random permutation numbers between 0 and 3.
Example 2 - working
In the last example, the function filled the tensor with a tuple of random permutation numbers between 0 and 8.
Example 3 - breaking (to illustrate when it breaks)
In this example, the function will breaks because the shape of the tensor is defined by the variable argument size and it must be int, not a float.
Function 4— torch.empty()
torch.empty(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument size.
Example 1 - working
In the last example, the function filled the tensor with random uninitialized data in the shape of (2*3).
Example 2 - working
In the last example, the function filled the tensor with random uninitialized data in the shape of (9*4).
Example 3 - breaking (to illustrate when it breaks)
In this example, the function will breaks because the shape of the tensor is defined by the variable argument size and it must be int.
Function 5 — torch.rand_like()
torch.rand_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor
Returns a tensor with the same size as
input
that is filled with random numbers from a uniform distribution on the interval [0, 1)[0,1) .
Example - creating empty tensor
In this example, firstly we will create an empty tensor using an empty function.
example - creating random tensor like x tensor shape
After we created an empty tensor in 9*4 shape, we will use the .rand_like(x)
function to create a tensor similar to (x) tensor shape.
Conclusion
We covered in this blog some of the important random functions in Pytorch don't forget to visit documentation for more.
Reference Links
- Official documentation for
torch.Tensor
: https://pytorch.org/docs/stable/tensors.html - random functions documentation : https://pytorch.org/docs/stable/torch.html#torch.rand