Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024

Applications of Tensorflow

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

What is Tensorflow?

Tensorflow is a Machine Learning library built on top of C++, Python, and CUDA by Google Brain Team. We use Tensorflow to create Machine Learning models; it is suitable for newbies and experienced persons.


Building a simple ML model with Tensorflow


a. Installing Tensorflow in the python environment

!pip install tensorflow==2.0.0


b. Importing Tensorflow in the notebook

import tensorflow as tf


c. Loading the MNIST Digits dataset available in TF library

mnist = tf.keras.datasets.mnist


d. Splitting the dataset into training-testing and normalizing the pixels by dividing with 255

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


e. Building a simple Convolutional Neural Network with 1 Hidden layer (having 128 neurons) and 1 Output layer (having 10 neurons)

model = tf.keras.models.Sequential([
   tf.keras.layers.Flatten(input_shape=(28, 28)),
   tf.keras.layers.Dense(128, activation='relu'),
   tf.keras.layers.Dense(10, activation='softmax')


f. Configuring the model for training with the following:

Optimizer - Adam

Loss Function - Sparse Categorical Cross-entropy

Evaluation Metrics - Accuracy



g. Training the Image-Classifier model on the Train dataset (with five iterations), y_train, epochs=5)


h. Evaluating the model with the Test dataset

model.evaluate(x_test, y_test)




Result: The model predicts results on the unseen data with close to 98% accuracy.


Tensorflow Applications

Tensorflow has many applications because it makes image augmentation, complex model training & many more deep learning tasks easier to implement. Check out this blog to learn more about that.


Real-World systems using Tensorflow

Nowadays, tech-giant companies like Airbnb, Lenovo, Intel, Google, Cocacola, Twitter, MI, NVIDIA, Uber, Qualcomm, and many more are using Tensorflow.


Google uses TensorFlow in many of its products like Google Translate, Google Search, GMAIL, etc.


Paypal is using the Machine Learning capabilities of Tensorflow for the task of Fraud Detection in online transactions. Customers feel safe and secure when these cutting-edge technologies are deployed to protect their bank accounts.


Spotify recommends songs and podcasts to its users by training a machine learning model using their customer’s previous data. The machine learning infrastructure of Spotify is built on top of Tensorflow.


Airbus extracts information from the satellite images and delivers good insights to its clients using Tensorflow.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Classification of Movie Reviews with Tensorflow

We’ll build a simple model using Tensorflow to classify the movie reviews as positive or negative.


We’ll use the IMDB Movies dataset for this task. The dataset is available in the Tensorflow library. It has 50,000 polar reviews, binary classified as either positive or negative. Out of 50K, 25K are for training, and 25K are for testing purposes.


Importing Necessary Libraries

import numpy as np

import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds

import matplotlib.pyplot as plt

Loading the dataset and splitting it into Training and Testing

train_data, test_data = tfds.load(name="imdb_reviews", split=["train", "test"], batch_size=-1, as_supervised=True)

train_examples, train_labels = tfds.as_numpy(train_data)
test_examples, test_labels = tfds.as_numpy(test_data)


The testing and training set both contain 25,000 reviews.

Example Review



Output: This was an absolutely terrible movie. Don't be lured in by Christopher Walken or Michael Ironside. Both are great actors, but this must simply be their worst role in history. Even their great acting could not redeem this movie's ridiculous storyline. This movie is an early nineties US propaganda piece. The most pathetic scenes were those when the Columbian rebels were making their cases for revolutions. Maria Conchita Alonso appeared phony, and her pseudo-love affair with Walken was nothing but a pathetic emotional plug in a movie that was devoid of any real meaning. I am disappointed that there are movies like this, ruining actor's like Christopher Walken's good name. I could barely sit through it.




Output: array([0])


This review is negative because corresponding to the 1st index, we have 0 stored in the labels array.


Building the Model

We’ll convert the text into embeddings vectors to represent the text data. We’ll be using pre-trained text embedding as the first layer so that we don’t have to do all the hefty text-preprocessing.

We’re using this model for text embedding and transfer learning.


There will be 16 neurons in the first and the only hidden layer in our model and one in the output layer. The last neuron will predict 1 or 0 depending on the positive or negative review.

model = ""
hub_layer = hub.KerasLayer(model, input_shape=[], dtype=tf.string, trainable=True)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(16, activation='relu'))




Configuring the Model

Since our task is binary classification, we’ll use the Binary Crossentropy to calculate the loss and Binary accuracy metrics to evaluate the model.

             metrics=[tf.metrics.BinaryAccuracy(threshold=0.0, name='accuracy')])


Training the model with the Training Dataset

history =, train_labels,
                   validation_data=(x_val, y_val),




Evaluating the model’s performance with the Testing Dataset

results = model.evaluate(test_examples, test_labels)





Our model gives an accuracy close to 84% with the unseen dataset. The loss is around 83%. Considering our model is elementary, the result is pretty good. We can increase the accuracy by changing hyperparameters or changing the number of hidden layers.

Frequently Asked Questions

Q1. What are the different types of datasets available in the TensorFlow library?

Ans. There are many types of datasets that are available in TensorFlow, such as:

  • Audio Datasets
  • Graphs type
  • Image Datasets
  • Image Classification
  • Object Detection
  • Text


Q2. What is ImageDataGenerator?

Ans. ImageDataGenerator is one of the features of tensorflow.keras API. We use it to perform data augmentation, such that we can train our model with different new combinations of data. With ImageDataGenerator, we do not generate new images directly, but the dataset images are transformed dynamically.


Q3. What are the basic steps in building an ML model using TensorFlow?

Ans. We can build a model with Tensorflow by following these easy steps:

  1. Installing and importing Tensorflow
  2. Loading the dataset
  3. Splitting the dataset in training and testing
  4. Initializing the model
  5. Configuring the model
  6. Training the model on the available dataset
  7. Evaluating the model on the unseen dataset


Q4. Who developed Tensorflow and why?

Ans. Google’s Brain Team developed Tensorflow with the sole intention of making Machine Learning and Deep Learning tasks easier for every individual. Tensorflow has many users ranging from novice to expert.

Key Takeaways

Check out this link if you are a Machine Learning enthusiast or want to brush up your knowledge with ML blogs.

 If you are preparing for the upcoming Campus Placements, don't worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.

Topics covered
What is Tensorflow?
Building a simple ML model with Tensorflow
Tensorflow Applications
Real-World systems using Tensorflow
Classification of Movie Reviews with Tensorflow
Importing Necessary Libraries
Loading the dataset and splitting it into Training and Testing
Example Review
Building the Model
Configuring the Model
Training the model with the Training Dataset
Evaluating the model’s performance with the Testing Dataset
Frequently Asked Questions
Key Takeaways