How to Perform Basic Matrix Operations with Pytorch Tensor
Tags: machinelearning, pytorch, tensor, matrix
Published: 2020-05-30 10:28:58


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.


```python
# Import torch and other required modules
import torch
```


```python
# Number
t1 = torch.tensor(9.)
t1
```




    tensor(9.)




```python
# vector
t2 = torch.tensor([1,2,3,4,5])
t2


```




    tensor([1, 2, 3, 4, 5])




```python
# matrix
t3 = torch.tensor([[2., 6], 
                   [8, 9], 
                   [9, 4]])
t3
```




    tensor([[2., 6.],
            [8., 9.],
            [9., 4.]])




```python
# 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](https://dev-to-uploads.s3.amazonaws.com/i/2g2z12vwc6al019pywnj.png)

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


```python
# 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]))




```python
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 


```python
# add constant with matrix
x+2
```




    tensor([[1.7239, 2.7459, 2.4839],
            [2.8714, 1.9994, 2.8896]])




```python
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


```python
# 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


```python
x*3
```




    tensor([[-8.2830e-01,  2.2377e+00,  1.4516e+00],
            [ 2.6142e+00, -1.8869e-03,  2.6689e+00]])




```python
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


```python
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](https://dev-to-uploads.s3.amazonaws.com/i/bbtd7s6klstzqmuxplmw.png)
Images Sauce:[pytorch.org](https://pytorch.org/)

```python
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](https://dev-to-uploads.s3.amazonaws.com/i/glwqg5y18ybsbbnkgus9.png)
Images Sauce:[pytorch.org](https://pytorch.org/)

```python
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 


```python
m1 = torch.tensor([[2., 6], 
                   [8, 9], 
                   [9, 4]])
m1
```




    tensor([[2., 6.],
            [8., 9.],
            [9., 4.]])




```python
m2 = torch.tensor([[1., 6], 
                   [8, 4], 
                   [3, 4]])
m2
```




    tensor([[1., 6.],
            [8., 4.],
            [3., 4.]])



#### Addition of Matrices


```python
# add self witch is similar  to 2*m1
m1+m1
```




    tensor([[ 4., 12.],
            [16., 18.],
            [18.,  8.]])




```python
m1+m2
```




    tensor([[ 3., 12.],
            [16., 13.],
            [12.,  8.]])



#### Subtraction of Matrices


```python
m1-m2
```




    tensor([[1., 0.],
            [0., 5.],
            [6., 0.]])




```python
# Subtract with self
x-x
```




    tensor([[0., 0., 0.],
            [0., 0., 0.]])



#### Division of Matrices


```python
# 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]])




```python
x*y
```




    tensor([[ 9.2995e-02, -6.3612e-01, -1.7071e-01],
            [ 3.9101e-01,  7.3199e-04, -7.6575e-01]])



#### Division of Matrices


```python
x/x
```




    tensor([[1., 1., 1.],
            [1., 1., 1.]])




```python
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](https://dev-to-uploads.s3.amazonaws.com/i/tv392uufx22rx3p5uvan.png)

Images Sauce:[chem.libretexts.org](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 Matrix multipication you can use @ oprator and * oparetor is scaler multipication


```python
#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]))




```python
#let's take X 
Y = torch.tensor([[2., 6, 9], 
                   [8, 9, 4]])
, X.shape
```




    'X.shape'




```python
#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 


```python
# 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



```python
m1.shape
```




    torch.Size([3, 2])




```python
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](https://jovian.ml/pk500/01-tensor-operations)