DEV Community

pankaj kumar
pankaj kumar

Posted on

How to Perform Basic Matrix Operations with Pytorch Tensor

In this Notebook, I try to Explain Basic Matrix Operations using PyTorch tensor.

Lets Discuss Tensor First!

Tensor is a multi-dimensional matrix containing elements of a single data type.
like tensor is multidimensional so you can Easily handle number Which is a zero-dimensional matrix, vector Which is a single-dimensional matrix, matrix Which is a two-dimensional matrix, or multi-dimensions matrix.

# Import torch and other required modules
import torch
# Number
t1 = torch.tensor(9.)
t1
tensor(9.)
# vector
t2 = torch.tensor([1,2,3,4,5])
t2


tensor([1, 2, 3, 4, 5])
# matrix
t3 = torch.tensor([[2., 6], 
                   [8, 9], 
                   [9, 4]])
t3
tensor([[2., 6.],
        [8., 9.],
        [9., 4.]])
# n-dimentional
t4 = torch.tensor([[[11., 12., 13.],
         [13., 14., 15.]],

        [[15., 16., 17.],
         [17., 18., 19.]]])
t4
tensor([[[11., 12., 13.],
         [13., 14., 15.]],

        [[15., 16., 17.],
         [17., 18., 19.]]])

In the ML/DL you can use CPU or GPU for processing and torch can handle both devices with using torch.device for more detail go to https://pytorch.org/docs/stable/tensors.html

Let's Discuss Matrix and Operations of Matrix

Matrix

In mathematics, a matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. --Wikipedia
Alt Text

Image Source: Wikipedia
In the above matrix, you can see an m × n matrix: The m is the number of Horizontal are rows and the n is verticals are columns. In the matrix, each element is denoted by a variable with two subscripts like a 2,1 that means second row and first column

The Ml/DL matrix is very important because with matrix data handling and representation are very easy so Pytorch provides a tensor for handling matrix or higher dimensional matrix as I discussed above.
now Let's discuss a different type of matrix and how to create and handle with tensor

Matrix Operations

Scalar Operations

  • Addition
  • Subtraction
  • Multiplication
  • division
  • other mathematical function

Matrix Operations (in the addition, subtraction, scalar matrix Multiplication snd division must be dimension order is same)

  • Addition of Matrices
  • Subtraction of Matrices
  • scaler Multiplication of Matrices
  • scaler Multiplication of Matrices
  • Multiplication of Matrices

Read more:
https://byjus.com/jee/matrix-operations/

https://chem.libretexts.org/Bookshelves/Physical_and_Theoretical_Chemistry_Textbook_Maps/Book%3A_Mathematical_Methods_in_Chemistry_(Levitus)/15%3A_Matrices/15.03%3A_Matrix_Multiplication

# for example let's take 2x4 matrix 
x = torch.randn((2,3))

x, x.shape

(tensor([[-2.7610e-01,  7.4592e-01,  4.8388e-01],
         [ 8.7141e-01, -6.2898e-04,  8.8964e-01]]), torch.Size([2, 3]))
y = torch.randn((2,3))
y, x.shape
(tensor([[-0.3368, -0.8528, -0.3528],
         [ 0.4487, -1.1638, -0.8607]]), torch.Size([2, 3]))

Scaler Opretions of Matrix

Addition of scaler number with matrix element

# add constant with matrix
x+2
tensor([[1.7239, 2.7459, 2.4839],
        [2.8714, 1.9994, 2.8896]])
2*x
tensor([[-5.5220e-01,  1.4918e+00,  9.6776e-01],
        [ 1.7428e+00, -1.2580e-03,  1.7793e+00]])

Subtract scaler number with matrix element

# Subtract constant with matrix
x-2
tensor([[-2.2761, -1.2541, -1.5161],
        [-1.1286, -2.0006, -1.1104]])

Multiplication of scaler number with matrix element

x*3
tensor([[-8.2830e-01,  2.2377e+00,  1.4516e+00],
        [ 2.6142e+00, -1.8869e-03,  2.6689e+00]])
x*-1
tensor([[ 2.7610e-01, -7.4592e-01, -4.8388e-01],
        [-8.7141e-01,  6.2898e-04, -8.8964e-01]])

Division of scaler number with matrix element

x/2
tensor([[-1.3805e-01,  3.7296e-01,  2.4194e-01],
        [ 4.3571e-01, -3.1449e-04,  4.4482e-01]])

Other Math function

Absolute Function

Alt Text
Images Sauce:pytorch.org

torch.abs(x)
tensor([[2.7610e-01, 7.4592e-01, 4.8388e-01],
        [8.7141e-01, 6.2898e-04, 8.8964e-01]])

Ceiling

Alt Text
Images Sauce:pytorch.org

torch.ceil(x)
tensor([[-0., 1., 1.],
        [1., -0., 1.]])

Similarly, you can use other math functions like Cos(x), sin(x), etc.

Matrix opretions

m1 = torch.tensor([[2., 6], 
                   [8, 9], 
                   [9, 4]])
m1
tensor([[2., 6.],
        [8., 9.],
        [9., 4.]])
m2 = torch.tensor([[1., 6], 
                   [8, 4], 
                   [3, 4]])
m2
tensor([[1., 6.],
        [8., 4.],
        [3., 4.]])

Addition of Matrices

# add self witch is similar  to 2*m1
m1+m1
tensor([[ 4., 12.],
        [16., 18.],
        [18.,  8.]])
m1+m2
tensor([[ 3., 12.],
        [16., 13.],
        [12.,  8.]])

Subtraction of Matrices

m1-m2
tensor([[1., 0.],
        [0., 5.],
        [6., 0.]])
# Subtract with self
x-x
tensor([[0., 0., 0.],
        [0., 0., 0.]])

Division of Matrices

# this is scaler Multiplication don't be confused with matrix Multiplication. in scaler, Multiplication must be the dimensions  are the same  
x*x
tensor([[7.6231e-02, 5.5639e-01, 2.3414e-01],
        [7.5936e-01, 3.9561e-07, 7.9146e-01]])
x*y
tensor([[ 9.2995e-02, -6.3612e-01, -1.7071e-01],
        [ 3.9101e-01,  7.3199e-04, -7.6575e-01]])

Division of Matrices

x/x
tensor([[1., 1., 1.],
        [1., 1., 1.]])
x/y
tensor([[ 8.1973e-01, -8.7466e-01, -1.3716e+00],
        [ 1.9420e+00,  5.4046e-04, -1.0336e+00]])

Multiplication of Matrices

If X and Y are matrix and X has dimensions m×n and Y have dimensions n×p, then the product of X and Y has dimensions m×p.
The entry (XY)ij is obtained by multiplying row I of X by column j of Y, which is done by multiplying corresponding entries together and then adding the results:

Alt Text

Images Sauce:chem.libretexts.org

For Matrix multipication you can use @ oprator and * oparetor is scaler multipication

#let's take X 
X = torch.tensor([[2., 6], 
                   [8, 9], 
                   [9, 4]])
X, X.shape
(tensor([[2., 6.],
         [8., 9.],
         [9., 4.]]), torch.Size([3, 2]))
#let's take X 
Y = torch.tensor([[2., 6, 9], 
                   [8, 9, 4]])
, X.shape
'X.shape'
#multiply X ans Y 
X@Y
tensor([[ 52.,  66.,  42.],
        [ 88., 129., 108.],
        [ 50.,  90.,  97.]])

If dimention of matrix is not according to the rul then you can not perform matrix multipication

# example with error
m1@m2
---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-29-f3553d88c578> in <module>
      1 # example with error
----> 2 m1@m2


RuntimeError: size mismatch, m1: [3 x 2], m2: [3 x 2] at /opt/conda/conda-bld/pytorch_1587428266983/work/aten/src/TH/generic/THTensorMath.cpp:41
m1.shape
torch.Size([3, 2])
m2.shape
torch.Size([3, 2])

The dimension of m1 is 3x2 and the dimension of m2 is 3x2 so m1 column is not matched with m2 rows 3!=2 that's why this is fire dimension error

For live code click here

Top comments (0)