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

5 Must know PyTorch Functions to handle your Tensor Operations

PyTorch is an optimized tensor library for deep learning using GPUs and CPUs.

PyTorch is the fastest growing Deep Learning framework and it is also used by Fast.ai in its MOOC. PyTorch feels natural to use it if you already are a Python developer.

Here we are about to discuss 5 PyTorch functions that will help you with your tensor(Matrix) operations.

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

TORCH.MM

torch.mm(input, mat2, *, out=None) → Tensor

Performs matrix multiplication of matrices input and mat2.

If input is a (n×m) tensor, mat2 is a (m×p) tensor, out will be a (n×p) tensor. This operation is only for two dimensional matrices.

t1 = torch.full((3,4),3.14)
t2 = torch.eye(4,4)
t3 = torch.mm(t1,t2)
print(t3)
tensor([[3.1400, 3.1400, 3.1400, 3.1400], [3.1400, 3.1400, 3.1400, 3.1400], [3.1400, 3.1400, 3.1400, 3.1400]])