Tuesday, June 30, 2026
HomeArtificial IntelligenceLearn how to Save and Load Your Keras Deep Studying Mannequin

Learn how to Save and Load Your Keras Deep Studying Mannequin

[ad_1]

Final Up to date on June 20, 2022

Keras is a straightforward and highly effective Python library for deep studying.

On condition that deep studying fashions can take hours, days and even weeks to coach, you will need to know methods to save and cargo them from disk.

On this submit, you’ll uncover how one can save your Keras fashions to file and cargo them up once more to make predictions.

After studying this tutorial you’ll know:

  • Learn how to save mannequin weights and mannequin structure in separate information.
  • Learn how to save mannequin structure in each YAML and JSON format.
  • Learn how to save mannequin weights and structure right into a single file for later use.

Kick-start your mission with my new e-book Deep Studying With Python, together with step-by-step tutorials and the Python supply code information for all examples.

Let’s get began.

  • Replace Mar 2017: Added directions to put in h5py first.
  • Replace Mar/2017: Up to date examples for adjustments to the Keras API.
  • Replace Mar/2018: Added alternate hyperlink to obtain the dataset.
  • Replace Could/2019: Added part on saving and loading the mannequin to a single file.
  • Replace Sep/2019: Added observe about utilizing PyYAML model 5.
  • Replace Jun/2022: Added observe about deprecated YAML format and added part about protocol buffer.
Learn how to Save and Load Your Keras Deep Studying Mannequin

Learn how to Save and Load Your Keras Deep Studying Fashions
Picture by art_inthecity, some rights reserved.

Tutorial Overview

If you’re new to Keras or deep studying, see this step-by-step Keras tutorial.

Keras separates the issues of saving your mannequin structure and saving your mannequin weights.

Mannequin weights are saved to HDF5 format. It is a grid format that’s splendid for storing multi-dimensional arrays of numbers.

The mannequin construction will be described and saved utilizing two completely different codecs: JSON and YAML.

On this submit we’re going to have a look at three examples of saving and loading your mannequin to file:

  • Save Mannequin to JSON.
  • Save Mannequin to YAML.
  • Save Mannequin to HDF5.

The primary two examples save the mannequin structure and weights individually. The mannequin weights are saved right into a HDF5 format file in all instances.

The examples will use the identical easy community skilled on the Pima Indians onset of diabetes binary classification dataset. It is a small dataset that incorporates all numerical knowledge and is straightforward to work with. You possibly can obtain this dataset and place it in your working listing with the filename “pima-indians-diabetes.csv” (replace: obtain from right here).

Verify that you’ve got TensorFlow v2.x put in (e.g. v2.9 as of June 2022).

Word: Saving fashions requires that you’ve got the h5py library put in. It’s normally put in as a dependency with TensorFlow. You too can set up it simply as follows:


Need assistance with Deep Studying in Python?

Take my free 2-week e mail course and uncover MLPs, CNNs and LSTMs (with code).

Click on to sign-up now and in addition get a free PDF Book model of the course.


Save Your Neural Community Mannequin to JSON

JSON is a straightforward file format for describing knowledge hierarchically.

Keras gives the power to explain any mannequin utilizing JSON format with a to_json() perform. This may be saved to file and later loaded through the model_from_json() perform that can create a brand new mannequin from the JSON specification.

The weights are saved instantly from the mannequin utilizing the save_weights() perform and later loaded utilizing the symmetrical load_weights() perform.

The instance beneath trains and evaluates a easy mannequin on the Pima Indians dataset. The mannequin is then transformed to JSON format and written to mannequin.json within the native listing. The community weights are written to mannequin.h5 within the native listing.

The mannequin and weight knowledge is loaded from the saved information and a brand new mannequin is created. It is very important compile the loaded mannequin earlier than it’s used. That is in order that predictions made utilizing the mannequin can use the suitable environment friendly computation from the Keras backend.

The mannequin is evaluated in the identical means printing the identical analysis rating.

Word: Your outcomes could fluctuate given the stochastic nature of the algorithm or analysis process, or variations in numerical precision. Think about working the instance a number of occasions and evaluate the typical consequence.

Operating this instance gives the output beneath.

The JSON format of the mannequin seems like the next:

Save Your Neural Community Mannequin to YAML

Word: This technique solely applies to TensorFlow 2.5 or earlier. In case you run it in later variations of TensorFlow, you will note a RuntimeError with the message “Technique mannequin.to_yaml() has been eliminated as a consequence of safety danger of arbitrary code execution. Please use mannequin.to_json() as an alternative.”

This instance is far the identical because the above JSON instance, besides the YAML format is used for the mannequin specification.

Word, this instance assumes that you’ve got PyYAML 5 put in, for instance:

On this instance, the mannequin is described utilizing YAML, saved to file mannequin.yaml and later loaded into a brand new mannequin through the model_from_yaml() perform.

Weights are dealt with in the identical means as above in HDF5 format as mannequin.h5.

Word: Your outcomes could fluctuate given the stochastic nature of the algorithm or analysis process, or variations in numerical precision. Think about working the instance a number of occasions and evaluate the typical consequence.

Operating the instance shows the next output.

The mannequin described in YAML format seems like the next:

Save Mannequin Weights and Structure Collectively

Keras additionally helps an easier interface to avoid wasting each the mannequin weights and mannequin structure collectively right into a single H5 file.

Saving the mannequin on this means consists of the whole lot we have to know in regards to the mannequin, together with:

  • Mannequin weights.
  • Mannequin structure.
  • Mannequin compilation particulars (loss and metrics).
  • Mannequin optimizer state.

Which means we will load and use the mannequin instantly, with out having to re-compile it as we did within the examples above.

Word: that is the popular means for saving and loading your Keras mannequin.

Learn how to Save a Keras Mannequin

It can save you your mannequin by calling the save() perform on the mannequin and specifying the filename.

The instance beneath demonstrates this by first becoming a mannequin, evaluating it and saving it to the file mannequin.h5.

Word: Your outcomes could fluctuate given the stochastic nature of the algorithm or analysis process, or variations in numerical precision. Think about working the instance a number of occasions and evaluate the typical consequence.

Operating the instance matches the mannequin, summarizes the fashions efficiency on the coaching dataset and saves the mannequin to file.

We will later load this mannequin from file and use it.

Word that in Keras library, there’s one other perform doing the identical, as follows:

Learn how to Load a Keras Mannequin

Your saved mannequin can then be loaded later by calling the load_model() perform and passing the filename. The perform returns the mannequin with the identical structure and weights.

On this case, we load the mannequin, summarize the structure and consider it on the identical dataset to substantiate the weights and structure are the identical.

Operating the instance first hundreds the mannequin, prints a abstract of the mannequin structure then evaluates the loaded mannequin on the identical dataset.

Word: Your outcomes could fluctuate given the stochastic nature of the algorithm or analysis process, or variations in numerical precision. Think about working the instance a number of occasions and evaluate the typical consequence.

The mannequin achieves the identical accuracy rating which on this case is 77%.

Protocol Buffer Format

Whereas saving and loading a Keras mannequin utilizing HDF5 format is the advisable means, TensorFlow helps one more format, the protocol buffer. It’s thought of sooner to avoid wasting and cargo a protocol buffer format however doing so will produce a number of information. The syntax is similar, besides that we don’t want to offer the .h5 extension to the filename:

These will create a listing “mannequin” with the next information:

That is additionally the format we used to avoid wasting a mannequin in TensorFlow v1.x. You could encounter this while you obtain a pretrained mannequin from TensorFlow Hub.

Additional Studying

Abstract

On this submit, you found methods to serialize your Keras deep studying fashions.

You discovered how one can save your skilled fashions to information and later load them up and use them to make predictions.

You additionally discovered that mannequin weights are simply saved utilizing  HDF5 format and that the community construction will be saved in both JSON or YAML format.

Do you have got any questions on saving your deep studying fashions or about this submit?
Ask your questions within the feedback and I’ll do my greatest to reply them.

Develop Deep Studying Initiatives with Python!

Deep Learning with Python

 What If You May Develop A Community in Minutes

…with just some traces of Python

Uncover how in my new Book:

Deep Studying With Python

It covers end-to-end tasks on subjects like:

Multilayer PerceptronsConvolutional Nets and Recurrent Neural Nets, and extra…

Lastly Carry Deep Studying To

Your Personal Initiatives

Skip the Teachers. Simply Outcomes.

See What’s Inside

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments