| View source on GitHub | 
Callback to save the Keras model or model weights at some frequency.
Inherits From: Callback
tf.keras.callbacks.ModelCheckpoint(
    filepath,
    monitor='val_loss',
    verbose=0,
    save_best_only=False,
    save_weights_only=False,
    mode='auto',
    save_freq='epoch',
    options=None,
    initial_value_threshold=None,
    **kwargs
)
  ModelCheckpoint callback is used in conjunction with training using model.fit() to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved.
A few options this callback provides include:
Note: If you getWARNING:tensorflow:Can save best model only with <name> available, skippingsee the description of themonitorargument for details on how to get this right.
model.compile(loss=..., optimizer=...,
              metrics=['accuracy'])
EPOCHS = 10
checkpoint_filepath = '/tmp/checkpoint'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True,
    monitor='val_accuracy',
    mode='max',
    save_best_only=True)
# Model weights are saved at the end of every epoch, if it's the best seen
# so far.
model.fit(epochs=EPOCHS, callbacks=[model_checkpoint_callback])
# The model weights (that are considered the best) are loaded into the model.
model.load_weights(checkpoint_filepath)
  
| Args | |
|---|---|
| filepath | string or PathLike, path to save the model file. e.g. filepath = os.path.join(working_dir, 'ckpt', file_name).filepathcan contain named formatting options, which will be filled the value ofepochand keys inlogs(passed inon_epoch_end). For example: iffilepathisweights.{epoch:02d}-{val_loss:.2f}.hdf5, then the model checkpoints will be saved with the epoch number and the validation loss in the filename. The directory of the filepath should not be reused by any other callbacks to avoid conflicts. | 
| monitor | The metric name to monitor. Typically the metrics are set by the Model.compilemethod. Note:
 | 
| verbose | Verbosity mode, 0 or 1. Mode 0 is silent, and mode 1 displays messages when the callback takes an action. | 
| save_best_only | if save_best_only=True, it only saves when the model is considered the "best" and the latest best model according to the quantity monitored will not be overwritten. Iffilepathdoesn't contain formatting options like{epoch}thenfilepathwill be overwritten by each new better model. | 
| mode | one of {'auto', 'min', 'max'}. If save_best_only=True, the decision to overwrite the current save file is made based on either the maximization or the minimization of the monitored quantity. Forval_acc, this should bemax, forval_lossthis should bemin, etc. Inautomode, the mode is set tomaxif the quantities monitored are 'acc' or start with 'fmeasure' and are set tominfor the rest of the quantities. | 
| save_weights_only | if True, then only the model's weights will be saved ( model.save_weights(filepath)), else the full model is saved (model.save(filepath)). | 
| save_freq | 'epoch'or integer. When using'epoch', the callback saves the model after each epoch. When using integer, the callback saves the model at end of this many batches. If theModelis compiled withsteps_per_execution=N, then the saving criteria will be checked every Nth batch. Note that if the saving isn't aligned to epochs, the monitored metric may potentially be less reliable (it could reflect as little as 1 batch, since the metrics get reset every epoch). Defaults to'epoch'. | 
| options | Optional tf.train.CheckpointOptionsobject ifsave_weights_onlyis true or optionaltf.saved_model.SaveOptionsobject ifsave_weights_onlyis false. | 
| initial_value_threshold | Floating point initial "best" value of the metric to be monitored. Only applies if save_best_value=True. Only overwrites the model weights already saved if the performance of current model is better than this value. | 
| **kwargs | Additional arguments for backwards compatibility. Possible key is period. | 
set_model
set_model(
    model
)
 set_params
set_params(
    params
)
  
    © 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
    https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/keras/callbacks/ModelCheckpoint