Overview of Tensorflow (Full Explanation!)

Overview of Tensorflow (Full Explanation!)

In this article we will be giving lots of practical examples of building machine learning models with TensorFlow 2.0. Let us try to understand, what TensorFlow exactly is? This is a logo of Tensor Flow, which is an end-to-end open source platform for machine learning.

Tensorflow has a meaning, TensorFlow is made up of 2 words tensor and flow. Tensor is the multidimensional array and flow is a graph of operations. Internally, TensorFlow implements machine learning algorithms as a graph of operations on multidimensional array. Tensorflow was developed by Google Brain and it was released under Apache 2.0 license in November 2015. Current stable version of TensorFlow is 1.14 and it is a popular GitHub repo with 100 and 29k plus stars.

TensorFlow is a vibrant active community of developers with more than 1800 developers actively contributing to the code base. This course covers concepts from TensorFlow API version 2.0, which is the newest version of TensorFlow. Why do we really care about TensorFlow? TensorFlow provides easy to build and deploy machine learning models for a new comer in machine learning. If you are in expert in machine learning or a machine learning researcher TensorFlow enables, you to build state of machine learning models with Keras functional API and model sub classing APIs.

Another important thing about TensorFlow is that it supports production of machine learning models, anywhere from CPUs, GPUs to edge devices as well as web servers. And, TensorFlow API is available for Python, for Java and for Go programming languages. TensorFlow has a very flexible architecture; it enables easy deployment across different hardware platforms like CPUs, TPUs and GPUs, and computing devices like desktops, servers, mobile devices, and edge devices.

Tensorflow is being used by lots of companies around a world; these companies operate in different domains and are using TensorFlow to carry out build machine learning models in different domains. For example, Google is using TensorFlow to better its products various products like Gmail or doc, Airbnb for example is using TensorFlow to classify images and detect objects in their set of photographs.

AIRBUS is using TensorFlow to detect interesting objects from the satellite imagery and make it available to its customer. TensorFlow is also used for lot of social good applications as well as in the financial domain like PayPal is using TensorFlow for detecting fraudulent transactions, Twitter is using TensorFlow to run tweets. So, you can see that TensorFlow is a versatile product and is being used for developing and deploying machine learning models by companies across different domains.

You can check out some of these case studies on tensorFlow.org website. Let us try to build our first machine learning model with TensorFlow, we call it as TensorFlow hello world. We will train our first machine learning model with TensorFlow API.

This machine learning module helps us recognize hand written digits. We will train the machine learning model with famous MNIST dataset. MNIST dataset contains grace skilled images of hand written digits. There are 60,000 images in training set and 10,000 images in the test set of MNIST dataset. Each image is of size 28 by 28 pixel and each image is tagged with a label that is actual number it represents.

So, this is a Colab; this is a Google colab environment it allows us to run Python programs directly in the browser. Here we code our model in the colab environment, it has got text and code cell. This is an example of a text cell and this is an example of a code cell. In text cell we have written comments or some text that will help us understand what is going on in the colab.

In the code cell we write essentially the Python code, we will first go through the colab cell by cell and then run it. In the first cell; in the first code cell we import required packages and install TensorFlow 2.0 after installing TensorFlow dot 2.0 we import the TensorFlow package.

Next we load the MNIST dataset, mnist dataset is available in the TensorFlow dataset package. So, we can directly load that with this particular command and so, essentially mnist dataset is defined as tf.Keras.dataset.mnist and we load mnist dataset with load data command. The load data command essentially gives us mnist dataset in two tuple, the first tuple contains the training data and the second
tuple contains a test data.

So, we have the training features in x train matrix, y train vector contains the label of the training examples, x test metric contains the features and y test vector contains the label corresponding to each of the examples. The ith entry in x train metric represents features for the ith example and ith entry in the y train gives us the corresponding label. After loading the dataset we normalize the dataset by dividing each pixel value by 255 The normalization helps us achieve faster conversions during training.

Now, that we have loaded the dataset and pre-process state, the next task is to build a model. We will build tf.keras.models.sequential model by stacking the layers. Next we use loss function and an optimizer for the model, we select the sparse categorical cross entropy loss as the loss for this particular model and we choose adam as an optimizer for this particular model. You can note that we first flattener input, so our original input is 28 by 28 pixels.

So, we will flatten it to make it into a vector of size 784 and this particular input is fed into the dense layer which has got 128 units. We use Relu activation in this particular dense layer and this is the only hidden layer that we use in this particular neural network. In addition, we use dropout regularization with a dropout rate of 0.2, the output layer contains 10 units one corresponding to each of the digits between 0 to 9 and we use softmax as an activation function for the output layer.

Now that we have compiled our model the next step is to train the model. We use model.fit function for training the model. The fit function takes the training features and training labels as an argument along with the number of epochs, just to remind you an epoch is one full iteration of the training set. After training the model we evaluate the model on the test set which has the test features and the test labels.

Notice that the model was trained on the training data and it is performance was evaluated on the test data. This ensures that we have a fair estimate of the model performance on unseen data. You can observe that we have specified our model its training and evaluation all in less than 10 lines of code with tf API. This is the ease of use that makes TensorFlow an API of choice for machine learning developers.

Now that we have written the code for model specification, training and evaluation let us execute the code in the notebook to see what kind of performance we achieve on this model. So, in order to run the code, we have to first connect to the Colab environment which we have are already connected here.

After connecting to Colab environment we can execute this notebook cell by cell. This run button over here, if you press this the code in this particular code cell will get executed, alternatively, we can press the control enter button to execute the cell as a keyboard short cut.

So, let us download the TensorFlow 2.0 beta version which we have we have already download in this case. If this is not downloaded it will take some time to download the version from the internet and hence this particular code cell might take a bit longer for you.

Next, we load the MNIST data and normalize it, next we compile our model. So, as we press the train button we can see that the model is getting trained and the progress of the model is shown with a progress bar and in each of the epoch we see some statistics about loss and accuracy and amount of time the training takes per sample. So, you can observe that the loss is going down with each epoch starting with 0.3 we got the loss down to 0.07 and the accuracy is going up, we started with accuracy of 0.91 and accuracy has climbed up all the way up to 0.97.

So, we started with 91 percent accuracy and after 50 epoch we have accuracy of 97 percent. After training the model when we evaluated the evaluated the model we achieve similar performance on the test data. One can see that the loss on test data is very close to the loss on the training data as well as the accuracy that we are getting on the test data is comparable to the accuracy that we are getting on the training data.

So, in this module we build our first TensorFlow model for recognizing hand written digits from MNIST dataset. Just now, we finished building our first machine learning model with TensorFlow, we called it as TensorFlow hello world, you must have observed that we used Python in our browser. So, for most of the exercises in this course we are going to use this tool called Google Colab.

Colab is a Jupyter notebook that can be run from the browser. It uses cloud run time and can run in the browser without you needing to do lot of complicated setup on your machine. Let us try to understand the basic features of Colab. So, that it is easier for you to use in the subsequent practical applications.

So, colab can be accessed by colab.research.Google.com URL and when you open colab you can either load one of your existing notebooks that you can get it from the drive, you can load notebooks from the drive. You can also load notebooks from the GitHub all that you have to do is you have to enter the GitHub URL of the notebook and the notebook will be open for you.

You can also upload your existing Jupyter notebooks through the upload tab and then the notebook will be available to you in the browser that you can run. Then we also have several example colabs that shows functionality of colab like reading external data from drive, sheet and cloud storage. Colab for getting data from Google big query or creating interesting input and output forms in the colab.

So, let us try to start a new Python notebook. So, this is how we start colab we start a new Python notebook, we can save it; we can save a copy in drive or we can save a copy in GitHub let me save a copy in drive and let me call this as “HelloWorld”. So, colab file has an extension ipynb which is exactly same as the extension of Jupyter notebooks and in colab we can seamlessly mix text and the code this mix it a very nice platform to write the documentation along with the code.

So, that it is easier for the reader to follow what is going on in the notebook. In addition to that there are elements of collaboration inbuilt in the colab, one can comment on the cell or one can share the colab with their collaborators with the share button here.

The colab has mainly two cells, one is called code cell and second is called text cell. In code cell we essentially write the Python program whatever we write in the code cell and if we execute the code cell with the Run button over here the content of the code cell is interpreted by the Python interpreter and if it is the valid Python code then only that particular code would be run.

So, let us try to print “Hello World”, in order to run the colab we need to first connect to the cloud run time it is right now connecting it has connected and now initialize it is initializing. Now you can see that you are connected to a cloud run time and we see the status of the RAM and disk in this particular cloud run time. In order to execute the cell we simply need to run the cell and you can see that hello world is being printed over here.

So, the other type of cell is a text cell; in text cell we can write content using what is called as mark down. So, I can write some sample content here this is my first colab and I can highlight some of the content using a visual editor here or if you are familiar with mark down you can straight away use mark down syntax in colab.

Apart from text we can also have links, we can also insert links or we can also insert images, we can also have list of items in colab. So, we can say that in my first colab here we demonstrate how to print using Python print statement and perform basic mathematical operations and you can see that as I was typing the actual output is visible in this part of the screen. So, if you go to the other cell the output of the markdown can be seen over here.

So, let us try to perform addition of two numbers and say that a =3, b=2, y = a + b. And we can simply print and we can see that the addition operation is carried out and we can see here finally, print y we can also write the content of this particular code cell across different code cells let us try to see that.

So, I can write comments like initialize the variables and then I can have simple code cell initializing a to 10 and b to 20, then I can say add a and b, let us say I want to write this a. So, I can also insert mathematical equations using latech I can simply write the code saying that y is equal to a plus b and I can say print y.

And let us write the code to print the addition of two numbers here. Now, we can run each of the cell and then perform the addition and finally, print the number, you can see that a was 10 and b is 20, we added this 2 numbers to get 30 in the resulting variable. So, this is the colab environment, we can also import useful Python libraries like TensorFlow in colab and then we can execute or we can build practical machine learning applications in colab.

One of the great point about colab is that you can execute your Python code in the browser and you do not need to do lot of complicated set up on your own machines. So, this brings us to the end of our first module hope you enjoyed it .

Share this post ...

7 thoughts on “Overview of Tensorflow (Full Explanation!)”

  1. I think that machine learning Will replace the way we think and act. I am so happy because this post is so insightful.

    1. Thanks Hartono! Yes, of course! we still have many things that we can do with machine learning, and in the future we believe that machine learning can be more useful for human activities.

  2. Pingback: Data Science: Composite Examples | My Universal NK

  3. Pingback: The Future of JavaScript & Machine Learning: What you should know! | My Universal NK

  4. Pingback: Steps in Machine Learning Process | My Universal NK

  5. Pingback: Gradient Descent | My Universal NK

  6. Pingback: Introduction to Tensors: Explained! | My Universal NK

Leave a Reply

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