Basic Tensor Calculation using NumPy in Python
In this tutorial, we will learn
- What is tensor
- How to create a tensor
- Basic operations on tensor
What is Tensor?
Tensors are multi-dimensional arrays. To be specific it is an n-dimensional array with n>2. They are used in linear algebra like vector and matrices.
Tensors are immutable that is you cannot update the contents but can create a new one. The tensor notation is much similar to matrix notation denoted by a capital letter
[[t111, t121, t131] [[t112, t122, t132] [[t113, t123, t133] T =([ [t211, t221, t231], [t212, t222, t232], [t213, t223, t233] ]) [t311, t321, t331]] [t312, t322, t332]] [t313, t323, t333]]
Tensors can be created by using array() function from Numpy which creates n-dimensional arrays. For that, we are going to need the Numpy library.
To install Numpy with Anaconda prompt, open the prompt and type:
conda install numpy
If you want to install with pip, just replace the word ‘conda’ with ‘pip’.
I have used Jupyter notebook to implement this, you can choose whichever python editor you want.
import numpy as np #importing the library
Creating Tensor-
Let’s start by creating tensor-
# creating tensor T = np.array([ [[1,4,7], [2,5,8], [3,6,9]], [[10,40,70], [20,50,80], [30,60,90]], [[100,400,700],[200,500,800],[300,600,900]], ]) print(T) print("This tensor is of dimension:",T.shape)
Output:
[[[ 1 4 7] [ 2 5 8] [ 3 6 9]] [[ 10 40 70] [ 20 50 80] [ 30 60 90]] [[100 400 700] [200 500 800] [300 600 900]]] This tensor is of dimension: (3, 3, 3)
For this tensor axis 0 specifies level, axis 1 specifies row and axis 2 specifies the column.
Basic Operations on Tensor-
Now, let’s do some basic arithmetic operations on tensors
Tensor Addition
In Numpy we can add tensors by adding arrays.
# tensor addition import numpy as np T1 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T2 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T = T1 + T2 print(T)
Output:
[[[10 20 30] [40 50 60] [70 80 90]] [[ 4 8 12] [16 20 24] [28 32 36]] [[ 6 12 18] [24 30 36] [42 48 54]]]
Tensor Subtraction in Python
Similarly applies for Subtraction
# tensor subtraction import numpy as np T1 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T2 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T = T1 - T2 print(T)
Output:
[[[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]]
Tensor Multiplication in Python
We can multiply tensor by multiplying arrays using Numpy. Tensor Multiplication is also known as Hadamard Product
#tensor multiplication T1 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T2 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T = T1*T2 print(T)
Output:
[[[ 25 100 225] [ 400 625 900] [1225 1600 2025]] [[ 4 16 36] [ 64 100 144] [ 196 256 324]] [[ 9 36 81] [ 144 225 324] [ 441 576 729]]]
Tensor Division
Similarly applies for the division
T1 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T2 = np.array([ [[5,10,15],[20,25,30], [35,40,45]], [[2,4,6], [8,10,12], [14,16,18]], [[3,6,9], [12,15,18], [21,24,27]], ]) T = T1/T2 print(T)
Output:
[[[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]]
Conclusion
In this tutorial, we learned about what tensors are and how to do arithmetic operations between tensors using Numpy.
Leave a Reply