5.0 / 5
We will cover the following tasks in 44 minutes:
We will understand the Rhyme interface and our learning environment. You will get a virtual machine, you will need Jupyter Notebook and TensorFlow for this course and both of these are already installed on your virtual machine. Jupyter Notebooks are very popular with Data Science and Machine Learning Engineers as one can write code in cells and use other cells for documentation.
Importing TensorFlow and the Dataset
In this course, we are going to use TensorFlow’s implementation of Keras. Keras is a high level API to build and train models. We are going to use the MNIST dataset in this course. This dataset has images of handwritten digits. Since we are only interested in understanding saving and reloading models in this course, we are going to use a very small subset of the dataset so that the training process is fast. So, we are going to use 1000 training examples and 1000 test examples.
Building a Model
Instead of writing code to just build a single model, let’s create a method that returns a model. This way, we can reuse the code to make as many instances of the model as we want. Our model has three layers. We compile the model with
sparse categorical crossentropy loss and
adam optimizer. In this example, we will use accuracy as our only training metric.
We can save checkpoints during and after training models. This way, you can pick up training where you left off - in case the training was interrupted or you can use a trained model without having to retrain it.
We will use a callback from Keras called
ModelCheckpoint to perform this task. We will need to configure this callback and then use it while training our model. Notice that we store only the trained weights in the checkpoint files. We have not stored the network architecture. So, if we want to restore any of the saved checkpoints, we will have to do that on a model with the same architecture that we used while training and saving the checkpoints.
Manually Saving Weights
We will create a new model and train it. Then, we will manually save the weights of this trained model. We will create a new instance of the untrained model. Finally, we will load the manually saved weights to this new, untrained model.
Saving Entire Models
We can save entire models to a file with the weight values, the model’s configuration, and the optimizer’s configuration. This allows us to checkpoint a model and resume training later - from the exact same state - even without having access to the original code. Keras provides a basic save format using the HDF5 standard.
About the Host (Amit Yadav)
I am a Software Engineer with many years of experience in writing commercial software. My current areas of interest include computer vision and sequence modelling for automated signal processing using deep learning as well as developing chatbots.