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

An Introdcution to Pytorch and 5 basic functions it can do

In the current technological sphere the words Arificial Intelligence and Machine learning can be heard in almost all areas. Machine learning is a subset of Artifical Intelligence that deals with training a system (the learning part of the machine) to do certain functions without the aid of a user. Pytorch is a library used for machine learning. It's free and it's also open source. Here I'll be showing 5 functions it can do. These fucntions are:

  • torch.equal
  • torch.rand
  • torch.range
  • torch.chunk
  • torch.reshape

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.equal

This function is used to check whether two tensors are of same size and has same elements.

# Example 1 - creating two tensors of same size and value
t1 = torch.tensor([1,2])
t2 = torch.tensor([1,2])
torch.equal (t1, t2)
True