Learn practical skills, build real-world projects, and advance your career

5 PyTorch Functions To Get You Started With PyTorch

Pytorch is an open source machine learning framework developed in 2016 by Facebook's AI Research Lab (FAIR) to make deep learning algorithms more accessible and easier to implement. Pytorch makes solving the already complicated task of implementing deep learning models much easier and intuitive.

You can check out the amazing website for official documentation of PyTorch to dive in deeper. It has everything for beginners to advanced developers starting from tutorials to various resources to learn PyTorch from.

Here are the 5 Pytorch functions that we will learn here to get you started with this amazing framework:

  • torch.arange()
  • torch.gather()
  • torch.where()
  • torch.broadcast_to()
  • torch.sort()

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.arange()

torch.arange returns a range of numbers with values from the interval [start, end) taken with common difference step beginning from start.

# Example 1 - working 
torch.arange(3.)
tensor([0., 1., 2.])