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

Assignment 01 - Getting Familiar with Tensor API

For this assignment I have chosen functions that are related to matrix operations commonly used in linear algebra, as it's the basis for Machine-Learning theory.

We'll examine the following functions

  • det
  • eig
  • svd
  • lstsq
  • qr
# Import torch and other required modules
import torch
from numpy import pi as PI

Function 1 - torch.det

Doc: https://pytorch.org/docs/master/generated/torch.det.html#torch.det

This function calculates the determinant of a square tensor, i.e, and nxn tensor.
An exception is thrown if the given matrix is not square.
In addition, this function can calculate batches of matrices. The return value in this case is a tensor that contains a value per each square matrix in the input batch. These values are the determinants of these matrices.

# let's create a simple matrix
m = torch.tensor([[PI, 3], [13, 42]])
m
tensor([[ 3.1416,  3.0000],
        [13.0000, 42.0000]])
# let's calculate its determinant
m.det()
tensor(92.9469)