5 Useful Functions in PyTorch

PyTorch is a popular open-source machine learning library that is renowned for its flexibility and ease of use. It was developed primarily by Facebook's AI Research lab (FAIR). It provides a rich set of functions for building, training, and deploying deep learning models. Its rich ecosystem includes modules for optimization, data loading, visualization, and integration with other popular libraries such as NumPy and SciPy. Below we will be taking a brief look at the following 5 functions:

  • torch.tensor()
  • torch.rand()
  • torch.numel()
  • torch.cat()
  • torch.le()
# 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.tensor()

# Example 1
torch.tensor([[1, 2], [3, 4.]])
tensor([[1., 2.],
        [3., 4.]])