What is PyTorch
PyTorch is an open-source machine learning library developed by Facebook. It is used for Deep Neural Network and Natural Language Processing purposes.
PyTorch is a Scientific computing package for Python that is similar to NumPy. However, PyTorch has some features that allow it to be more optimized for deep learning research with more flexibility and speed, since it allows the use of the computer's GPU for much faster processing.
TOP 5 PyTorch Functions
For my functions, I've selected items that deal with generating tensors that are filled with random numbers selected in different ways, and also breaking large tensors into more manageable pieces. I've explored some of the differences between these functions to determine their best use cases.
- Function 1 : torch.chunk()
- Function 2 : torch.arange()
- Function 3 : torch.rand()
- Function 4 : torch.randint_like()
- Function 5 : torch.randperm()
Before we begin, let's install and import PyTorch
# Uncomment and run the appropriate command for your operating system, if required
# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch
Function 1 - torch.chunk()
Syntax
torch.chunk(input, chunks, dim=0) → List of Tensors
Parameter :
1. input (Tensor) – the tensor to split
2. chunks (int) – number of chunks to return
3. dim (int) – dimension along which to split the tensor
The torch.chunk function is useful for splitting large tensors into equally sized chunks of the original.
Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by chunks.
This can be used to more quickly train a predictive program, or whenever a section of data should be analyzed seperately from the larger tensor.