Deep learning TensorFlow---save and load Keras model

Table of contents

Introduction to Keras

save the whole model

Save the weights of the model


Introduction to Keras

A Keras model consists of several components:

  • A schema, or configuration, that specifies which layers a model contains and how they are connected.
  • A set of weight values ​​(i.e. "the state of the model").
  • Optimizer (defined by compiling the model).
  • A set of losses and metrics (   defined by compiling the model or by calling add_loss() or  ).add_metric()

We can save these pieces to disk all at once, or only selectively save some of them, via the Keras API:

  • Save everything to a single archive in TensorFlow SavedModel format (or the older Keras H5 format). This is standard practice.
  • Only the schema/configuration is saved, usually as a JSON file.
  • Only weight values ​​are saved. Usually used when training a model.

save the whole model

Save the Keras model:

model = ...  

model.save('my_model')

Load the model back:

from tensorflow import keras 

model = keras.models.load_model('my_model')

This will save the most complete model, including:

  • Architecture/configuration of the model
  • The weight values ​​of the model (learned during training)
  • Model compilation information (if called  compile())
  • The optimizer and its state (if any, allowing you to restart training from where it left off)

You can also switch to H5 format by:

  • will be  save_format='h5' passed to  save().
  •  Pass a filename ending  in .h5 or  to  ..kerassave()

Notice! Compared with the SavedModel format, the H5 file does not include the following two aspects:

  • Passed  model.add_loss() and  model.add_metric() added external losses and metrics are not saved (unlike SavedModel). If your model has such losses and metrics and you want to resume training, you need to re-add these losses yourself after loading the model. Note that this does not apply to passes  self.add_loss() and  losses/indicators created withinself.add_metric()  layers . These losses and metrics are preserved as long as the layer is loaded, since they are   part of the layer's method.call
  • Computational graphs for custom objects such as custom layers are not included in the saved file . At load time, Keras needs access to the Python classes/functions of these objects in order to rebuild the model.

Save the weights of the model

Note that you need to make sure you have installed HDF5 and its Python library h5py before using it:

model.save_weights('my_model_weights.h5')


If you need to initialize an exact same model in code, use:

model.load_weights('my_model_weights.h5')


If you need to load weights into a different network structure (some layers are the same), such as fine-tune or transfer-learning, you can load the model by layer name:

model.load_weights('my_model_weights.h5', by_name=True)

Example:


"""
假如原模型为:
    model = Sequential()
    model.add(Dense(2, input_dim=3, name="dense_1"))
    model.add(Dense(3, name="dense_2"))
    ...
    model.save_weights(fname)
"""

# new model 构建的时候要和原来的模型一样 才可以使用保存的权重
model = Sequential()
model.add(Dense(2, input_dim=3, name="dense_1"))  # will be loaded
model.add(Dense(10, name="new_dense"))  # will not be loaded
 

model.load_weights(fname, by_name=True)


 

Always remember, there will be reverberations

Guess you like

Origin blog.csdn.net/qq_52213943/article/details/123842076