Im Load From Pickle but Train Again

How to Relieve and Load Your Keras Deep Learning Model

Last Updated on August 27, 2020

Keras is a unproblematic and powerful Python library for deep learning.

Given that deep learning models can take hours, days and even weeks to train, it is of import to know how to salvage and load them from deejay.

In this post, you will discover how you can save your Keras models to file and load them up once again to brand predictions.

After reading this tutorial you volition know:

  • How to save model weights and model architecture in dissever files.
  • How to save model architecture in both YAML and JSON format.
  • How to save model weights and architecture into a single file for later use.

Kick-offset your project with my new book Deep Learning With Python, including step-by-step tutorials and the Python source lawmaking files for all examples.

Let's get started.

  • Update Mar 2017: Added instructions to install h5py first.
  • Update Mar/2017: Updated examples for changes to the Keras API.
  • Update Mar/2018: Added alternate link to download the dataset.
  • Update May/2019: Added department on saving and loading the model to a unmarried file.
  • Update Sep/2019: Added note about using PyYAML version 5.

Save and Load Your Keras Deep Learning Models

How to Salve and Load Your Keras Deep Learning Models
Photograph past art_inthecity, some rights reserved.

Tutorial Overview

If y'all are new to Keras or deep learning, run into this step-past-step Keras tutorial.

Keras separates the concerns of saving your model architecture and saving your model weights.

Model weights are saved to HDF5 format. This is a grid format that is ideal for storing multi-dimensional arrays of numbers.

The model structure tin be described and saved using two different formats: JSON and YAML.

In this post nosotros are going to look at ii examples of saving and loading your model to file:

  • Relieve Model to JSON.
  • Save Model to YAML.

Each example volition too demonstrate saving and loading your model weights to HDF5 formatted files.

The examples will use the same simple network trained on the Pima Indians onset of diabetes binary classification dataset. This is a small dataset that contains all numerical information and is easy to work with. You can download this dataset and place it in your working directory with the filename "pima-indians-diabetes.csv" (update: download from here).

Confirm that yous have the latest version of Keras installed (east.g. v2.2.iv as of May 2019).

Notation: Saving models requires that y'all have the h5py library installed. Y'all tin install information technology easily as follows:

Demand help with Deep Learning in Python?

Accept my free 2-calendar week email class and discover MLPs, CNNs and LSTMs (with code).

Click to sign-up now and also get a gratuitous PDF Ebook version of the grade.

Save Your Neural Network Model to JSON

JSON is a unproblematic file format for describing information hierarchically.

Keras provides the ability to describe any model using JSON format with a to_json() part. This can be saved to file and afterwards loaded via the model_from_json() role that will create a new model from the JSON specification.

The weights are saved directly from the model using the save_weights() part and later on loaded using the symmetrical load_weights() part.

The example below trains and evaluates a unproblematic model on the Pima Indians dataset. The model is so converted to JSON format and written to model.json in the local directory. The network weights are written to model.h5 in the local directory.

The model and weight information is loaded from the saved files and a new model is created. Information technology is important to compile the loaded model before information technology is used. This is and so that predictions made using the model tin can use the appropriate efficient computation from the Keras backend.

The model is evaluated in the same way printing the same evaluation score.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Running this example provides the output below.

The JSON format of the model looks like the following:

Salve Your Neural Network Model to YAML

This example is much the same as the to a higher place JSON example, except the YAML format is used for the model specification.

Note, this example assumes that you have PyYAML 5 installed, for instance:

In this example, the model is described using YAML, saved to file model.yaml and later loaded into a new model via the model_from_yaml() function.

Weights are handled in the aforementioned way as above in HDF5 format as model.h5.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the boilerplate outcome.

Running the case displays the following output.

The model described in YAML format looks like the following:

Salve Model Weights and Compages Together

Keras likewise supports a simpler interface to save both the model weights and model architecture together into a unmarried H5 file.

Saving the model in this mode includes everything we demand to know nigh the model, including:

  • Model weights.
  • Model architecture.
  • Model compilation details (loss and metrics).
  • Model optimizer state.

This means that we tin can load and use the model directly, without having to re-compile it as nosotros did in the examples in a higher place.

Annotation: this is the preferred way for saving and loading your Keras model.

How to Save a Keras Model

You tin save your model by calling the salve() function on the model and specifying the filename.

The example beneath demonstrates this by get-go plumbing fixtures a model, evaluating it and saving information technology to the file model.h5.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation process, or differences in numerical precision. Consider running the example a few times and compare the boilerplate outcome.

Running the example fits the model, summarizes the models performance on the training dataset and saves the model to file.

We tin later load this model from file and use it.

How to Load a Keras Model

Your saved model tin then exist loaded later by calling the load_model() function and passing the filename. The function returns the model with the aforementioned compages and weights.

In this case, we load the model, summarize the architecture and evaluate it on the same dataset to ostend the weights and architecture are the same.

Running the case commencement loads the model, prints a summary of the model compages so evaluates the loaded model on the same dataset.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation process, or differences in numerical precision. Consider running the example a few times and compare the average issue.

The model achieves the same accuracy score which in this example is 77%.

Farther Reading

  • How can I salvage a Keras model? in the Keras documentation.
  • About Keras models in the Keras documentation.

Summary

In this post, yous discovered how to serialize your Keras deep learning models.

You learned how you can salvage your trained models to files and after load them upwardly and use them to make predictions.

Y'all also learned that model weights are hands stored using  HDF5 format and that the network structure can be saved in either JSON or YAML format.

Exercise you have whatsoever questions most saving your deep learning models or virtually this mail service?
Ask your questions in the comments and I will do my best to answer them.

Develop Deep Learning Projects with Python!

Deep Learning with Python

 What If Y'all Could Develop A Network in Minutes

...with but a few lines of Python

Discover how in my new Ebook:
Deep Learning With Python

It covers stop-to-terminate projects on topics like:
Multilayer Perceptrons,Convolutional Nets andRecurrent Neural Nets, and more...

Finally Bring Deep Learning To
Your Own Projects

Skip the Academics. Merely Results.

Run into What's Inside

chatmanmusly1995.blogspot.com

Source: https://machinelearningmastery.com/save-load-keras-deep-learning-models/

Related Posts

0 Response to "Im Load From Pickle but Train Again"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel