Home » Software Development » TensorFlow Tutorial For Beginners

About Shubham Aggarwal

Shubham Aggarwal
Shubham is a Java Backend and Data Analytics Engineer with more than 3 years of experience in building quality products with Spring Boot, MongoDB, Elasticsearch, MySQL, Docker, AWS, Git, PrestoDB tools and I have a deep knowledge and passion towards analytics, Micro-service based architecture, design patterns, antipatterns and software design thinking.

TensorFlow Tutorial For Beginners

1. Introduction

In this post, we feature a comprehensive article on TensorFlow Tutorial. We will cover the basics of the TensorFlow library and try to understand different use-cases in which it can be used. Personally, I’ve looked and heard about TensorFlow so much that I am not sure what it is actually made for and what precisely, is its advantages and superpowers. That is why, at the beginning of this lesson, we will try to cover what are the use-cases in which you will want to try TensorFlow library and what advantages it can offer to you as compared to its alternatives (if there are any).

A lot of different definitions can be provided for TF but we will stick with a simple one. Based on its website, here is one-liner through which TensorFlow is described:

The core open source library to help you develop and train ML models.

Well, that was pretty basic and straightforward. Maybe that’s it! In this lesson, we will not try to cover the complete ground with TensorFlow because that cannot be done in a single lesson. TensorFlow is so huge and important that almost everything can be completed with it. Although everything is a big word, at the beginning of this lesson, we will try to mention some of the use-cases where TensorFlow is put to use in production. Let’s find out more about those use-cases.

2. Top use-cases for TensorFlow

Even as a beginner, I have struggled to understand the importance, use-cases and real advantages of each Data Science and Machine Learning related library because there are so many of them that you couldn’t even count them. Each one of them is popular, widely used, ready for production usage etc. In this section, we will only look at TensorFlow and its top use-cases where it is understood to be a perfect fit for use. Here are they:

  • Audio recognition – As TensorFlow is based on Neural networks, it can manage many kind of signals, audio & voice signals are one of them. Even with some noise present in the signal, TensorFlow can provide you excellent results even when you are doing complex operations like sentiment analysis or voice search in your applications.
  • Textual analysis – With TensorFlow, it is possible to perform semantic analysis on the text as well. With the implementation of summarisation inside TensorFlow, it is also possible to produce news headline automatically from a given paragraph of news content. Google Translate also makes use of TensorFlow for translating complex dialects into their plain versions.
  • Image Recognition – TensorFlow object recognition algorithms help to identify small objects in big pictures, people in real-time video feeds. It can also identify irregular patterns of skin cells at the microscopic level to identify a chronic illness better than a human doctor.
  • Time-series analysis – The simplest example for time-series analysis would be the recommendations you get via Google ads whenever you search for something on the web. It is not a surprise how Google is able to identify our needs so fast and translate into a monetary benefit by showing us exactly what we need.
  • Classification – One of the real world examples of classification with TensorFlow is what Airbnb does. Airbnb uses TensorFlow in an image classification system where TensorFlow is able to classify an image into different sections of the house, like balcony, dining room, swimming pool, etc. This way, the Airbnb saves a lot of human hours to categorize the images into sections which now can be presented to the users with optimal classification.

As we can infer, it sounds like TensorFlow can actually be used anywhere there is a problem waiting to be solved. Actually, that is it. TensorFlow can be used anywhere where you want to work with patterns, recognition, analysis and it can help you take your products to market with its community support. Many production level deployment systems are also present which supports TensorFlow programs to run on the cloud like Algorithmia. Before we can start to write some programs with TensorFlow, let us first understand what tensors are and what is their importance.

3. What are Tensors?

Tensors are the commons terms we hear whenever we talk about TensorFlow. Simply put, Tensors are just a way to store n-dimensional data in a deep learning (or simply, machine learning) environment. If you know how to imaging 1-d and 2-d arrays, n-d arrays are quite similar.

TensorFlow Tutorial - Multi-dimensional
Multi-dimensional Tensors

Just like what is represented above, Tensors are simply n-dimensional arrays which are optimized to store and process n-dimensions of data which is considerably complex. Each of the dimension in a Tensor can be considered as a feature in Machine Learning. This simply means that more attributes a dataset has, more dimension & complex nature will its Tensor represents.

One thing to understand is the difference between a matrix and a Tensor. Based on one of the comments on this SO question, a matrix is just a two-dimensional table to organize information and a tensor is just its generalization. You can think of a tensor as a higher-dimensional way to organize information. So a matrix (5×5 for example) is a tensor of rank 2. And a tensor of rank 3 would be a “3D-matrix” like a 5x5x5 matrix. Please note that the dimension of the tensor is called its rank.

4. Installing TensorFlow

TensorFlow is compatible with both Python 2 and Python 3. Apart from Python, TensorFlow API is also available in C++, Haskell, Java, Go, Rust and R (3rd party library). This means that it is quite extensive in development and readily available for any framework and programming language you might be diving into.

For using TensorFlow on your local environment, using pip is the simplest way to install it. Just use the following command:

pip install tensorflow

I will be making use of the Anaconda manager for all the code in this lesson. I will launch a Jupyter Notebook for the same via the Anaconda manager:

TensorFlow Tutorial - Anaconda
Anaconda Navigator

We are using Anaconda Navigator’s based Jupyter notebooks as most of the required packages are already installed in the environment prepared by Anaconda, this means that we can directly dive into the code snippets we want to write. If it doesn’t work for you by default, just make use of the pip command we mentioned earlier and relaunch the Anaconda kernel for the program to work.

5. Nearest Neighbours Algorithm

The nearest neighbor algorithm is one of the commonly used Classification Algorithm. Other examples for Classification Algorithms are Naïve Bayes’ classifier and Decision tree classifier.

The nearest neighbor algorithm is a lazy learning algorithm, which simply means the algorithm delay processing the training data until a new instance must be classified and so, a new instance is examined with respect to all training instances (observations) and is given its target value. The instance-based methods keep using the training data from its memory without learning any knowledge from the data. Every new instance requires the entire training data to be processed due to which classification is delayed and is called lazy learning.

Just like what the algorithm sounds, there must be a way to identify the distance to say that the neighbor is the nearest one. The nearest neighbors are defined in terms of the Euclidean distance. To quickly understand the exact meaning of this algorithm, let’s consider the following two-dimensional points belonging to two different classes represented as two colors:

TensorFlow Tutorial - Nearest Neighbours
Nearest Neighbours

The points are representing training instances by the corresponding feature vectors. The feature vector is an (x, y) coordinate pair for each point. Let a new testing instance arrives (shown as a blue dot) and is plotted on the same coordinate plane:

TensorFlow Tutorial - New point in Graph
New point in Graph

The k-nearest neighbour algorithm requires ‘k’ to be defined as a value. For example, if k=3, we compute the distances of the new instance from all training data and mark three closest points. If the three closest points belong to the same family, we can easily assign a new point to the identified family.

6. Implementing Nearest Neighbours with TensorFlow

In this section, we will implement the nearest neighbour algorithm we studied in the last section using TensorFlow. To implement a simple but yet important example, we will make use of a dataset which is closer to the real world. We will use the MNIST database of handwritten digits.

To start, we should add the appropriate import statements in our Python program:

import numpy as np
import tensorflow as tf

Next, it is time to import the dataset we mentioned. It is very easy to import the mentioned handwritten dataset as TensorFlow includes it, we just have to inform it that the dataset must be made available at a specific location. Let’s add the code snippet which will do just that:

# Import Handwritten digits data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

Although with some warning, we will get a success message that the data has been downloaded over the network in the specified directory. Here is the warnings (and the success message) in the output:

TensorFlow Tutorial - Dataset
Dataset downloaded

Now that the data is downloaded, we will do what should be done with any dataset to be prepared for a Machine Learning algorithm, we will split it in two parts, one of which will be used to train the ML model and another split will be used to test the model developed. Here is the code snippet which will split the data into two parts:

X_training, Y_training = mnist.train.next_batch(5000)
X_test, Y_test = mnist.test.next_batch(200)

We have correctly split the dataset available into two parts. Now, based on the available training data, we need to plot graphs, plot each of the available data point on the graph so that it can be classified as one of the digit. So, let’s calculate the distance for each of the point here. Here is the code snippet for next steps:

xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])

# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
# Prediction: Get Nearest neighbor
pred = tf.argmin(distance, 0)

accuracy = 0.

# Assign default value)
init = tf.global_variables_initializer()

Now that all the variables are in place to get the neighbour which is derived to be the nearest one, we can finally start training the data and provide details regarding the accuracy of our model in the end. We will start by constructing a TF session under which all computations will be done. Here is the code snippet for next steps:

with tf.Session() as sess:
    sess.run(init)

    # loop over test data
    for i in range(len(X_test)):
        # Get nearest neighbor
        nn_index = sess.run(pred, feed_dict={xtr: X_training, xte: X_test[i, :]})
        # Get nearest neighbor class label and compare it to its true label
        print("Test", i, "Prediction:", np.argmax(Y_training[nn_index]), \
            "True Class:", np.argmax(Y_test[i]))
        # Calculate accuracy
        if np.argmax(Y_training[nn_index]) == np.argmax(Y_test[i]):
            accuracy += 1./len(X_test)
    print("Done!")
    print("Accuracy:", accuracy)

As we know about TensorFlow, we must start a TensorFlow session before we make any computations with it. That is what we did with the opening statement. After opening the session, we looped over the test data, got their nearest neighbour and made a comparison to its true label.

After that, we can finally provide the accuracy of the algorithm, let’s see how accurate our current model comes out to be:

TensorFlow Tutorial - Digit prediction
Digit prediction accuracy

As shown in the output above, our algorithm ran with about 91% accuracy which is quite high. This is one of the reasons for TensorFlow being so successful in production. Not only can it run so fast but it can also produce amazingly accurate results. There are many other reasons to select TensorFlow for your use-case as well, such as:

  • Large community of similar users are present for any problem which you can think of solving with TensorFlow. This means that if you face any issue, it is 99% chance that a lot of people has faced exactly the same problem which means, you can find a probable solution as well.
  • Inclusion of Keras in TensorFlow means that a lot advanced models can be built on top of TensorFlow package without anymore requirements. This also means that if you use Keras from inside of TensorFlow, you’ll also get the computational power that comes with TensorFlow.
  • TensorBoard is something which allows us to easily visualise the metrics of the developed model and is very straight forward to work with.

Although TensorFlow sounds too good to be true, it has some shortcomings as well. It is beneficial to quote from one of the excerpts from the Hacker News here:

  • Tensorflow has a way too large API surface area: parsing command lines arguments handling, unit test runners, logging, help formatting strings… most of those are not as good as available counterparts in python.
  • The C++ and Go versions are radically different from the Python version. Limited code reuse, different APIs, not maintained or documented with the same attention.
  • The technical debt in the source code is huge. For instance, There are 3 redundant implementations in the source code of a safe division (_safe_div), with slightly different interfaces (sometimes with default params, sometimes not). It’s technical debt.
  • If TensorFlow is used in cloud for a problem which is not so scalable, it can incur a lot of infrastructure cost for the deployment due to the complex computations it performs with Tensors and connected graphs.

In every way, it reminds me of Angular.io project. A failed promise to be true multi-language, failing to use the expressiveness of python, with a super large API that tries to do things we didn’t ask it to do and a lack of general sounding architecture.

7. Why is TensorFlow so slow?

Although the headline of the section is confusing, we will explain what we will discuss here. TensorFlow can be slow sometimes but it is the way it is because we allow it to be. The data structure and computations TensorFlow performs is of huge scale. So, we have some points we can mention here:

  • We should check TensorFlow based algorithms speed on a cluster instead of just a home computer. This is because of the computations TensorFlow needs to perform to produce highly accurate results.
  • Most of the slowness in program execution in production comes from the read pipelines as network waits too long to read from the disk. For the same reason, TFRecords, an internal file format for TensorFlow exists. For the same reason, part of the training code should be processed on the CPU.
  • The quality of a TensorFlow model is very high which means that even if it takes longer to run and train a model, we can be sure that it will be of high quality. I think that is a sacrifice I can make if I want models to be highly accurate. After all, accuracy is something which matters a lot in ML models.
  • It may be slow because TF is built that way, a lot of libraries are part of TF by default and are not optional to download.

Based on the points above, it is quite clear that even though TensorFlow might behave a little slow in production, it’s worth the accuracy required in a production system. And not just its accuracy, even the community supports it receives is incredible.

8. Where to go next?

To continue to learn TensorFlow and it’s more applications, we can recommend some of the steps you can take to enhance your learning on the topic:

  • If you want to continue with the handwritten digit dataset, try to increase its accuracy to 95% and deploy it as a usable real-time model in your application
  • Try to implement other classification algorithms with TensorFlow, like Naive Bayes algorithm
  • Try to start with developing simple neural networks and move on to Convolutional neural networks and recurrent neural networks
  • A lot of optimizers are available in the TensorFlow package, try to play around those and learn there internal working and how are results computed in each of them
  • Another good project for TensorFlow will be to construct a live feed video camera which identified people. It also keeps count of different faces it captures. This means that if the same face appears in front of the camera at a gap of 30 seconds, it will increment its count. Or just capture the number plate data of cars which it captures, which can be used in real-time traffic monitoring systems.

TensorFlow is an extremely big community where finding problems to solve is very easy. This means that one cannot go out of problems which can be solved much more efficiently with TensorFlow. Starting to work with TensorFlow can be a little bit tricky but know, there is always help available out there.

9. TensorFlow Tutorial – Conclusion

In this lesson, we gained a lot of knowledge regarding one of the most used and popular Machine Learning and Data Science related package available, TensorFlow, which is backed up by Google for a very long time now. Usually, with ML, everything starts to sound complex and that is why we started by discussing the use-cases where TensorFlow can be used and it sounded like I was saying, everywhere. TensorFlow is so powerful due to the fact that a lot of packages have been included in itself, like Keras. Sometimes, it behaves like a little bit slow (and bulkier) package as well, for the same reason that many libraries have been made part of it and those cannot be excluded when you download TensorFlow.

After use-cases, we also covered what data structures are used by TensorFlow to complete the computations it is ought to complete and we finally provided a very real example where we showed the high accuracy of a simple and effective TensorFlow based ML model. One thing for sure, TensorFlow is something no one ever will be afraid to use in production, even in critical cases like Fraud detection, medical analysis, and treatments. This gives a lot of applause to TensorFlow for being so versatile and high-performant in terms of the accuracy of the solution it produces.

10. Download the Source Code

This was an example of Machine Learning with TensorFlow.
Download
You can download the full source code of this example here: TensorFlow Tutorial For Beginners
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of