Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas! Do you know about models? Have you ever heard something about Keras? If not, then don't worry. We will help you to clear all your doubts.
In this article, we will discuss saving and loading models with Keras. We will discuss how we can implement it. Models in Keras are used to organize the various layers of the neural network. Keras provides two types of models. The first one is the sequential model which is a stack of layers in a linear manner. The second is the Model class which allows complex models.
Now, let’s discuss what Keras is.
What is Keras
Keras is a deep learning API which is developed by Google to implement neural networks. We can use Keras to implement deep learning in Python as well as in R.
Keras can be run on CPU (Central Processing Unit), GPU (Graphics Processing Unit), and TPU (Tensor Processing Units). Keras is straightforward to understand and incorporate into the deep learning models. Keras supports multi-backend, and we can use it along with TensorFlow, Theano, and CNTK (Microsoft Cognitive Toolkit). Keras has a large active community.
Let us understand the importance of saving and loading models with Keras.
Importance of Saving and Loading Models
We can save a deep learning model during the runtime or after the training of deep learning model. In machine learning and deep learning, training is the process of determining the outputs which the model learns from the given data. We save the deep learning models so that we can use them in the future without training them one more time. Saving a model in Keras ensures that we can continue to recreate the deep learning model at a later stage.
Now let us understand what the methods are to save a model in Keras.
Various Ways of Saving Model using Keras
Keras provides three ways to save our deep learning model. The methods are as follows:
Save the Whole Model and Load it When Needed
We can save the deep learning model as a single artefact and use it whenever needed in the future. We can save the architecture of the model, information during the runtime of the model, the weights along with the biases (if any) which are learned during the execution of the model, the state, and the optimizers which help us to resume the process of training where we left off.
Saving the Architecture of the Model
The architecture of the deep learning model states the layers of the model and the interconnection of these layers among themselves. This helps us to create a new model with totally new weights (it is a parameter in neural networks which helps to transform input data in the layers of the network) and no information about the compilation process.
Saving the Weights of the Model and Loading it When Needed
We can save the model's weights and load it. This is useful when we do not need the information on the compilation and the state of optimizers. This is also useful in the case of transfer learning.
We need to install h5py library to save our models successfully. We can install it by writing the following command:
sudo pip install h5py
You can also try this code with Online Python Compiler
Pickle is used for serializing(process of converting the state of an object into byte stream also known as pickling) and deserializing(process of reconstructing the object from the byte stream also known as depickling) Python object structures. Pickle is a tool in Python that allows us to save our machine learning and deep learning models. This helps us to minimize the re-training of the model and allows us to share the created model and reload it even on different machines.
Implementation
import tensorflow as tf
import numpy as np
# Importing the pickle library
import pickle
from tensorflow.keras.datasets import boston_housing
from sklearn import preprocessing
(train_x,train_y),(test_x,test_y)=boston_housing.load_data()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
def HousePricePrediction():
model=Sequential()
model.add(Dense(128,activation='relu',input_shape=(train_x[0].shape)))
model.add(Dense(64,activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae'])
return model
k=4
num_val_samples=len(train_x)
num_epochs=100
all_scores=[]
model=HousePricePrediction()
history=model.fit(x=train_x,y=train_y,epochs=num_epochs,batch_size=1,verbose=1,validation_data=(test_x,test_y))
# Saving the model, passing the model and giving name of model
pickle.dump(model,open('model','wb'))
# Loading the saved model with the saved name
model=pickle.load(open('model','rb'))
model.summary()
You can also try this code with Online Python Compiler
This is the structure of the model created. You can see the number of layers present in the model. You can also see the total number of parameters, trainable and non-trainable parameters.
We will get the model file after implementing the above-mentioned code.
Model File
Saving and Loading Model to JSON File
JSON stands for JavaScript Object Notation. It is commonly used for transferring data in web applications. It allows various machines to communicate with each other by sending messages in a defined way. We can describe the deep learning model using the to_json() function. We can load the saved model by using the model_from_json() function. The weights can be stored using the save_weights() function. We can use the weights using the load_weights() function.
Implementation
import tensorflow as tf
from tensorflow.keras.datasets import boston_housing
from sklearn import preprocessing
(train_x,train_y),(test_x,test_y)=boston_housing.load_data()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
def HousePricePrediction():
model=Sequential()
model.add(Dense(128,activation='relu',input_shape=(train_x[0].shape)))
model.add(Dense(64,activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae'])
return model
import numpy as np
k=4
num_val_samples=len(train_x)
num_epochs=100
all_scores=[]
model=HousePricePrediction()
history=model.fit(x=train_x,y=train_y,epochs=num_epochs,batch_size=1,verbose=1,validation_data=(test_x,test_y))
# Saving model in JSON file
from tensorflow.keras.models import model_from_json
model_in_json=model.to_json()
with open('model.json','w') as json_file:
json_file.write(model_in_json)
# Loading model from JSON file
model_file=open('model.json','r')
json_model=model_file.read()
model2=model_from_json(json_model)
model2.summary()
You can also try this code with Online Python Compiler
We will get model.json file after implementing the above mentioned code.
JSON File
Saving and Loading Model to YAML File
YAML is a data-serialization method. This method can be used only in TensorFlow version 2.5 or earlier. We have used TensorFlow 2.5.0 to save the model. We will get a RuntimeError with a message Method model.to_yaml() is removed due to security reasons. We can use model.to_json() instead.
Implementation
!pip install tensorflow==2.5.0
You can also try this code with Online Python Compiler
import tensorflow as tf
from tensorflow.keras.datasets import boston_housing
from sklearn import preprocessing
(train_x,train_y),(test_x,test_y)=boston_housing.load_data()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
def HousePricePrediction():
model=Sequential()
model.add(Dense(128,activation='relu',input_shape=(train_x[0].shape)))
model.add(Dense(64,activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae'])
return model
import numpy as np
num_val_samples=len(train_x)
num_epochs=100
all_scores=[]
model=HousePricePrediction()
history=model.fit(x=train_x,y=train_y,epochs=num_epochs,batch_size=1,verbose=1,validation_data=(test_x,test_y))
# Saving model in YAML file
from tensorflow.keras.models import model_from_yaml
model_in_yaml=model.to_json()
with open('model.yaml','w') as yaml_file:
yaml_file.write(model_in_yaml)
# Loading model from YAML file
model_file=open('model.yaml','r')
yaml_model=model_file.read()
model3=model.to_json(yaml_model)
model3.summary()
You can also try this code with Online Python Compiler
We will get model.yaml file after implementing the above mentioned code.
YAML File
Saving and Loading Model to HDF5 File
HDF5 is a library for storing scientific data. It stores groups or datasets. The HDF5 contains weights grouped by the names of their layer. We can list the weights by adding the training weights to the list of non-trainable weights by using layer weights.
Implementation
import tensorflow as tf
from tensorflow.keras.datasets import boston_housing
from sklearn import preprocessing
(train_x,train_y),(test_x,test_y)=boston_housing.load_data()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
def HousePricePrediction():
model=Sequential()
model.add(Dense(128,activation='relu',input_shape=(train_x[0].shape)))
model.add(Dense(64,activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae'])
return model
import numpy as np
k=4
num_val_samples=len(train_x)
num_epochs=100
all_scores=[]
model=HousePricePrediction()
history=model.fit(x=train_x,y=train_y,epochs=num_epochs,batch_size=1,verbose=1,validation_data=(test_x,test_y))
# Saving model and weights in .h5 file format
model.save('model.h5')
# Loading model and weights from .h5 file format
from tensorflow.keras.models import load_model
model4=load_model('model.h5')
model4.summary()
You can also try this code with Online Python Compiler
We will get model.h5 file after implementing the above mentioned code.
HDF5 File
Alright! Now we hope you understand the Saving and Loading Model with Keras.
Frequently Asked Questions
What is Keras?
Keras is an open-source library which gives an interface to work with artificial neural networks (better known as ANN) in Python. Keras is compatible with Ubuntu and Windows.
What is the difference between Keras and TensorFlow?
TensorFlow is an open-source end-to-end platform, and developers use this library for machine learning and deep learning tasks. In contrast, Keras is a neural network library which is included in TensorFlow.
What are the types of layers in Keras?
There are various types of layers in Keras, such as a convolutional layer, pooling layer, recurrent layer, embedding layer, normalization layer, and noise layer.
What is flattening in Keras?
Flattening is the process of converting the 2-dimensional layers from pooled feature maps into a single continuous linear vector.
What are the main components of Keras API?
Keras API is mainly divided into three categories. The components are model, layer, and core modules.
Conclusion
In this article, we discussed the Saving and Loading Model with Keras. We also learnt about the various ways of saving and loading the model in Keras. We learnt how we can save our model in JSON, YAML, and HDF5 format. We hope this blog on the Saving and Loading Model with Keras was helpful. You can also refer to other similar articles.