Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

Hi pals! Welcome to the next deep learning tutorial, where we are at the exciting stage of TensorFlow. In the last tutorial, we just installed the TensorFlow library with the help of Anaconda, and we saw all the procedures step by step. We saw all the prerequisites and understood how you can follow the best procedure to download and install TensorFlow successfully without any trouble. If you have done all the steps, then you might be interested in knowing the basics of TensorFlow. No matter if you are a beginner or have knowledge about TensorFlow, this lecture will be equally beneficial for all of you because there is some important and interesting information that not all people know. So, have a look at the topics that will be discussed with you in just a bit.

  • What is a tensor?

  • What are some important types of tensors that we use all the time while using TensorFlow?

  • How can we start programming in TensorFlow?

  • What are the different operations on the TensorFlow?

  • How can you print the multi-dimensional array int he TensorFlow?

Moreover, you will see some important notes related to the practice that we are going to perform so, you can say, no point will be missing and we will recall our previous concepts all the time when we need them so that the beginners may also get the clear concepts that what is going on in the course.

What is a Tensor?

There are different meanings of tensors in different fields of study, but we are ignoring others and focusing on the field with which we are most concerned: the mathematical definition of the tensor. This term is used most of the time when dealing with the data structure. We believe you have a background in programming languages and know the basics, such as what a data structure is, so I will just discuss the basic definition of tensors.

"The term "tensor" is the generalization of the metrics of nth dimensions and is the mathematical representation of a scaler, vector, dyad, triad, and other dimensions."

Keep in mind, in tensor, all values are identical in the data type with a known shape. Moreover, this shape can also be unknown. There are different ways to introduce the new tensor in TensorFlow while you are programming in it. If it is not clear at the moment, leave that because you will see the practical implementation in the next section. By the same token, you will see more of the additional concepts in just a bit, but before this, let me remind you of something:

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning


Types of Data Structure

No. of Rank

Description

No. of Components

Vector

1

There is only the magnitude but no direction. 

1

Scaler

2

It has both magnitude and direction both.

3

Dyad

3

It has both magnitude and direction both. If x, y, and z are the components of the directions,  then the overall direction is expressed by applying the sum operation of all these components. 

9

Triad

4

It has the magnitude and also have the direction that is obtained by multiplying the 3 x 3 x 3.

27


Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

Types of TensorFlow

Before going deep into the practical work, I want to clarify some important points in the learning of TensorFlow. Moreover, in this tutorial, we will try to divide the lecture into the practical implementation and the theoretical part of the tutorial. We will see some terms often in this tutorial, and I have not discussed them before. So, have a look at the following descriptions.

The Shape of Tensor

The length of the tensor is called its shape, and we can understand the term "shape" in a way that it is defined with the help of the total number of rows and columns. While declaring the tensor, we have to provide its shape.

The Rank of Tensors

The rank defines the dimensions of the tensor. So, in other words, rank defines the order of the dimensions that starts at 1 and ends on the nth dimension.

Type of Tensor

When talking about the tensor, the “type” means the data type of that particular tensor. Just as we consider the data type in other programming languages, when talking about the language of TensorFlow, the type provides the same information about the tensor.

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

Moreover, in order to learn more about the type of the tensors, we examine them with respect to different operations. In this way, we found the following types of tensors:

  1. tf.Variable

  2. tf.constant

  3. tf.placeholder

  4. tf.SparseTensor

Basics of TensorFlow Programming Language

Before we get into the practical application of the information we discussed above, we'll go over the fundamentals of programming with TensorFlow. These are not the only concepts, but how you apply them in TensorFlow may be unfamiliar to you. So, have a look at the information given below:

Comment in the TensorFlow

When you want your compiler to ignore some lines that you have written as notes, you use the “sign of hash” before those lines. In other words, the compiler ignores every line that you start with this sign. In other programming languages, we use different types of signs for the same purpose, such as, // is used in C++ to start the comment line when we are using compilers such as Visual Studio and Dev C++.

Printing the Message in TensorFlow

When we want to print the results or the input to show on the screen, we use the following command:

print(message)

Where the message may be the input, output, or any other value that we want to print. Yet, we have to follow the proper pattern for this. 

Applying Operations in TensorFlow

To apply the operations that we have discussed above, we have to first launch TensorFlow. We covered this in our previous session. Yet, every time, you have to follow some specific steps. Have a look at the details of these steps:

  • Fire up your Anaconda navigator from the search bar. 

  • Go to the Home tab, where you can find the “Jupyter notebook."

  • Click on the launch button. 

  • Select “Python” from the drop-down menu on the right side of the screen. 

  • A new localhost will appear on your Google browser, where you have to write the following command:

import tensorflow as tf

from tensorflow import keras


Make sure you are using the same pattern and take care of the case of the alphabets in each word.

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

Here, you can see, we have provided that information about the type of tensor we want, and as an output, it has provided us with the information about the output. There is no need to provide you with the meaning of the int16 here as we all know that there are different types of integers and we have used the one that occupies the int16 space in the memory. You can change the data type for the practice. It is obvious that you are just feeding the input value, and the compiler is showing the output of the same kind as you were expecting. Here, the shape was empty because we have not input its value. So, we have understood that there is no need to provide the shape all the time. But, in the next programs, we will surely use the shape to tell you the importance of this type. 

Printing Multi-Dimensional Arrays in TensorFlow

Before the practical implementation, you have seen the information about the dimensions of the Tensors. Now, we are moving forward with these types, and here is the practical way to produce these tensors in TensorFlow.

Printing the Two Dimensions Tensor

Here, you can print the two-dimensional array with the help of some additional commands. I'll go over everything in detail one by one. In the case discussed before, we have provided information about the tensor without any shape value. Now, have a look at the case given next;

Copy the following code and insert it into your TensorFlow compiler:


a=tf.constant([[3,5,7],[3,9,1]])

print(a)


Here comes the interesting point. In this case, we are just declaring the array with two dimensions, and TensorFlow will provide you with the shape (information of the dimension) and the memory this tensor name “a" is occupying by itself. As a result, you now know that this array has two rows and three columns.

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

By the same token, you can use any number of dimensions, and the information will be provided to you without any issues. Let us add the other dimensions.

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

Let’s have another example to initialize the matrix in TensorFlow, and you will see the shortcut form of the declaration of a unit matrix of your own choice. We know that a unit matrix has all the elements equal to one. So, you can have it by writing the following code:

a=tf.ones((3,3)

print(a)

The result should look like this:

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

Other examples of matrices that can be generated in such a way are the identity matrix and zero matrices. The identity matrix and the zero matrices are two other matrices that can be generated in this manner. For a zero and identity matrix, we will use “zero” and “eye” respectively in place of “ones” in the code given above. 

The next step is to practice creating a matrix containing random numbers between the ranges that are specified by us. For this, we are using the random operation, and the code for this is given in the next line:

a=tf.random.uniform((1,5),minval=1, maxval=3)

print(a)

When we observe these lines, we will understand that first of all, we are using the random number where the numbers of

 Rows and columns are given by us. I have given the single-row and five-column matrix in which I am providing the compiler with the maximum and minimum values. Now, the compiler will generate random values between these numbers and display the matrix in the order you specify.

Basics of TensorFlow for Deep Learning, tensorflow deep learning, deep learning tensorflow, deep learning python, python deep learning

So, from the programs above, we have learned some important points:

  • Just like in the formation of matrices in MATLAB, you have to put the values of rows in square brackets. 

  • Each element is separated from the others with the help of a comma.

  • Each row is separated by applying the comma between the rows and enclosing the rows in the brackets.

  • All the rows are enclosed in the additional square bracket.

  • You have to use the commas properly, or even if you have a single additional space, you can get an error from the compiler while running it.

  • It is convenient to give the name of the matrix you are declaring so that you can feed the name into the print operation.

  • If you do not name the matrix, you can also use the whole matrix in the print operation, but it is confusing and can cause errors.

  • For the special kinds of matrices that we have learned in our early concepts of matrices, we do not have to use the square brackets, but instead, we will use the parentheses along with the number of elements so that compiler may have the information about the numbers of rows and columns, and by reading the name of the specific type of the matrix, it will automatically generate the special matrices such as the unit matrix, the null matrix, etc.

  • The special kind of matrices can also be performed in the TensorFlow but you have to follow the syntax and have to get the clear concept for the performance.

So, in this tutorial, we have started using the TensorFlow that we had installed in the previous lecture. Some of the steps to launch TensorFlow are the same and you will practice them every day in this course. We have seen how can we apply the basic functions in the TensorFlow related to the matrices. We have seen the types of tensors that are, more or less, similar to the matrices. You will see the advanced information about the same concepts in the tutorials given next as we are moving from the basics to the advance so stay with us for more tutorials.