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

PyTorch tensor operations for Matrix Algebra

PyTorch is a python library which is used for doing mathematical operations over multi-dimensional tensors.
Some PyTorch functions I am going to be talking about in this notebook are:-

  • torch.cross
  • torch.diag
  • torch.det
  • torch.matmul
  • torch.symeig

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

This function computes the cross product of two tensors

# Example 1 - working 
A = torch.tensor([[1, 2, 0], 
                  [3, 4., 5]])
B = torch.tensor([[5, 6., 7],
                  [8, 9, 0]])
A, B
(tensor([[1., 2., 0.],
         [3., 4., 5.]]), tensor([[5., 6., 7.],
         [8., 9., 0.]]))