tf.keras.callbacks.ModelCheckpoint
Stay organized with collections
Save and categorize content based on your preferences.
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, **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:
- Whether to only keep the model that has achieved the "best performance" so
far, or whether to save the model at the end of every epoch regardless of
performance.
- Definition of 'best'; which quantity to monitor and whether it should be
maximized or minimized.
- The frequency it should save at. Currently, the callback supports saving at
the end of every epoch, or after a fixed number of training batches.
- Whether only weights are saved, or the whole model is saved.
Example:
EPOCHS = 10
checkpoint_filepath = '/tmp/checkpoint'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_filepath,
save_weights_only=True,
monitor='val_acc',
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)
Arguments |
filepath
|
string or PathLike , path to save the model file. filepath
can contain named formatting options, which will be filled the value of
epoch and keys in logs (passed in on_epoch_end ). For example: if
filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5 , then the model
checkpoints will be saved with the epoch number and the validation loss
in the filename.
|
monitor
|
quantity to monitor.
|
verbose
|
verbosity mode, 0 or 1.
|
save_best_only
|
if save_best_only=True , the latest best model according
to the quantity monitored will not be overwritten.
If filepath doesn't contain formatting options like {epoch} then
filepath will 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. For val_acc , this
should be max , for val_loss this should be min , etc. In auto
mode, the direction is automatically inferred from the name of the
monitored quantity.
|
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 the Model is compiled with
experimental_steps_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.CheckpointOptions object if
save_weights_only is true or optional tf.saved_model.SavedOptions
object if save_weights_only is false.
|
**kwargs
|
Additional arguments for backwards compatibility. Possible key
is period .
|
Methods
set_model
View source
set_model(
model
)
set_params
View source
set_params(
params
)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.callbacks.ModelCheckpoint\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/callbacks/ModelCheckpoint) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/keras/callbacks.py#L1057-L1442) |\n\nCallback to save the Keras model or model weights at some frequency.\n\nInherits From: [`Callback`](../../../tf/keras/callbacks/Callback)\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.callbacks.ModelCheckpoint`](/api_docs/python/tf/keras/callbacks/ModelCheckpoint)\n\n\u003cbr /\u003e\n\n tf.keras.callbacks.ModelCheckpoint(\n filepath, monitor='val_loss', verbose=0, save_best_only=False,\n save_weights_only=False, mode='auto', save_freq='epoch', options=None, **kwargs\n )\n\n`ModelCheckpoint` callback is used in conjunction with training using\n`model.fit()` to save a model or weights (in a checkpoint file) at some\ninterval, so the model or weights can be loaded later to continue the training\nfrom the state saved.\n\nA few options this callback provides include:\n\n- Whether to only keep the model that has achieved the \"best performance\" so far, or whether to save the model at the end of every epoch regardless of performance.\n- Definition of 'best'; which quantity to monitor and whether it should be maximized or minimized.\n- The frequency it should save at. Currently, the callback supports saving at the end of every epoch, or after a fixed number of training batches.\n- Whether only weights are saved, or the whole model is saved.\n\n#### Example:\n\n EPOCHS = 10\n checkpoint_filepath = '/tmp/checkpoint'\n model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n filepath=checkpoint_filepath,\n save_weights_only=True,\n monitor='val_acc',\n mode='max',\n save_best_only=True)\n\n # Model weights are saved at the end of every epoch, if it's the best seen\n # so far.\n model.fit(epochs=EPOCHS, callbacks=[model_checkpoint_callback])\n\n # The model weights (that are considered the best) are loaded into the model.\n model.load_weights(checkpoint_filepath)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `filepath` | string or `PathLike`, path to save the model file. `filepath` can contain named formatting options, which will be filled the value of `epoch` and keys in `logs` (passed in `on_epoch_end`). For example: if `filepath` is `weights.{epoch:02d}-{val_loss:.2f}.hdf5`, then the model checkpoints will be saved with the epoch number and the validation loss in the filename. |\n| `monitor` | quantity to monitor. |\n| `verbose` | verbosity mode, 0 or 1. |\n| `save_best_only` | if `save_best_only=True`, the latest best model according to the quantity monitored will not be overwritten. If `filepath` doesn't contain formatting options like `{epoch}` then `filepath` will be overwritten by each new better model. |\n| `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. For `val_acc`, this should be `max`, for `val_loss` this should be `min`, etc. In `auto` mode, the direction is automatically inferred from the name of the monitored quantity. |\n| `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)`). |\n| `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 the `Model` is compiled with `experimental_steps_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'`. |\n| `options` | Optional [`tf.train.CheckpointOptions`](../../../tf/train/CheckpointOptions) object if `save_weights_only` is true or optional `tf.saved_model.SavedOptions` object if `save_weights_only` is false. |\n| `**kwargs` | Additional arguments for backwards compatibility. Possible key is `period`. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `set_model`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/keras/callbacks.py#L1215-L1221) \n\n set_model(\n model\n )\n\n### `set_params`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/keras/callbacks.py#L616-L617) \n\n set_params(\n params\n )"]]