Mathematical Foundations in Tensorflow

Before constructing a basic TensorFlow program, it’s critical to grasp the mathematical ideas required for TensorFlow. Any machine learning algorithm’s core is considered mathematics. A strategy or solution for a certain machine learning algorithm is established with the aid of key mathematical principles. Let’s dive into the mathematical foundations of TensorFlow.
Scalar
A scalar is a physical quantity with no direction that is totally characterized by its magnitude. Scalars are vectors with just one dimension.
Python3
# importing packagesimport tensorflow as tf# creating a scalarscalar = tf.constant(7)scalar |
Output:
<tf.Tensor: shape=(), dtype=int32, numpy=7>
Checking the dimensions:
Python3
scalar.ndim |
Output:
0
Vector
A vector is a two-dimensional object with both magnitude and direction. we can interpret vector geometrically as a directed line segment with an arrow showing the direction and a length of the line equal to the magnitude of the vector. below is an example of creating a vector in TensorFlow.
Python3
# importing packagesimport tensorflow as tf# create a vectorvector = tf.constant([10, 10])# checking the dimensions of vectorvector.ndim |
Output:
1
Matrix
Matrix is a term that refers to multi-dimensional arrays that are organized in rows and columns. The row and column lengths determine the size of the matrix. When a matrix has “a” rows and “b” columns, the matrix is represented as an “a*b” matrix, which also specifies the length of the matrix.
Python3
# importing packagesimport tensorflow as tf# creating a matrixmatrix = tf.constant([[1, 2], [3, 4]])print(matrix)print('the number of dimensions of a matrix is :\'+str(matrix.ndim)) |
Output:
tf.Tensor( [[1 2] [3 4]], shape=(2, 2), dtype=int32) the number of dimensions of a matrix is : 2
Mathematical Operation
Addition
When two or more matrices have the same dimension, they can be added together. The term “addition” refers to the process of adding each element to the given place or position.
Python3
# importing packagesimport tensorflow as tf# creating two tensorsmatrix = tf.constant([[1, 2], [3, 4]])matrix1 = tf.constant([[2, 4], [6, 8]])# addition of two matricesprint(matrix+matrix1) |
Output:
tf.Tensor( [[ 3 6] [ 9 12]], shape=(2, 2), dtype=int32)
Subtraction
The subtraction of matrices works in the same way as the addition of two matrices does. If the dimensions of two matrices are same, the user can subtract them.
Python3
# importing packagesimport tensorflow as tf# creating two tensorsmatrix = tf.constant([[1, 2], [3, 4]])matrix1 = tf.constant([[2, 4], [6, 8]])# subtraction of two matricesprint(matrix1 - matrix) |
Output:
tf.Tensor( [[1 2] [3 4]], shape=(2, 2), dtype=int32)
Multiplication
The dimension n must equal a for two matrices m*n and a*b to be multipliable. m*b is the resultant matrix.
Python3
# importing packagesimport tensorflow as tf# creating two tensorsmatrix = tf.constant([[1, 2], [3, 4]])matrix1 = tf.constant([[2, 4], [6, 8]])# multiplication of two matricesprint(matrix1 * matrix) |
Output:
tf.Tensor( [[ 2 8] [18 32]], shape=(2, 2), dtype=int32)
Division
To perform division two matrices must have the same dimensions, just like addition.
Python3
# importing packagesimport tensorflow as tf# creating two tensorsmatrix = tf.constant([[1, 2],[3, 4]])matrix1 = tf.constant([[2, 4],[6, 8]])# division of two matricesprint(matrix1 / matrix) |
Output:
tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float64)
Transpose
A matrix’s transposition is determined by converting its rows into columns or columns into rows. The letter “T” in the superscript of the supplied matrix denotes the transpose of the matrix.
The transpose of a matrix M m*n is MT (transpose) n*m, which is derived by transposing the column vectors into row vectors. tf.transpose() method is used to find the transpose of a matrix in TensorFlow. if M is a matrix, transpose is represented by MT
Python3
# importing packagesimport tensorflow as tf# creating a matrixmatrix = tf.constant([[1, 2], [3, 4]])# transpose of the matrixprint(tf.transpose(matrix)) |
Output:
tf.Tensor( [[1 3] [2 4]], shape=(2, 2), dtype=int32)
Dot Product
The sum of the products of matching components is the dot product of two vectors. Components that are on the same axis and may be represented as:
tf.tensordot() method is used to find dot product in TensorFlow. when we specify axes =1, matrix multiplication takes place.
Python3
# importing packagesimport tensorflow as tf# creating a matrixmatrix = tf.constant([[1, 2], [3, 4]])# dot product of matricesprint('dot product of matrices is : ' + str(tf.tensordot(matrix, matrix, axes=1))) |
Output:
dot product of matrices is : tf.Tensor( [[ 7 10] [15 22]], shape=(2, 2), dtype=int32)




