In this notebook, I will explain tensors with Python Numpy. For numpy refresher please checkout following resource...
https://www.nbshare.io/notebook/692176011/Numpy-Basics/
importnumpyasnpBefore we delve in to tensors. We need to understand first what is Scalars, Vectors, Arrays and Matrices.
Scalar - Scalar is just a number. Example number 55 is a scalar. Scalar has no dimensions.
Vector - Vector is one dimensional array or scalar with orientation (i.e. horizontal or vertical). Vector can be 1,2 or n dimensional.
Array - Array is a computer language term used in data structures. Array is a collection of similar data elements.
Matrix - Matrix is a mathematical term. It is a 2D array of numbers represented in rows and columns.
Example of 3 dimensional Vector is shown below.
threeDVector=np.array([1,2,3])threeDVectorarray([1, 2, 3])
threeDVector.shape(3,)
Matrix is 2 dimensional array.
np.array([[1,2,3],[4,5,6]])array([[1, 2, 3],
[4, 5, 6]])Tensor is a mathematical object with more than two dimensions. The number of dimensions of an array defines the order of a Tensor. For example a scalar is 0 order tensor. Similarly vector is a 1 order tensor. A 2D matrix is 2nd order Tensor so on and so forth.
Below is an example of vector which is 1 order tensor.
np.array([1,2,3])array([1, 2, 3])
Let us define a two dimensional tensor.
twodTensor=np.arange(36).reshape(6,6)twodTensorarray([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])Let us access the ist row of above tensor
twodTensor[0]array([0, 1, 2, 3, 4, 5])
twodTensor.shape(6, 6)
Three dimensional Tensor example is shown below...
threedTensor=np.arange(36).reshape(4,3,3)threedTensorarray([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]],
[[27, 28, 29],
[30, 31, 32],
[33, 34, 35]]])Note the 3 enclosing brackets - array[[[]]] which indicates it is 3 dimensional tensor.
t3t=np.arange(36).reshape(4,3,3)Let us make our 3 dimension tensor to a 4 dimension tensor.
t4t=t3t[np.newaxis,:,:]Note the 4 enclosing brackets in the below array.
t4tarray([[[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]],
[[27, 28, 29],
[30, 31, 32],
[33, 34, 35]]]])Note our 4th dimension has only 1 element (the 3x3 array). Let us see that using tensor.shape
t4t.shape(1, 4, 3, 3)
t4t[0]#4th dimensionarray([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]],
[[27, 28, 29],
[30, 31, 32],
[33, 34, 35]]])Tensors Arithemetic is mathematical operations on vector and matrix.
Example - Find the dot product of two 3 dimensional vectors, we can use np.dot
array1=np.array([4,4,3])array2=np.array([2,2,3])array1.shape(3,)
array2.shape(3,)
np.dot(array1,array2)25
Dot product two matrix is shown below.
array1=np.array([[1,2,3],[4,4,3]])array2=np.array([[2,2,3],[1,2,3]])print(array1)print("\n")print(array2)[[1 2 3] [4 4 3]] [[2 2 3] [1 2 3]]
np.dot(array1,array2)#this will throw error because row of first matrix is not equal to column of second matrix - Refer matrix multiplication fundamentals---------------------------------------------------------------------------ValueError Traceback (most recent call last) Input In [24], in <cell line: 1>()----> 1np.dot(array1,array2) File <__array_function__ internals>:180, in dot(*args, **kwargs)ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)
array1=np.array([[1,2,3],[4,4,3],[1,1,1]])array2=np.array([[2,2,3],[1,2,3],[1,2,3]])Dot product for above matrices will work because both the arrays are 3x3 matrices.
np.dot(array1,array2)array([[ 7, 12, 18],
[15, 22, 33],
[ 4, 6, 9]])