网页 综合 更新时间 2026-11-14 10:05:07

Save, serialize, and export models

自动蜘蛛 keras.io 正文约 5141 字
Save, serialize, and export models
Authors: Neel Kovelamudi, Francois Chollet Date created: 2023/06/14 Last modified: 2023/06/30 Description: Complete guide to saving, serializing, and exporting models.
View in Colab • GitHub source
A Keras model consists of multiple components:
The Keras API saves all of these pieces together in a unified format, marked by the .keras extension. This is a zip archive consisting of the following:
Let's take a look at how this works.
If you only have 10 seconds to read this guide, here's what you need to know.
Saving a Keras model:
Loading the model back:
Now, let's look at the details.
This section is about saving an entire model to a single file. The file will include:
You can save a model with model.save() or keras.models.save_model() (which is equivalent). You can load it back with keras.models.load_model().
The only supported format in Keras 3 is the "Keras v3" format, which uses the .keras extension.
This section covers the basic workflows for handling custom layers, functions, and models in Keras saving and reloading.
When saving a model that includes custom objects, such as a subclassed Layer, you must define a get_config() method on the object class. If the arguments passed to the constructor (__init__() method) of the custom object aren't Python objects (anything other than base types like ints, strings, etc.), then you must also explicitly deserialize these arguments in the from_config() class method.
Please see the Defining the config methods section for more details and examples.
The saved .keras file is lightweight and does not store the Python code for custom objects. Therefore, to reload the model, load_model requires access to the definition of any custom objects used through one of the following methods:
Below are examples of each workflow:
This is the preferred method, as custom object registration greatly simplifies saving and loading code. Adding the @keras.saving.register_keras_serializable decorator to the class definition of a custom object registers the object globally in a master list, allowing Keras to recognize the object when loading the model.
Let's create a custom model involving both a custom layer and a custom activation function to demonstrate this.
Any code within the custom object scope will be able to recognize the custom objects passed to the scope argument. Therefore, loading the model within the scope will allow the loading of our custom objects.
This section is about saving only the model's configuration, without its state. The model's configuration (or architecture) specifies what layers the model contains, and how these layers are connected. If you have the configuration of a model, then the model can be created with a freshly initialized state (no weights or compilation information).
The following serialization APIs are available:
You can do in-memory cloning of a model via keras.models.clone_model(). This is equivalent to getting the config then recreating the model from its config (so it does not preserve compilation information or layer weights values).
Calling model.get_config() or layer.get_config() will return a Python dict containing the configuration of the model or layer, respectively. You should define get_config() to contain arguments needed for the __init__() method of the model or layer. At loading time, the from_config(config) method will then call __init__() with these arguments to reconstruct the model or layer.
Now let's reconstruct the layer using the from_config() method:
Sequential model example:
Functional model example:
This is similar to get_config / from_config, except it turns the model into a JSON string, which can then be loaded without the original model class. It is also specific to models, it isn't meant for layers.
The keras.saving.serialize_keras_object() and keras.saving.deserialize_keras_object() APIs are general-purpose APIs that can be used to serialize or deserialize any Keras object and any custom object. It is at the foundation of saving model architecture and is behind all serialize()/deserialize() calls in keras.
Note the serialization format containing all the necessary information for proper reconstruction:
Now we can reconstruct the regularizer.
You can choose to only save & load a model's weights. This can be useful if:
Weights can be copied between different objects by using get_weights() and set_weights():
Transferring weights from one layer to another, in memory
Transferring weights from one model to another model with a compatible architecture, in memory
The case of stateless layers
Because stateless layers do not change the order or number of weights, models can have compatible architectures even if there are extra/missing stateless layers.
Weights can be saved to disk by calling model.save_weights(filepath). The filename should end in .weights.h5.
Note that changing layer.trainable may result in a different layer.weights ordering when the model contains nested layers.
When loading pretrained weights from a weights file, it is recommended to load the weights into the original checkpointed model, and then extract the desired weights/layers into a new model.
访问原文 返回搜索 https://keras.io/guides/serialization_and_saving/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。