Save and Load Your Keras Deep Learning Model
In this tutorial, we will learn about Keras and how to use it i.e. how to create a deep learning model on Keras, how to save it, and how to load it. So, let’s first start with Keras.
Keras is basically a library for simplifying deep learning. It works with TensorFlow in the backend and was developed by Microsoft. It is easier than Tensorflow but is slower than TensorFlow. This is the reason for its use in small models, with large models TensorFlow is preferred.
How to save a model using Keras:
Importing Keras for model creation:
Use the following code for importing Keras:
from keras.models import Sequential from keras.layers import Dense import numpy as np
Notice that it will show “Using Tensorflow backend”, this is because Keras works on the TensorFlow backend of Python.
Here, we will create a model. We will first create a model and put sequential (imported from keras.models) in it. Then we will add hidden layers to our model according to our convenience. Here, we have created three hidden layers. The first hidden layer will have 32 neurons and will take 10 inputs. In the second hidden layer, there are 10 neurons and now the activation function used is “relu”. Similarly in the last layer, there are 5 neurons and the activation function used is “sigmoid”.
In the end, the whole neural network will compile. Here, we will give optimizer i.e. used to give weight to the layers and loss which is given to minimize the cost of the network.
See the Python code below:
model = Sequential() model.add(Dense(32, input_dim=10)) model.add(Dense(10, activation="relu")) model.add(Dense(5, activation="sigmoid")) model.compile(optimizer="adam",loss="mse")
Here, we will save the model using model.save command. In double quotes give the location where you want your model to be saved.
model.save('/content/drive/My Drive/Colab Notebooks/simple.h5')
How to load model using Keras:
Now, to load the previously saved model use the following code. In quotes provide the path where you have saved your model previously.
To use the model we will give the inputs for the first layer in “model.predict” function. Here, we have given 10 zeroes as input.
from keras.models import load_model import numpy as np model=load_model("/content/drive/My Drive/Colab Notebooks/simple.h5") print(model.summary()) test=np.expand_dims(np.zeros(10), axis=0) print(model.predict(test))
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_1 (Dense) (None, 32) 352 _________________________________________________________________ dense_2 (Dense) (None, 10) 330 _________________________________________________________________ dense_3 (Dense) (None, 5) 55 ================================================================= Total params: 737 Trainable params: 737 Non-trainable params: 0 _________________________________________________________________ None [[0.5 0.5 0.5 0.5 0.5]]