Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

keras load
keras save
load model
save model

Keras load/save model

Educative Answers Team

Keras is a simple and powerful Python library for deep learning and machine learning algorithms.

A Keras model consists of multiple components:

  1. The architecture of the model, which specifies its layers and how they are connected with each other. It also includes the type of model, e.g., sequential, etc.
  2. A set of weights values (the “state of the model”).
  3. An optimizer (defined by compiling the model).
  4. A set of losses and metrics.

Many deep learning models are complex and may take days to train and test the data. Therefore, it is important to understand how to save and load data for later use.

Saving and loading the model

There are three ways in which the model can be saved:

  1. Saving everything into a single file, usually it is in the Keras H5 formatAn H5 file is a data file saved in the Hierarchical Data Format (HDF). It contains multidimensional arrays of scientific data..

  2. Saving the architecture as only a JSON or YAML file.

  3. Saving the weight values only. This is generally used when training the model so that they may be used again. These weights can be used in a similar representation of a different model.

svg viewer

Method 1

Saving the model

In order to save the model, a user can use the save() function specified in the Keras library. A user will specify the name and location of the file with .h5 extension. If you do not specify the path it will, by default, save it in the folder where the model is executed. Look at the syntax below:

model.save("myModel.h5")

The function save() saves the following features:

  1. The architecture of the model.
  2. Weights of the model.
  3. Training configuration.
  4. State of the optimizer.

Loading the model

In order to load the model, a user can use the load_model() function specified in the keras.models library. The user will first have to import this function from the relevant library in order to use it. The user will specify the name and location of the file with an .h5 extension. Take a look at the syntax below:

from keras.models import load_model
model_new = load_model("myModel.h5")

Method 2

Saving only the architecture of the model

The architecture is saved as a JSON file or a YAML file. Look at the syntax below:

As a JSON:

from keras.models import model_from_json

save_model_json = model.to_json()
with open("myModel.json", "w") as json_file:
    json_file.write(save_model_json )

As a YAML:

from keras.models import model_from_yaml
model_yaml = model.to_yaml()
with open("myModel.yaml", "w") as yaml_file:
    yaml_file.write(model_yaml)

Loading the saved architecture of the model

As a JSON:

json_file = open('myModel.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
new_model = model_from_json(loaded_model_json)

As a YAML:

yaml_file = open('myModel.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
new_model = model_from_yaml(loaded_model_yaml)

Method 3

Saving only the weights of the model

model.save_weights("myModel.h5")

Loading the saved weights of the model

new_model.load_weights("myModel.h5")

RELATED TAGS

keras load
keras save
load model
save model
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring