AfterAcademy Tech
•
20 Nov 2019

Tensorflow is an awesome open-source deep-learning library for everyone. Now that Tensorflow 2.0 has been released. There are many great features available in 2.0. Tensorflow 2.0 is a major upgrade to Tensorflow 1.x. In this blog post, we will go through the step by step guide on how to use Tensorflow 2.0 for training the model in Machine Learning. This blog is for both beginners as well as for advanced users who want to get started with Tensorflow 2.0 for Machine Learning.
In this blog, we are going to cover the following topics:
Tensorflow 2.0 is released so that it can be easily used by both beginners and experts. Things that make Tensorflow 2.0 better than other libraries of Machine Learning include:
a = tf.constant(5)
b = tf.constant(3)
c = a * b
with tf.Session() as sess:
print(sess.run(c))
But due to eager execution, Tensorflow 2.0 has simplified the code.
a = tf.constant(5)
b = tf.constant(3)
c = a * b
print(c)
Check the python version of the system by following code on the command prompt.
$ python --version
If not upgraded, upgrade it as follows:-
python -m pip install --upgrade pip
pip install tensorflowimport tensorflow as tf
If there is no error, then the Tensorflow is installed properly.
To use Tensorflow2.0 on Jupyter notebook, we need to follow these steps:-
import tensorflow as tf
If there is no error, then the TensorFlow is installed properly.
Google Colab is a free cloud service that supports free GPU where we can develop Machine Learning programs and improve our Python skills. We can use Tensorflow2.0 with Google Colab by following steps:-
!pip uninstall tensorflow
And then install it by
!pip install tensorflow
"""To import tensorflow_datasets package."""
import tensorflow_datasets as tfds
"""tfds.load is used to load the dataset from tensorflow_dataset.
After this, dataset will start to download unless you specify download=False"""
mnist_train = tfds.load(name="mnist", split="train")
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, activation='softmax')])
To add a dense layer of with 32 units of model, we code it as:
model.add(layers.Dense(32, activation='relu'))
Similarly, Tensorflow 2.0 has different new APIs which are easy to use.
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)

Image Source: Tensorflow Official
Tensorflow is having Tensorflow.js for browser and Tensorflow Lite for mobile platforms.
For a beginner, we need to understand how a Machine Learning model is trained. Then, understand which functions or APIs to use to implement the model.

Image Source: Tensorflow Official
import tensorflow as tf
"""The first phase is data ingestion and transformation.
Here, we take mnist dataset from tensorflow and then split it into training set and test set.
Training set trains the model and test-set will test how accurate the model is.
We divide them by 255 because the value of data ranges from 0 to 255.
Now by dividing it, the range is between 0 to 1."""
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
"""The code written below describes the model building phase.
Here we use a sequential layer in the model which means a sequence of the layer.
Flatten reduces the dimension of the model and dense adds layer of the neuron.
Each layer needs an activation function which is relu and softmax."""
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, activation='softmax')])
"""Then compile the model with an optimizer and a loss function.
Then, we train the data so that our dataset gives accurate results."""
model.compile(optimizer='adam',loss ='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
"""This function gives us how much accurate is our model on the test set."""
model.evaluate(x_test, y_test, verbose=2)
Tensorflow 2.0 provides that flexibility in the code so that we can set the parameter by ourselves and best fit the model. We can shuffle the dataset and then divide them into training and test set by making batches of data.
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Dropout
from tensorflow.keras import Model
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
"""10000 is buffer_size, 60000/32 = 1875 is the size of train_ds"""
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
Now, a class is made where the skeleton of the model is prepared.
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.flatten = Flatten(input_shape=(28, 28))
self.dense_1 = Dense(128, activation='relu')
self.dropout = Dropout(0.2)
self.dense_2 = Dense(10, activation='softmax')
def call(self, x):
x = self.flatten(x)
x = self.dense_1(x)
x = self.dropout(x)
return self.dense_2(x)
model = MyModel()
Here, MyModel class has the details of a layer of the model. Instead of using predefined functions, the object-oriented approach is used to set the parameter of the layers of the model.
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()
"""loss function and optimizer are used for the compiling of the model."""
train_loss = tf.keras.metrics.Mean(name= 'train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
Gradient tape will show us the variation of weights and biases of each node over a period of time. Gradient Tape tracks the automatic differentiation that occurs during the training. tape.gradient() is used to store the track of all gradients during the training.
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
The training of the model is done by running the training step to the number of epochs. Epochs define the number of times the algorithm will run through the entire dataset. The test-set is set up to predict the accuracy of the result.
@tf.function
def test_step(images, labels):
predictions = model(images)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
EPOCHS = 5
for epoch in range(EPOCHS):
for images, labels in train_ds:
train_step(images, labels)
for test_images, test_labels in test_ds:
test_step(test_images, test_labels)
After training and testing the data, the result of the data is displayed to the user. How the error is varying on the loss function is viewed by the user so that the data doesn’t overfit or underfit the model.
template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy:{}'
print(template.format(epoch+1,train_loss.result(),train_accuracy.result()*100,test_loss.result(),test_accuracy.result()*100))
Reset the metrics for the next epoch
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
Tensorflow2.0 provides this facility to run our various models over parallel GPUs. The problem is not to get it to work but to use multiple GPUs efficiently. We can either use it for data parallelism, model parallelism or just training different set on the different GPUs. It is easier to set up and saves a lot of time!
Simple code of training a model without multiple GPU is :
model = tf.keras.applications.ResNet50()
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)
model.compile(..., optimizer=optimizer)
model.fit(train_dataset, epochs=10)
To implement multiple GPU, we need to add only two lines of code like below:
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = tf.keras.applications.ResNet50()
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)
model.compile(..., optimizer=optimizer)
model.fit(train_dataset, epochs=10)
tf.distribute.MirroredStrategy supports distributed training on multiple GPUs at the same time. It creates one replica on each GPU and a variable is made in each GPU which is sync with each other. Each GPU gets the data equal to batch size. Taking advantage of multiple GPUs is very easy with Tensorflow 2.0.
Keras provides a safe format using the HDF5 standard. Tensorflow2.0 provides a model.save() function which is used to save the architecture, weights, and training configuration of a model. We can also load them on the web by Tensorflow.js or on Android by TensorFlow lite.
.h5 extension indicates that the model should be saved to the HDF5 file.
model = create_model()
model.fit(train_images, train_labels, epochs=5)
model.save('xyz.h5')
Most of the time when the Machine Learning developer is training a model, developers want to change things according to their own needs. Tensorflow 2.0 provides features where we can customize as per our needs. Some of the regularly used customization are:
model.compile(optimizer=Adam(), loss=BinaryCrossentropy(),metrics=[AUC(), Precision(), Recall()])
model.fit(data, epochs=10, validation_data=val_data, callbacks=[EarlyStopping(), TensorBoard(), ModelCheckpoint()])
Here, the callbacks are used in model.fit() method. Here, the class Earlystopping() is used so that the model stops training when the improvement in model is stopped. TensorBoard() class is used for providing a visualization of how the model is getting trained. ModelCheckpoint() is used to save the model after every epoch. Here the model will be saved 10 times.
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.dense_1 = layers.Dense(32)
self.dense_2 = layers.Dense(num_classes,activation='softmax')
def call(self, inputs):
x = self.dense_1(inputs)
x = tf.nn.relu(x)
return self.dense_2(x)
Here, we can use our own custom relu function instead of tf.nn.relu based on requirements. This is how we can do customization in Tensorflow 2.0 according to our requirements.
We have received a good amount of knowledge today.
Thank you so much for your time.
Now, let's start using Tensorflow 2.0 for Machine Learning.
Do share this blog with your fellow developers to spread the knowledge.
Happy Machine Learning 🙂
Team AfterAcademy
AfterAcademy Tech
In this blog, we will see how to get started with Competitive Programming. Competitive Programming is very important because in every interview you will be asked to write some code. So, let's learn together.

AfterAcademy Tech
In this tutorial, we are going to learn about Express.js, why it is used, what all features it offers, and how it is used in the development environment. This tutorial will provide you with the most basic and important knowledge that is required in order to use Express in web application servers.

AfterAcademy Tech
In this blog, we will discuss about the steps of problem solving during the technical interview and how to cross the first hurdle for a programmer to get into the software industry and land their dream job.

AfterAcademy Tech
In this tutorial, We are going to learn about the basics of Typescript. If you are having any doubts, whether to use typescript over javascript or Do I really need it? then this article is for you. Learn the concepts behind Typescript and its necessity in today's world.
