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

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular Machine Learning Course

Explore Free Courses

Suggested Blogs

Artificial Intelligence course fees
5381
Artificial intelligence (AI) was one of the most used words in 2023, which emphasizes how important and widespread this technology has become. If you
Read More

by venkatesh Rajanala

29 Feb 2024

Artificial Intelligence in Banking 2024: Examples & Challenges
6104
Introduction Millennials and their changing preferences have led to a wide-scale disruption of daily processes in many industries and a simultaneous g
Read More

by Pavan Vadapalli

27 Feb 2024

Top 9 Python Libraries for Machine Learning in 2024
75570
Machine learning is the most algorithm-intense field in computer science. Gone are those days when people had to code all algorithms for machine learn
Read More

by upGrad

19 Feb 2024

Top 15 IoT Interview Questions & Answers 2024 – For Beginners & Experienced
64423
These days, the minute you indulge in any technology-oriented discussion, interview questions on cloud computing come up in some form or the other. Th
Read More

by Kechit Goyal

19 Feb 2024

Data Preprocessing in Machine Learning: 7 Easy Steps To Follow
152710
Summary: In this article, you will learn about data preprocessing in Machine Learning: 7 easy steps to follow. Acquire the dataset Import all the cr
Read More

by Kechit Goyal

18 Feb 2024

Artificial Intelligence Salary in India [For Beginners & Experienced] in 2024
908649
Artificial Intelligence (AI) has been one of the hottest buzzwords in the tech sphere for quite some time now. As Data Science is advancing, both AI a
Read More

by upGrad

18 Feb 2024

24 Exciting IoT Project Ideas & Topics For Beginners 2024 [Latest]
759455
Summary: In this article, you will learn the 24 Exciting IoT Project Ideas & Topics. Take a glimpse at the project ideas listed below. Smart Agr
Read More

by Kechit Goyal

18 Feb 2024

Natural Language Processing (NLP) Projects & Topics For Beginners [2023]
107585
What are Natural Language Processing Projects? NLP project ideas advanced encompass various applications and research areas that leverage computation
Read More

by Pavan Vadapalli

17 Feb 2024

45+ Interesting Machine Learning Project Ideas For Beginners [2024]
328101
Summary: In this Article, you will learn Stock Prices Predictor Sports Predictor Develop A Sentiment Analyzer Enhance Healthcare Prepare ML Algorith
Read More

by Jaideep Khare

16 Feb 2024

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