Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconArtificial Intelligencebreadcumb forward arrow iconDeep Learning with TensorFlow 2.0: Everything You Need to Know

Deep Learning with TensorFlow 2.0: Everything You Need to Know

Last updated:
5th Jan, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
Deep Learning with TensorFlow 2.0: Everything You Need to Know

TensorFlow is the Machine Learning framework by Google. It is primarily used for Deep Learning related tasks and seamlessly integrates with other Google APIs as well. TensorFlow is clearly one of the most used libraries for Deep Learning in the industry right now and totally worth learning!

Best Machine Learning and AI Courses Online

By the end of this tutorial, you will have knowledge of the following.

  • What is TensorFlow?
  • What is new in TF 2.0?
  • TensorFlow vs Keras
  • Installing TensorFlow
  • Image Classifier in TensorFlow

What Is TensorFlow?

TensorFlow started as an Open-Source Deep Learning library by Google and now is a complete framework for end to end Machine Learning processes. You might be wondering why Google chose this name and what does “Tensor” mean.

Ads of upGrad blog

What Is A Tensor?

Tensors are effectively Multi-dimensional arrays that enable you to perform complex operations on multi-dimensional arrays. However, they are not just an N-dimensional array.

A Tensor also includes the transformations such as dot product, addition, matrix multiplication, etc.

In-demand Machine Learning Skills

Get Machine Learning Certification from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.

But Why Are They Important?

Tensors are not new. They have been in use since long, but their characteristics are heavily exploited in the area of Deep Learning, where the data is usually huge and of multiple dimensions.

Tensors, just like Numpy arrays, also have a shape and data type. All tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one.

But what makes them different from usual Numpy arrays is their ability to utilize GPU memory and compute power which is of the utmost importance when data is high-dimensional and size is in millions or more.

Tensors are highly used in Deep Learning frameworks such as Facebook’s Pytorch and Google’s TensorFlow, which is even named after them !

Google has also developed another AI accelerator, called Tensor Processing Unit (TPU), especially for TensorFlow which takes the optimization to a next level altogether!

Read: Regularization in Deep Learning

What’s New In TF 2.0 ?

Google had released the first version of TensorFlow 1 in 2015 by the Google Brain Team.

Using TensorFlow 1.x to make neural networks was not an easy task as it required a lot of code to be written.

Lazy Evaluation Vs Eager Evaluation

With TensorFlow 1.x, there used to be a need to make Sessions and run those sessions to generate the output of any “graph”. Let’s understand this with below code

import tensorflow as tf

a = tf.constant(1)
b = tf.constant(2)
c = tf.add(a, b)
print(c)

 

Running the above code won’t give you the output you want, i.e., 3. This is because TensorFlow 1.x worked in sessions.

A session is a type of environment that contains all the variables and the transformations that it needs to do.

A graph of transformations was made which was not evaluated until it was specifically called by running tf.session.run().

Therefore, the above code will return what you expect if you do:

tf.Session.run(c)

This is called Lazy evaluation. As it lazily waits until it is specifically told to run.

This lengthy and complicated process needed to be resolved and hence the need for TensorFlow 2.x came.

TF 2.x comes with Eager evaluation by default which makes it really easy for us to code and run the processes.

There are no sessions now and the neural network training which took 100 lines in TF 1.x takes less than 20 with TF 2.x.

TensorFlow’s eager execution is an imperative programming environment that evaluates operations immediately, without building graphs: operations return concrete values instead of constructing a computational graph to run later.

This makes it easy to get started with TensorFlow and debug models, and it reduces boilerplate as well.

TensorFlow Vs Keras

The question is really not TensorFlow vs Keras. It is TensorFlow with Keras. Keras provided a high-level API over TensorFlow 1.x which made it very easy to work with it.

Now with TF 2.0, TensorFlow has officially made Keras a part of its API for model designing and training with tf.keras.

All the code which was earlier done in Keras is now suggested to be done with tf.keras in TF 2.0 as it lets it use all the TensorFlow components and ecosystem such as:

  • TensorFlow Serving which is used to serve/deploy TensorFlow models seamlessly.
  • TensorFlow Lite which is the mobile version of TensorFlow capable of running on Android and IOS.
  • TensorFlow.js is a collection of APIs that allows us to build and train models using either the low-level JavaScript linear algebra library or the high-level layers API. Hence, deep learning models can be trained and run in a browser.
  • TensorBoard is a suite of visualization tools to understand, debug, and optimize TensorFlow programs.

Installing TensorFlow

If you are new to Machine Learning then the easiest way to get things rolling is by opening up a Colab Notebook. Just go to https://colab.research.google.com/ and click on “New Python 3 Notebook.”

Make sure the kernel says “connected” on the top right. Good news, TensorFlow comes pre-installed in Google Colab.

Voila! You’re all set.

To check if you’re using the right version, run the below snippet.

import tensorflow as tf
tf.__version__

It should say any version above 2.0.0 and you’re good to go.

Image Classifier In TensorFlow

Let’s now go over the “Hello World” of Deep Learning problems – the MNIST dataset.

We’ll build a short neural network to predict on the MNIST dataset. We will follow the below steps.

  1. Build a neural network that classifies images.
  2. Train a neural network.
  3. Evaluate the accuracy of the model

Importing TensorFlow.

import tensorflow as tf

Loading the MNIST data.

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

Building a tf.keras.Sequential model by stacking up the layers.

We’d need to choose an optimizer and a loss function as well for the model to train upon.

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=’relu’),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])

Defining the Sparse Categorical Cross Entropy loss function.

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

Compiling the model.

model.compile(optimizer=’adam’,
loss=loss_fn,
metrics=[‘accuracy’])

Training the model with 5 epochs.

model.fit(x_train, y_train, epochs=5)

Evaluating the model.

model.evaluate(x_test,  y_test, verbose=2)

 

313/313 – 0s – loss: 0.0825 – accuracy: 0.9753

[0.082541823387146, 0.9753000140190125]

The image classifier is now trained to ~98% accuracy on this dataset.

Before You Go

TensorFlow 2 focuses on simplicity and ease of use, with updates like eager execution, intuitive higher-level APIs, and flexible model building on any platform.

TensorFlow is the go-to library/tool for any deep learning task these days. The other most used and popular library is Facebook’s PyTorch.

TensorFlow’s extended ecosystem makes it a great place to begin your Deep Learning journey. It is easy to understand and more importantly, easy to implement.

The best place to start is with the user-friendly Sequential API. You can create models by plugging together building blocks. Learn more about deep learning techniques.

Ads of upGrad blog

Popular AI and ML Blogs & Free Courses

Conclusion

So, now that you have a detailed idea of all the major Deep learning frameworks out there, you can make an informed decision and choose the one that suits your project best.

If you are interested to know more about deep learning and artificial intelligence, check out our PG Diploma in Machine Learning and AI program which is designed for working professionals and provide 30+ case studies & assignments, 25+ industry mentorship sessions, 5+ practical hands-on capstone projects, more than 450 hours of rigorous training & job placement assistance with top firms.

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.
Get Free Consultation

Selectcaret down icon
Select Area of interestcaret down icon
Select Work Experiencecaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular Machine Learning Course

Explore Free Courses

Suggested Blogs

15 Interesting MATLAB Project Ideas & Topics For Beginners [2024]
82457
Diving into the world of engineering and data science, I’ve discovered the potential of MATLAB as an indispensable tool. It has accelerated my c
Read More

by Pavan Vadapalli

09 Jul 2024

5 Types of Research Design: Elements and Characteristics
47126
The reliability and quality of your research depend upon several factors such as determination of target audience, the survey of a sample population,
Read More

by Pavan Vadapalli

07 Jul 2024

Biological Neural Network: Importance, Components & Comparison
50612
Humans have made several attempts to mimic the biological systems, and one of them is artificial neural networks inspired by the biological neural net
Read More

by Pavan Vadapalli

04 Jul 2024

Production System in Artificial Intelligence and its Characteristics
86790
The AI market has witnessed rapid growth on the international level, and it is predicted to show a CAGR of 37.3% from 2023 to 2030. The production sys
Read More

by Pavan Vadapalli

03 Jul 2024

AI vs Human Intelligence: Difference Between AI & Human Intelligence
112983
In this article, you will learn about AI vs Human Intelligence, Difference Between AI & Human Intelligence. Definition of AI & Human Intelli
Read More

by Pavan Vadapalli

01 Jul 2024

Career Opportunities in Artificial Intelligence: List of Various Job Roles
89547
Artificial Intelligence or AI career opportunities have escalated recently due to its surging demands in industries. The hype that AI will create tons
Read More

by Pavan Vadapalli

26 Jun 2024

Gini Index for Decision Trees: Mechanism, Perfect & Imperfect Split With Examples
70805
As you start learning about supervised learning, it’s important to get acquainted with the concept of decision trees. Decision trees are akin to
Read More

by MK Gurucharan

24 Jun 2024

Random Forest Vs Decision Tree: Difference Between Random Forest and Decision Tree
51730
Recent advancements have paved the growth of multiple algorithms. These new and blazing algorithms have set the data on fire. They help in handling da
Read More

by Pavan Vadapalli

24 Jun 2024

Basic CNN Architecture: Explaining 5 Layers of Convolutional Neural Network
270717
Introduction In the last few years of the IT industry, there has been a huge demand for once particular skill set known as Deep Learning. Deep Learni
Read More

by MK Gurucharan

21 Jun 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon