Skip to main content

Exploring Tensor Fundamentals with TensorFlow 2.0

Exploring Tensor Fundamentals with TensorFlow Library

In the realm of artificial intelligence (AI), machine learning (ML), and deep learning (DL), tensors serve as the fundamental building blocks for representing and manipulating data. With the advent of powerful libraries like TensorFlow, developers and researchers have gained access to robust tools and frameworks for working with tensors effectively.

In this blog post, we will embark on a journey to explore the fundamental concepts of tensors and their applications within the TensorFlow library.

Understanding Tensors: The Foundation of Data Representation

At its core, a tensor is a multi-dimensional array or matrix that can hold numerical data of various types. Tensors come in different shapes and sizes, ranging from scalars (0-dimensional tensors) to vectors (1-dimensional tensors), matrices (2-dimensional tensors), and higher-dimensional arrays. Each dimension of a tensor represents a different mode or aspect of the data, allowing for rich and expressive data representations.

Introduction to TensorFlow: A Powerful Deep Learning Framework

TensorFlow, developed by Google Brain, is one of the most widely used and comprehensive deep learning frameworks available today. At its heart, TensorFlow revolves around the concept of tensors, providing a versatile and efficient platform for building, training, and deploying machine learning models. With its extensive suite of APIs and tools, TensorFlow empowers developers to unleash the full potential of tensors in solving a wide range of AI and ML tasks.

Tensor Fundamentals with Tensorflow Library

Hi guys welcome to the new blog. In this blog we are going to cover some of the fundamental concepts of tensors using Tensorflow. Topics that we are going to cover,

  • Introduction to tensors
  • Getting information from tensors
  • Tensor manipulation
  • Using @tf.function(boost up the regular Python functions)

Now, let's start by importing the Tensorflow library,

# Import Tensorflow
import tensorflow as tf
print(tf.__version__)
2.14.0

Creating tensors with tf.constant()

  • tf.constant() is a tensorflow function which helps you creates a constant tensors.

There are different types of tensors like scalar, vector, matrices etc... Let's start with the Scaler Tensor.

scalar = tf.constant(7)
scalar
<tf.Tensor: shape=(), dtype=int32, numpy=7>

Now, let's try to understand the output of tf.constant().

  • <tf.Tensor: defines that it's a tensor.
  • shape=() function gives you the information about the shape.
  • dtype=int32 gives you the information about the datatype of the tensor
  • numpy=7 defines the constant input value of the tensor

Now, let's try to extract some of the information from the tensor. Let's start with the number of dimensions of a tensor and ndim attribute will help you get this information.

scalar.ndim
0
  • 0 means scalar type tensors has 0 dimensions.

Now, the next type of tensor is vector, let's see how we can create the Vector type tensor in Tensorflow.

## Create a vector
vec = tf.constant([20, 20])
vec
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([20, 20])>

Now, let's try to understand the ouput of tf.constant() this time.

  • <tf.Tensor: defines that it's a tensor.
  • shape=(2,) function gives you the information about the shape which is 1 and number objects which is 2.
  • dtype=int32 gives you the information about the datatype of the tensor
  • numpy=array([20, 20]) defines the constant input values of the tensor

you can also verify the shape of the vector tensor through ndim attribute.

# Check the dimension of our vector
vec.ndim
1

now, this output value 1 proves the above mention point that vector type tensor is one dimensional tensor. Let's move on this time we are going to create a matrix type tensor.

# Create a matrix
mat = tf.constant([[10, 20],
                   [20, 10]])

mat
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[10, 20],
       [20, 10]])>

Now, let's try to understand the output of matrix type tensor,

  • <tf.Tensor: defines that it's a tensor.
  • shape=(2, 2) function gives you the information about the shape which is 2 and number objects which is 4.
  • dtype=int32 gives you the information about the datatype of the tensor
  • array([[10, 20],[20, 10]]) defines the constant input values of the matrix tensor

let's check the number of dimensions of matrix tensor,

mat.ndim
2

now, this output value 2 proves the above mention point that matrix tensor is a 2D tensor. This time we are going to try something different. Let's try to change some information in the tensor. we are going to change the dtype and for that I am going to create a new matrix tensor.

# Create another matrix
mat2 = tf.constant([[10., 20.],
                    [30., 40.],
                    [50., 60.]], dtype=tf.float16)

mat2
<tf.Tensor: shape=(3, 2), dtype=float16, numpy=
array([[10., 20.],
       [30., 40.],
       [50., 60.]], dtype=float16)>

now, this time something interesting happened. If you see the output of mat2 closely then you realize that this time dtype is dtype=float16, which means 32-bit is the default bit precision of tensorflow library. One more point is that this time in the mat2 tensor number of input values are 3 but the number of dimensions are still same which is 2 and you can verify this by executing the mat2.ndim statement.

mat2.ndim
2

Until now we have seen that scaler is 0D tensor, vector is 1D tensor and matrix is 2D tensor. Now we are going to see how we can create a nDimensional tensor.

t1 = tf.constant([[[11, 12, 13],
                   [14, 15, 16]],
                  [[17, 18, 19],
                   [20, 21, 22]],
                  [[23, 24, 25],
                   [27, 28, 29]]])

t1
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[11, 12, 13],
        [14, 15, 16]],

       [[17, 18, 19],
        [20, 21, 22]],

       [[23, 24, 25],
        [27, 28, 29]]])>

Now, let's try to understand the output of ndim matrix tensor,

  • <tf.Tensor: defines that it's a tensor.
  • shape=(3, 2, 3) function gives you the information about the shape which is 3 and number objects which is 18.
  • dtype=int32 gives you the information about the datatype of the tensor
  • array([[[11, 12, 13], [14, 15, 16]], [[17, 18, 19], [20, 21, 22]], [[23, 24, 25], [27, 28, 29]]]) defines the constant input values of the matrix tensor

let's check the number of dimensions of ndim matrix tensor,

t1.ndim
3

Conclusion

So, in this blog we have learned that how we can create different type of tensors with the help of tf.constant() function. we have created scaler which is 0D tensor, vector which is 1D tensor, matrix which is 2D tensor and in the end we have create a n-dimensional tensor. Hope you have enjoyed this blog. If you have any question or comments you can write in the comment section. Thanks!!!

Happy Coding!!!

Comments