Create Save and load Model with Graph – Part 5 | Tensorflow MNIST


This tutorial is about creating, saving and loading the model with graph on mnist dataset (Tensorflow MNIST). You will see the tensorflow neural network on the graph. Before reading the part 5, I wish you have gone through the previous blog. If not, please spend some time in reading this.

Tensorflow control practice with live codes, graphs and sessions-Part 4 | AI Sangam

Low Level introduction of Tensorflow in a simple way – Part 3 | AI Sangam

Optimize Parameters TensorflowTutorial-Part2 | AI Sangam

Tensorflow tutorials from basic for beginners-Part 1 | AI Sangam

In this tutorial you will learn:

What you will gain after reading from this blog

  • Graph and session for prediction phase
  • Drawing the live graph for the prediction phase

1. Training tensorflow keras deep neural network for mnist dataset

This blog is about practical coding part so we will move directly to the code part. Special thanks to tensorflow tutorial which has helped us in creating such wonderful tutorial. First of all mnist dataset is loaded which is part of tf.keras.datasets.mnist. Please see the below code to create the module ‘tensorflow._api.v1.keras.datasets.mnist.

Output of the code
module ‘tensorflow._api.v1.keras.datasets.mnist’ from ‘C:\\Python3.5\\lib\\site-packages\\tensorflow\\_api\\v1\\keras\\datasets\\mnist\\’;

It is important to see methods available with mnist module which is created above, please run the below code

Output of the code
[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__path__’, ‘__spec__’, ‘load_data’]

As you can see in the above code, we have method load_data available with the mnist module so data is loaded using the above method as coded below

This code will load x_train, y_train, x_test and y_test. Now let us see the shape of the training and testing data as well as target using the below command

Output of the code
Please show the shape of x_train: (60000, 28, 28)
Please show the shape of x_test: (10000, 28, 28)
Please show the shape of y_train: (60000,)
Please show the shape of y_test: (10000,)

Now, let us normalize the data by dividing the x_train and x_test by 255.0. Please see the below code to get it done

Let us create the model using the below code

Keras is used with tensorflow at the backend as working with tensorflow is difficult. Keras is used at the frontend which makes the task easy. Each row/sample of the training data has shape 28,28 so input shape =(28,28) is passed to the Flatten layer. Output of this layer is passed to the Dense layer with 512 neurons and activation function RELU. Deep neural network is being constructed using keras tensorflow deep learning library. To reduce the number of neurons 20 percent of them are dropped out. At the last part we pass the vectors/tensors to the last dense layer which is the softmax layer which has 10 neurons as there are 10 classes for m nist dataset (0 to 9). In this way tensorflow keras Sequential model is constructed.

Next step is to compile the model which has been created as above. To compile the model, we need  important parameters such as optimizer, loss function and metrics. Please have a look at Dlology blog to get the knowledge about which optimizer and loss function suites which type of task. Please look at the below code to get this step done.

Remember if you look at the mnist dataset, aim is to classify the digits from 0 to 9, hence this is treated as a category. This is the reason why loss function is chosen as sparse_categorical_crossentropy. It is a classification model and hence metrics is chosen as accuracy.

Training the model: Once you have successful in the above steps, it is vital to train the model. We have x_train and y_train from the mnist dataset. Model is trained using x_train and y_train with number of epochs. Changing the number of epochs will change the accuracy of the model. So it is important to train the model at such epoch where maximum accuracy is achieved. Please see the below code to get my point. 

Please see the screenshot to get with me at this step.

Training the model at epochs

Training the model at epochs

Let us save the model. Model saving will save our time because we will load the model once the model is saved and it will save our time of training the model every time to check the prediction phase. Model is saved in form of HDF5 format. If h5py is not installed in your system, please install it using the below command.

To save the model, please follow the below command

You can also save_weights of the model as well as serialize model to JSON format using the below code.

Next step is to load the model so that we can go the prediction phase. To load the model, we need to import submodule from keras using the below code.

Output of the model
keras.models.Sequential object at 0x000000000AA2B208 

2. Graph and session for prediction phase

Since the model is loaded it is important to proceed to the prediction phase with graph creation using tensorflow. Please have a look at the below code how prediction phase works

Some points to remember
1.) We have taken placeholder as different values of x will be updated each with the shape of 28,28.
2.) Both the values (graph and model.predict) are printed. Along with this graph is generated. Please have a look at the below output as well as graph for this session.
3.) model.predict is used to provide the output for the testing data.
4.) Some times model is not loaded and problem of unknown initializer: GlorotUniform when loading Keras model comes so please implement the below code to rectify the problem

Let us see the graph being plotted using the tensorflow using the above code as below

Graph showing prediction phase

Graph showing prediction phase

When you run the code, you will see code is using tensorflow at the backend, please visit the link C:\Users\datascience\.keras if you are using windows or /home/datascience/.keras/keras.json if you are using ubuntu. When you open such file, you will find the following content.

Please change the back-end to the tensorflow or theano. It depends on you.

What you have learned after this tutorial

In a nutshell this tutorial is about Tensorflow MNIST i.e building tensorflow neural network for mnist dataset . Proper code with both explanation as well as live graphs are shown in this blog. From my consideration, you have gained knowledge how to save the keras model as well as how to load the model. We also came across plotting the prediction phase on the graph in the tensorflow. Live sessions and practice will lead in increase interest in understanding deep learning libraries such as tensorflow. I also urge you to go through the first four tutorial on tensortflow series so that proper coordination as well as synchronization can be build. Don’t miss our video on real time face recognition.


Also Read:

Tensorflow control practice with live codes, graphs and sessions- Part 4 | AI Sangam

Leave a Reply

Your email address will not be published.