Maths in TensorFlow

For any Deep Learning Enthusiast, Maths is one of the most important skills. Be it the cost function, weight adjustment, or gradient descent, maths is everywhere.

Although TensorFlow and Keras have inbuilt functions for all these calculations, it is very important to know their implementation. So here in this article, we will learn about Maths in TensorFlow.

Tensor Data Structure

It is the basic data structure of TensorFlow. These are just multidimensional arrays. They can be imagined as edges of a graph representing data flow. Tensors are classified by:

  • Rank: The Dimension of a Tensor. Rank 1 is a vector, 2 is a matrix, and 3 a “Triad/Cuboid”.
  • Shape: The number of rows and columns in the tensor
  • Type: The Data type stored in tensor.

We can either make an N-D array using a library like Numpy, or we can convert the array into a tensor.

1-D Tensors

We can make NumPy arrays and then convert them to tensors.

import tensorflow as tf
import numpy as np
#Making a 1-D Numpy Array
One_D=np.array([1.3,1,4.0,9])
print(One_D)
print(One_D.dtype)

To convert this array into a tensor, use the “convert_to_tensor” function. It takes the array and the required data type as a parameter.

Tf_1D = tf.convert_to_tensor(One_D,dtype=tf.float64)

2-D Tensors

Just like one-dimensional arrays, 2-D arrays can also be initialized in the same way.

You can also use the “constant” operator in TensorFlow to change the array to a tensor. To learn more about constants in TensorFlow visit: Understanding Constant in TensorFlow in Python

import tensorflow as tf
import numpy as np
#Making a 2-D Numpy Array
Two_D=np.array([(2,2,2),(2,2,2),(2,2,2)],dtype='int32')
print(Two_D)
print(Two_D.dtype)

Operations With Tensors

Remember that Tensors are generalized vectors. Numpy arrays are very fast. They even beat pure python in speed. These arrays can be easily replaced with Tensors, but the reverse is not true. You can check the complete list of operations at Module: Tf: Math.

Apart from the basic operations like addition or subtraction, there are many operations in TensorFlow like:

  • Matrix Multiplication: Used with Rank 2 Tensors.
  • Determinant: Scalar values of a square matrix
  • Confusion Matrix: Handy for Precision values in Neural Networks.
  • Logical Operations: Boolean Operations
  • Soft plus: Widely used Activation function

There are more than 100 operations that can be performed on tensors.

import tensorflow as tf
import numpy as np
#Matrix Multiplication
matrix1=np.array([(1,2,3),(4,5,6),(7,8,9)],dtype='float64')
matrix2=np.array([(2,3,4),(3,4,5),(3,4,6)],dtype='float64')
#Convert to Tensors
m1=tf.constant(matrix1)
m2=tf.constant(matrix2)
prod=tf.matmul(m1,m2)
print("Multiplication: ")
print(prod)
#Determinant
det=tf.linalg.det(m2)
print("Determinant: ")
print(det)

Confusion Matrix used widely in Machine learning models. It is a performance measurer for a classification problem. Terminologies used are True Positive, True Negative, False Positive, and False Negative. If actual and predicted values are the same, it is true positive/negative. Otherwise false positive/negative.

Tensors can be used to calculate these values very easily.

import tensorflow as tf     
#Printing Example Confusion Matrix
print(tf.math.confusion_matrix([1, 2, 4], [2, 2, 4]))

Very large datasets can also be summarized using these matrices.

Errors with Tensors

While working with tensors, you may encounter an error like:

“Tensor has no object with attribute Numpy”. Either upgrade your TensorFlow version or try using “Early execution”.

If you are working with local IDEs, you might see the tensor data type as  “python.framework.ops.tensor”. Can be easily changed into a NumPy array. Then you will be able to retrieve data.

import tensorflow as tf
import numpy as np
b=tf.add(14,13)
print(b)
#prints--tf.Tensor(27, shape=(), dtype=int32)
b=np.array(b)
print(b)
#prints 27

Feel Free to ask any doubts in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *