Module keras.api.keras.callbacks
Public API for tf.keras.callbacks namespace.
Expand source code
# This file is MACHINE GENERATED! Do not edit.
# Generated by: tensorflow/python/tools/api/generator/create_python_api.py script.
"""Public API for tf.keras.callbacks namespace.
"""
from __future__ import print_function as _print_function
import sys as _sys
from keras.callbacks import BaseLogger
from keras.callbacks import CSVLogger
from keras.callbacks import Callback
from keras.callbacks import CallbackList
from keras.callbacks import EarlyStopping
from keras.callbacks import History
from keras.callbacks import LambdaCallback
from keras.callbacks import LearningRateScheduler
from keras.callbacks import ModelCheckpoint
from keras.callbacks import ProgbarLogger
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import RemoteMonitor
from keras.callbacks import TerminateOnNaN
from keras.callbacks_v1 import TensorBoard
del _print_function
from tensorflow.python.util import module_wrapper as _module_wrapper
if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):
_sys.modules[__name__] = _module_wrapper.TFModuleWrapper(
_sys.modules[__name__], "keras.callbacks", public_apis=None, deprecation=True,
has_lite=False)
Sub-modules
keras.api.keras.callbacks.experimental
Classes
class BaseLogger (stateful_metrics=None)
-
Callback that accumulates epoch averages of metrics.
This callback is automatically applied to every Keras model.
Args
stateful_metrics
- Iterable of string names of metrics that
should not be averaged over an epoch.
Metrics in this list will be logged as-is in
on_epoch_end
. All others will be averaged inon_epoch_end
.
Expand source code
class BaseLogger(Callback): """Callback that accumulates epoch averages of metrics. This callback is automatically applied to every Keras model. Args: stateful_metrics: Iterable of string names of metrics that should *not* be averaged over an epoch. Metrics in this list will be logged as-is in `on_epoch_end`. All others will be averaged in `on_epoch_end`. """ def __init__(self, stateful_metrics=None): super(BaseLogger, self).__init__() self.stateful_metrics = set(stateful_metrics or []) def on_epoch_begin(self, epoch, logs=None): self.seen = 0 self.totals = {} def on_batch_end(self, batch, logs=None): logs = logs or {} batch_size = logs.get('size', 0) # In case of distribution strategy we can potentially run multiple steps # at the same time, we should account for that in the `seen` calculation. num_steps = logs.get('num_steps', 1) self.seen += batch_size * num_steps for k, v in logs.items(): if k in self.stateful_metrics: self.totals[k] = v else: if k in self.totals: self.totals[k] += v * batch_size else: self.totals[k] = v * batch_size def on_epoch_end(self, epoch, logs=None): if logs is not None: for k in self.params['metrics']: if k in self.totals: # Make value available to next callbacks. if k in self.stateful_metrics: logs[k] = self.totals[k] else: logs[k] = self.totals[k] / self.seen
Ancestors
Inherited members
class CSVLogger (filename, separator=',', append=False)
-
Callback that streams epoch results to a CSV file.
Supports all values that can be represented as a string, including 1D iterables such as
np.ndarray
.Example:
csv_logger = CSVLogger('training.log') model.fit(X_train, Y_train, callbacks=[csv_logger])
Args
filename
- Filename of the CSV file, e.g.
'run/log.csv'
. separator
- String used to separate elements in the CSV file.
append
- Boolean. True: append if file exists (useful for continuing training). False: overwrite existing file.
Expand source code
class CSVLogger(Callback): """Callback that streams epoch results to a CSV file. Supports all values that can be represented as a string, including 1D iterables such as `np.ndarray`. Example: ```python csv_logger = CSVLogger('training.log') model.fit(X_train, Y_train, callbacks=[csv_logger]) ``` Args: filename: Filename of the CSV file, e.g. `'run/log.csv'`. separator: String used to separate elements in the CSV file. append: Boolean. True: append if file exists (useful for continuing training). False: overwrite existing file. """ def __init__(self, filename, separator=',', append=False): self.sep = separator self.filename = path_to_string(filename) self.append = append self.writer = None self.keys = None self.append_header = True super(CSVLogger, self).__init__() def on_train_begin(self, logs=None): if self.append: if tf.io.gfile.exists(self.filename): with tf.io.gfile.GFile(self.filename, 'r') as f: self.append_header = not bool(len(f.readline())) mode = 'a' else: mode = 'w' self.csv_file = tf.io.gfile.GFile(self.filename, mode) def on_epoch_end(self, epoch, logs=None): logs = logs or {} def handle_value(k): is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0 if isinstance(k, str): return k elif isinstance(k, collections.abc.Iterable) and not is_zero_dim_ndarray: return '"[%s]"' % (', '.join(map(str, k))) else: return k if self.keys is None: self.keys = sorted(logs.keys()) if self.model.stop_training: # We set NA so that csv parsers do not fail for this last epoch. logs = dict((k, logs[k]) if k in logs else (k, 'NA') for k in self.keys) if not self.writer: class CustomDialect(csv.excel): delimiter = self.sep fieldnames = ['epoch'] + self.keys self.writer = csv.DictWriter( self.csv_file, fieldnames=fieldnames, dialect=CustomDialect) if self.append_header: self.writer.writeheader() row_dict = collections.OrderedDict({'epoch': epoch}) row_dict.update((key, handle_value(logs[key])) for key in self.keys) self.writer.writerow(row_dict) self.csv_file.flush() def on_train_end(self, logs=None): self.csv_file.close() self.writer = None
Ancestors
Inherited members
class Callback
-
Abstract base class used to build new callbacks.
Callbacks can be passed to keras methods such as
fit
,evaluate
, andpredict
in order to hook into the various stages of the model training and inference lifecycle.To create a custom callback, subclass
Callback
and override the method associated with the stage of interest. See https://www.tensorflow.org/guide/keras/custom_callback for more information.Example:
>>> training_finished = False >>> class MyCallback(tf.keras.callbacks.Callback): ... def on_train_end(self, logs=None): ... global training_finished ... training_finished = True >>> model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))]) >>> model.compile(loss='mean_squared_error') >>> model.fit(tf.constant([[1.0]]), tf.constant([[1.0]]), ... callbacks=[MyCallback()]) >>> assert training_finished == True
If you want to use
Callback
objects in a custom training loop:- You should pack all your callbacks into a single
callbacks.CallbackList
so they can all be called together. - You will need to manually call all the
on_*
methods at the apropriate locations in your loop. Like this:
``` callbacks = tf.keras.callbacks.CallbackList([…]) callbacks.append(…)
callbacks.on_train_begin(…) for epoch in range(EPOCHS): callbacks.on_epoch_begin(epoch) for i, data in dataset.enumerate(): callbacks.on_train_batch_begin(i) batch_logs = model.train_step(data) callbacks.on_train_batch_end(i, batch_logs) epoch_logs = … callbacks.on_epoch_end(epoch, epoch_logs) final_logs=… callbacks.on_train_end(final_logs) ```
Attributes
params
- Dict. Training parameters (eg. verbosity, batch size, number of epochs…).
model
- Instance of
keras.models.Model
. Reference of the model being trained.
The
logs
dictionary that callback methods take as argument will contain keys for quantities relevant to the current batch or epoch (see method-specific docstrings).Expand source code
class Callback: """Abstract base class used to build new callbacks. Callbacks can be passed to keras methods such as `fit`, `evaluate`, and `predict` in order to hook into the various stages of the model training and inference lifecycle. To create a custom callback, subclass `keras.callbacks.Callback` and override the method associated with the stage of interest. See https://www.tensorflow.org/guide/keras/custom_callback for more information. Example: >>> training_finished = False >>> class MyCallback(tf.keras.callbacks.Callback): ... def on_train_end(self, logs=None): ... global training_finished ... training_finished = True >>> model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))]) >>> model.compile(loss='mean_squared_error') >>> model.fit(tf.constant([[1.0]]), tf.constant([[1.0]]), ... callbacks=[MyCallback()]) >>> assert training_finished == True If you want to use `Callback` objects in a custom training loop: 1. You should pack all your callbacks into a single `callbacks.CallbackList` so they can all be called together. 2. You will need to manually call all the `on_*` methods at the apropriate locations in your loop. Like this: ``` callbacks = tf.keras.callbacks.CallbackList([...]) callbacks.append(...) callbacks.on_train_begin(...) for epoch in range(EPOCHS): callbacks.on_epoch_begin(epoch) for i, data in dataset.enumerate(): callbacks.on_train_batch_begin(i) batch_logs = model.train_step(data) callbacks.on_train_batch_end(i, batch_logs) epoch_logs = ... callbacks.on_epoch_end(epoch, epoch_logs) final_logs=... callbacks.on_train_end(final_logs) ``` Attributes: params: Dict. Training parameters (eg. verbosity, batch size, number of epochs...). model: Instance of `keras.models.Model`. Reference of the model being trained. The `logs` dictionary that callback methods take as argument will contain keys for quantities relevant to the current batch or epoch (see method-specific docstrings). """ def __init__(self): self.validation_data = None # pylint: disable=g-missing-from-attributes self.model = None # Whether this Callback should only run on the chief worker in a # Multi-Worker setting. # TODO(omalleyt): Make this attr public once solution is stable. self._chief_worker_only = None self._supports_tf_logs = False def set_params(self, params): self.params = params def set_model(self, model): self.model = model @doc_controls.for_subclass_implementers @generic_utils.default def on_batch_begin(self, batch, logs=None): """A backwards compatibility alias for `on_train_batch_begin`.""" @doc_controls.for_subclass_implementers @generic_utils.default def on_batch_end(self, batch, logs=None): """A backwards compatibility alias for `on_train_batch_end`.""" @doc_controls.for_subclass_implementers def on_epoch_begin(self, epoch, logs=None): """Called at the start of an epoch. Subclasses should override for any actions to run. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers def on_epoch_end(self, epoch, logs=None): """Called at the end of an epoch. Subclasses should override for any actions to run. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict, metric results for this training epoch, and for the validation epoch if validation is performed. Validation result keys are prefixed with `val_`. For training epoch, the values of the `Model`'s metrics are returned. Example : `{'loss': 0.2, 'accuracy': 0.7}`. """ @doc_controls.for_subclass_implementers @generic_utils.default def on_train_batch_begin(self, batch, logs=None): """Called at the beginning of a training batch in `fit` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ # For backwards compatibility. self.on_batch_begin(batch, logs=logs) @doc_controls.for_subclass_implementers @generic_utils.default def on_train_batch_end(self, batch, logs=None): """Called at the end of a training batch in `fit` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ # For backwards compatibility. self.on_batch_end(batch, logs=logs) @doc_controls.for_subclass_implementers @generic_utils.default def on_test_batch_begin(self, batch, logs=None): """Called at the beginning of a batch in `evaluate` methods. Also called at the beginning of a validation batch in the `fit` methods, if validation data is provided. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers @generic_utils.default def on_test_batch_end(self, batch, logs=None): """Called at the end of a batch in `evaluate` methods. Also called at the end of a validation batch in the `fit` methods, if validation data is provided. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ @doc_controls.for_subclass_implementers @generic_utils.default def on_predict_batch_begin(self, batch, logs=None): """Called at the beginning of a batch in `predict` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers @generic_utils.default def on_predict_batch_end(self, batch, logs=None): """Called at the end of a batch in `predict` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ @doc_controls.for_subclass_implementers def on_train_begin(self, logs=None): """Called at the beginning of training. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers def on_train_end(self, logs=None): """Called at the end of training. Subclasses should override for any actions to run. Args: logs: Dict. Currently the output of the last call to `on_epoch_end()` is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers def on_test_begin(self, logs=None): """Called at the beginning of evaluation or validation. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers def on_test_end(self, logs=None): """Called at the end of evaluation or validation. Subclasses should override for any actions to run. Args: logs: Dict. Currently the output of the last call to `on_test_batch_end()` is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers def on_predict_begin(self, logs=None): """Called at the beginning of prediction. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ @doc_controls.for_subclass_implementers def on_predict_end(self, logs=None): """Called at the end of prediction. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ def _implements_train_batch_hooks(self): """Determines if this Callback should be called for each train batch.""" return (not generic_utils.is_default(self.on_batch_begin) or not generic_utils.is_default(self.on_batch_end) or not generic_utils.is_default(self.on_train_batch_begin) or not generic_utils.is_default(self.on_train_batch_end)) def _implements_test_batch_hooks(self): """Determines if this Callback should be called for each test batch.""" return (not generic_utils.is_default(self.on_test_batch_begin) or not generic_utils.is_default(self.on_test_batch_end)) def _implements_predict_batch_hooks(self): """Determines if this Callback should be called for each predict batch.""" return (not generic_utils.is_default(self.on_predict_batch_begin) or not generic_utils.is_default(self.on_predict_batch_end))
Subclasses
- BackupAndRestore
- BaseLogger
- CSVLogger
- EarlyStopping
- History
- LambdaCallback
- LearningRateScheduler
- ModelCheckpoint
- ProgbarLogger
- ReduceLROnPlateau
- RemoteMonitor
- TensorBoard
- TerminateOnNaN
- BatchCountingCB
- LearningRateBatchScheduler
- Counter
- StepTimingCallback
Methods
def on_batch_begin(self, batch, logs=None)
-
A backwards compatibility alias for
on_train_batch_begin
.Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_batch_begin(self, batch, logs=None): """A backwards compatibility alias for `on_train_batch_begin`."""
def on_batch_end(self, batch, logs=None)
-
A backwards compatibility alias for
on_train_batch_end
.Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_batch_end(self, batch, logs=None): """A backwards compatibility alias for `on_train_batch_end`."""
def on_epoch_begin(self, epoch, logs=None)
-
Called at the start of an epoch.
Subclasses should override for any actions to run. This function should only be called during TRAIN mode.
Args
epoch
- Integer, index of epoch.
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_epoch_begin(self, epoch, logs=None): """Called at the start of an epoch. Subclasses should override for any actions to run. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_epoch_end(self, epoch, logs=None)
-
Called at the end of an epoch.
Subclasses should override for any actions to run. This function should only be called during TRAIN mode.
Args
epoch
- Integer, index of epoch.
logs
- Dict, metric results for this training epoch, and for the
validation epoch if validation is performed. Validation result keys
are prefixed with
val_
. For training epoch, the values of the
Model
's metrics are returned. Example :{'loss': 0.2, 'accuracy': 0.7}
.Expand source code
@doc_controls.for_subclass_implementers def on_epoch_end(self, epoch, logs=None): """Called at the end of an epoch. Subclasses should override for any actions to run. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict, metric results for this training epoch, and for the validation epoch if validation is performed. Validation result keys are prefixed with `val_`. For training epoch, the values of the `Model`'s metrics are returned. Example : `{'loss': 0.2, 'accuracy': 0.7}`. """
def on_predict_batch_begin(self, batch, logs=None)
-
Called at the beginning of a batch in
predict
methods.Subclasses should override for any actions to run.
Note that if the
steps_per_execution
argument tocompile
intf.keras.Model
is set toN
, this method will only be called everyN
batches.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_predict_batch_begin(self, batch, logs=None): """Called at the beginning of a batch in `predict` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_predict_batch_end(self, batch, logs=None)
-
Called at the end of a batch in
predict
methods.Subclasses should override for any actions to run.
Note that if the
steps_per_execution
argument tocompile
intf.keras.Model
is set toN
, this method will only be called everyN
batches.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Aggregated metric results up until this batch.
Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_predict_batch_end(self, batch, logs=None): """Called at the end of a batch in `predict` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """
def on_predict_begin(self, logs=None)
-
Called at the beginning of prediction.
Subclasses should override for any actions to run.
Args
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_predict_begin(self, logs=None): """Called at the beginning of prediction. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_predict_end(self, logs=None)
-
Called at the end of prediction.
Subclasses should override for any actions to run.
Args
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_predict_end(self, logs=None): """Called at the end of prediction. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_test_batch_begin(self, batch, logs=None)
-
Called at the beginning of a batch in
evaluate
methods.Also called at the beginning of a validation batch in the
fit
methods, if validation data is provided.Subclasses should override for any actions to run.
Note that if the
steps_per_execution
argument tocompile
intf.keras.Model
is set toN
, this method will only be called everyN
batches.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_test_batch_begin(self, batch, logs=None): """Called at the beginning of a batch in `evaluate` methods. Also called at the beginning of a validation batch in the `fit` methods, if validation data is provided. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_test_batch_end(self, batch, logs=None)
-
Called at the end of a batch in
evaluate
methods.Also called at the end of a validation batch in the
fit
methods, if validation data is provided.Subclasses should override for any actions to run.
Note that if the
steps_per_execution
argument tocompile
intf.keras.Model
is set toN
, this method will only be called everyN
batches.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Aggregated metric results up until this batch.
Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_test_batch_end(self, batch, logs=None): """Called at the end of a batch in `evaluate` methods. Also called at the end of a validation batch in the `fit` methods, if validation data is provided. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """
def on_test_begin(self, logs=None)
-
Called at the beginning of evaluation or validation.
Subclasses should override for any actions to run.
Args
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_test_begin(self, logs=None): """Called at the beginning of evaluation or validation. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_test_end(self, logs=None)
-
Called at the end of evaluation or validation.
Subclasses should override for any actions to run.
Args
logs
- Dict. Currently the output of the last call to
on_test_batch_end()
is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_test_end(self, logs=None): """Called at the end of evaluation or validation. Subclasses should override for any actions to run. Args: logs: Dict. Currently the output of the last call to `on_test_batch_end()` is passed to this argument for this method but that may change in the future. """
def on_train_batch_begin(self, batch, logs=None)
-
Called at the beginning of a training batch in
fit
methods.Subclasses should override for any actions to run.
Note that if the
steps_per_execution
argument tocompile
intf.keras.Model
is set toN
, this method will only be called everyN
batches.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_train_batch_begin(self, batch, logs=None): """Called at the beginning of a training batch in `fit` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ # For backwards compatibility. self.on_batch_begin(batch, logs=logs)
def on_train_batch_end(self, batch, logs=None)
-
Called at the end of a training batch in
fit
methods.Subclasses should override for any actions to run.
Note that if the
steps_per_execution
argument tocompile
intf.keras.Model
is set toN
, this method will only be called everyN
batches.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Aggregated metric results up until this batch.
Expand source code
@doc_controls.for_subclass_implementers @generic_utils.default def on_train_batch_end(self, batch, logs=None): """Called at the end of a training batch in `fit` methods. Subclasses should override for any actions to run. Note that if the `steps_per_execution` argument to `compile` in `tf.keras.Model` is set to `N`, this method will only be called every `N` batches. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ # For backwards compatibility. self.on_batch_end(batch, logs=logs)
def on_train_begin(self, logs=None)
-
Called at the beginning of training.
Subclasses should override for any actions to run.
Args
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_train_begin(self, logs=None): """Called at the beginning of training. Subclasses should override for any actions to run. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """
def on_train_end(self, logs=None)
-
Called at the end of training.
Subclasses should override for any actions to run.
Args
logs
- Dict. Currently the output of the last call to
on_epoch_end()
is passed to this argument for this method but that may change in the future.
Expand source code
@doc_controls.for_subclass_implementers def on_train_end(self, logs=None): """Called at the end of training. Subclasses should override for any actions to run. Args: logs: Dict. Currently the output of the last call to `on_epoch_end()` is passed to this argument for this method but that may change in the future. """
def set_model(self, model)
-
Expand source code
def set_model(self, model): self.model = model
def set_params(self, params)
-
Expand source code
def set_params(self, params): self.params = params
- You should pack all your callbacks into a single
class CallbackList (callbacks=None, add_history=False, add_progbar=False, model=None, **params)
-
Container abstracting a list of callbacks.
Container for
Callback
instances.This object wraps a list of
Callback
instances, making it possible to call them all at once via a single endpoint (e.g.callback_list.on_epoch_end(…)
).Args
callbacks
- List of
Callback
instances. add_history
- Whether a
History
callback should be added, if one does not already exist in thecallbacks
list. add_progbar
- Whether a
ProgbarLogger
callback should be added, if one does not already exist in thecallbacks
list. model
- The
Model
these callbacks are used with. **params
- If provided, parameters will be passed to each
Callback
viaCallback.set_params()
.
Expand source code
class CallbackList: """Container abstracting a list of callbacks.""" def __init__(self, callbacks=None, add_history=False, add_progbar=False, model=None, **params): """Container for `Callback` instances. This object wraps a list of `Callback` instances, making it possible to call them all at once via a single endpoint (e.g. `callback_list.on_epoch_end(...)`). Args: callbacks: List of `Callback` instances. add_history: Whether a `History` callback should be added, if one does not already exist in the `callbacks` list. add_progbar: Whether a `ProgbarLogger` callback should be added, if one does not already exist in the `callbacks` list. model: The `Model` these callbacks are used with. **params: If provided, parameters will be passed to each `Callback` via `Callback.set_params`. """ self.callbacks = tf.nest.flatten(callbacks) if callbacks else [] self._add_default_callbacks(add_history, add_progbar) if model: self.set_model(model) if params: self.set_params(params) # Performance optimization: determines if batch hooks need to be called. # pylint: disable=protected-access self._supports_tf_logs = all( getattr(cb, '_supports_tf_logs', False) for cb in self.callbacks) self._batch_hooks_support_tf_logs = all( getattr(cb, '_supports_tf_logs', False) for cb in self.callbacks if cb._implements_train_batch_hooks() or cb ._implements_test_batch_hooks() or cb._implements_predict_batch_hooks()) self._should_call_train_batch_hooks = any( cb._implements_train_batch_hooks() for cb in self.callbacks) self._should_call_test_batch_hooks = any( cb._implements_test_batch_hooks() for cb in self.callbacks) self._should_call_predict_batch_hooks = any( cb._implements_predict_batch_hooks() for cb in self.callbacks) # pylint: enable=protected-access self._disallow_batch_hooks_in_ps_strategy() # Performance check: Check batch hooks for slowness compared to batch time. # Only run check for custom callbacks (i.e. not present in this file). self._check_timing = any( cbk.__class__.__name__ not in globals() for cbk in self.callbacks) self._num_batches_for_timing_check = 5 self._hook_times = {} self._batch_start_time = None self._batch_times = [] def _add_default_callbacks(self, add_history, add_progbar): """Adds `Callback`s that are always present.""" self._progbar = None self._history = None for cb in self.callbacks: if isinstance(cb, ProgbarLogger): self._progbar = cb elif isinstance(cb, History): self._history = cb if self._progbar is None and add_progbar: self._progbar = ProgbarLogger(count_mode='steps') self.callbacks.insert(0, self._progbar) if self._history is None and add_history: self._history = History() self.callbacks.append(self._history) def _process_logs(self, logs, is_batch_hook=False): """Turns tensors into numpy arrays or Python scalars if necessary.""" if logs is None: return {} if self._supports_tf_logs: return logs if is_batch_hook and self._batch_hooks_support_tf_logs: return logs return tf_utils.sync_to_numpy_or_python_type(logs) def append(self, callback): self.callbacks.append(callback) def set_params(self, params): self.params = params for callback in self.callbacks: callback.set_params(params) def set_model(self, model): self.model = model if self._history: model.history = self._history for callback in self.callbacks: callback.set_model(model) def _call_batch_hook(self, mode, hook, batch, logs=None): """Helper function for all batch_{begin | end} methods.""" if not self.callbacks: return if hook == 'begin': self._call_batch_begin_hook(mode, batch, logs) elif hook == 'end': self._call_batch_end_hook(mode, batch, logs) else: raise ValueError('Unrecognized hook: {}'.format(hook)) def _call_batch_begin_hook(self, mode, batch, logs): """Helper function for `on_*_batch_begin` methods.""" hook_name = 'on_{mode}_batch_begin'.format(mode=mode) self._call_batch_hook_helper(hook_name, batch, logs) if self._check_timing: self._batch_start_time = time.time() def _call_batch_end_hook(self, mode, batch, logs): """Helper function for `on_*_batch_end` methods.""" hook_name = 'on_{mode}_batch_end'.format(mode=mode) if self._check_timing and batch >= 1: batch_time = time.time() - self._batch_start_time self._batch_times.append(batch_time) self._call_batch_hook_helper(hook_name, batch, logs) if len(self._batch_times) >= self._num_batches_for_timing_check: end_hook_name = hook_name begin_hook_name = 'on_{mode}_batch_begin'.format(mode=mode) avg_batch_time = sum(self._batch_times) / len(self._batch_times) avg_end_hook_time = sum(self._hook_times[end_hook_name]) / len( self._hook_times[end_hook_name]) avg_begin_hook_time = sum(self._hook_times[begin_hook_name]) / len( self._hook_times[begin_hook_name]) threshold_time = 1.0 * avg_batch_time warning_msg = ('Callback method `{hook}` is slow compared to ' 'the batch time (batch time: {batch_time:.4f}s vs ' '`{hook}` time: {hook_time:.4f}s). Check your callbacks.') if avg_begin_hook_time > threshold_time: logging.warning(warning_msg.format( hook=begin_hook_name, batch_time=avg_batch_time, hook_time=avg_begin_hook_time)) if avg_end_hook_time > threshold_time: logging.warning(warning_msg.format( hook=end_hook_name, batch_time=avg_batch_time, hook_time=avg_end_hook_time)) self._check_timing = False self._batch_start_time = None self._batch_times = [] self._hook_times = {} def _call_batch_hook_helper(self, hook_name, batch, logs): """Helper function for `on_*_batch_*` methods.""" if self._check_timing: start_time = time.time() logs = self._process_logs(logs, is_batch_hook=True) for callback in self.callbacks: hook = getattr(callback, hook_name) hook(batch, logs) if self._check_timing: if hook_name not in self._hook_times: self._hook_times[hook_name] = [] self._hook_times[hook_name].append(time.time() - start_time) def _call_begin_hook(self, mode): """Helper function for on_{train|test|predict}_begin methods.""" if mode == ModeKeys.TRAIN: self.on_train_begin() elif mode == ModeKeys.TEST: self.on_test_begin() else: self.on_predict_begin() def _call_end_hook(self, mode): """Helper function for on_{train|test|predict}_end methods.""" if mode == ModeKeys.TRAIN: self.on_train_end() elif mode == ModeKeys.TEST: self.on_test_end() else: self.on_predict_end() def on_batch_begin(self, batch, logs=None): if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'begin', batch, logs=logs) def on_batch_end(self, batch, logs=None): if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'end', batch, logs=logs) def on_epoch_begin(self, epoch, logs=None): """Calls the `on_epoch_begin` methods of its callbacks. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_epoch_begin(epoch, logs) def on_epoch_end(self, epoch, logs=None): """Calls the `on_epoch_end` methods of its callbacks. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict, metric results for this training epoch, and for the validation epoch if validation is performed. Validation result keys are prefixed with `val_`. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_epoch_end(epoch, logs) def on_train_batch_begin(self, batch, logs=None): """Calls the `on_train_batch_begin` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict, contains the return value of `model.train_step`. Typically, the values of the `Model`'s metrics are returned. Example: `{'loss': 0.2, 'accuracy': 0.7}`. """ if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'begin', batch, logs=logs) def on_train_batch_end(self, batch, logs=None): """Calls the `on_train_batch_end` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'end', batch, logs=logs) def on_test_batch_begin(self, batch, logs=None): """Calls the `on_test_batch_begin` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict, contains the return value of `model.test_step`. Typically, the values of the `Model`'s metrics are returned. Example: `{'loss': 0.2, 'accuracy': 0.7}`. """ if self._should_call_test_batch_hooks: self._call_batch_hook(ModeKeys.TEST, 'begin', batch, logs=logs) def on_test_batch_end(self, batch, logs=None): """Calls the `on_test_batch_end` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ if self._should_call_test_batch_hooks: self._call_batch_hook(ModeKeys.TEST, 'end', batch, logs=logs) def on_predict_batch_begin(self, batch, logs=None): """Calls the `on_predict_batch_begin` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict, contains the return value of `model.predict_step`, it typically returns a dict with a key 'outputs' containing the model's outputs. """ if self._should_call_predict_batch_hooks: self._call_batch_hook(ModeKeys.PREDICT, 'begin', batch, logs=logs) def on_predict_batch_end(self, batch, logs=None): """Calls the `on_predict_batch_end` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ if self._should_call_predict_batch_hooks: self._call_batch_hook(ModeKeys.PREDICT, 'end', batch, logs=logs) def on_train_begin(self, logs=None): """Calls the `on_train_begin` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_train_begin(logs) def on_train_end(self, logs=None): """Calls the `on_train_end` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_train_end(logs) def on_test_begin(self, logs=None): """Calls the `on_test_begin` methods of its callbacks. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_test_begin(logs) def on_test_end(self, logs=None): """Calls the `on_test_end` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_test_end(logs) def on_predict_begin(self, logs=None): """Calls the 'on_predict_begin` methods of its callbacks. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_predict_begin(logs) def on_predict_end(self, logs=None): """Calls the `on_predict_end` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_predict_end(logs) def __iter__(self): return iter(self.callbacks) def _disallow_batch_hooks_in_ps_strategy(self): """Error out if batch-level callbacks are passed with PSStrategy.""" # pylint: disable=protected-access strategy = tf.distribute.get_strategy() if strategy._should_use_with_coordinator: unsupported_callbacks = [] for cb in self.callbacks: # These Callbacks can accept RemoteValues directly. if getattr(cb, '_supports_tf_logs', False): continue if (cb._implements_train_batch_hooks() or cb._implements_test_batch_hooks() or cb._implements_predict_batch_hooks()): unsupported_callbacks.append(cb) if unsupported_callbacks: raise ValueError('Batch-level `Callback`s are not supported with ' '`ParameterServerStrategy`. Found unsupported ' 'callbacks: {}'.format(unsupported_callbacks)) # pylint: enable=protected-access
Methods
def append(self, callback)
-
Expand source code
def append(self, callback): self.callbacks.append(callback)
def on_batch_begin(self, batch, logs=None)
-
Expand source code
def on_batch_begin(self, batch, logs=None): if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'begin', batch, logs=logs)
def on_batch_end(self, batch, logs=None)
-
Expand source code
def on_batch_end(self, batch, logs=None): if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'end', batch, logs=logs)
def on_epoch_begin(self, epoch, logs=None)
-
Calls the
on_epoch_begin
methods of its callbacks.This function should only be called during TRAIN mode.
Args
epoch
- Integer, index of epoch.
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
def on_epoch_begin(self, epoch, logs=None): """Calls the `on_epoch_begin` methods of its callbacks. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_epoch_begin(epoch, logs)
def on_epoch_end(self, epoch, logs=None)
-
Calls the
on_epoch_end
methods of its callbacks.This function should only be called during TRAIN mode.
Args
epoch
- Integer, index of epoch.
logs
- Dict, metric results for this training epoch, and for the
validation epoch if validation is performed. Validation result keys
are prefixed with
val_
.
Expand source code
def on_epoch_end(self, epoch, logs=None): """Calls the `on_epoch_end` methods of its callbacks. This function should only be called during TRAIN mode. Args: epoch: Integer, index of epoch. logs: Dict, metric results for this training epoch, and for the validation epoch if validation is performed. Validation result keys are prefixed with `val_`. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_epoch_end(epoch, logs)
def on_predict_batch_begin(self, batch, logs=None)
-
Calls the
on_predict_batch_begin
methods of its callbacks.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict, contains the return value of
model.predict_step
, it typically returns a dict with a key 'outputs' containing the model's outputs.
Expand source code
def on_predict_batch_begin(self, batch, logs=None): """Calls the `on_predict_batch_begin` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict, contains the return value of `model.predict_step`, it typically returns a dict with a key 'outputs' containing the model's outputs. """ if self._should_call_predict_batch_hooks: self._call_batch_hook(ModeKeys.PREDICT, 'begin', batch, logs=logs)
def on_predict_batch_end(self, batch, logs=None)
-
Calls the
on_predict_batch_end
methods of its callbacks.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Aggregated metric results up until this batch.
Expand source code
def on_predict_batch_end(self, batch, logs=None): """Calls the `on_predict_batch_end` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ if self._should_call_predict_batch_hooks: self._call_batch_hook(ModeKeys.PREDICT, 'end', batch, logs=logs)
def on_predict_begin(self, logs=None)
-
Calls the 'on_predict_begin` methods of its callbacks.
Args
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
def on_predict_begin(self, logs=None): """Calls the 'on_predict_begin` methods of its callbacks. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_predict_begin(logs)
def on_predict_end(self, logs=None)
-
Calls the
on_predict_end
methods of its callbacks.Args
logs
- Dict. Currently, no data is passed via this argument for this method, but that may change in the future.
Expand source code
def on_predict_end(self, logs=None): """Calls the `on_predict_end` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_predict_end(logs)
def on_test_batch_begin(self, batch, logs=None)
-
Calls the
on_test_batch_begin
methods of its callbacks.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict, contains the return value of
model.test_step
. Typically, the values of theModel
's metrics are returned. Example:{'loss': 0.2, 'accuracy': 0.7}
.
Expand source code
def on_test_batch_begin(self, batch, logs=None): """Calls the `on_test_batch_begin` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict, contains the return value of `model.test_step`. Typically, the values of the `Model`'s metrics are returned. Example: `{'loss': 0.2, 'accuracy': 0.7}`. """ if self._should_call_test_batch_hooks: self._call_batch_hook(ModeKeys.TEST, 'begin', batch, logs=logs)
def on_test_batch_end(self, batch, logs=None)
-
Calls the
on_test_batch_end
methods of its callbacks.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Aggregated metric results up until this batch.
Expand source code
def on_test_batch_end(self, batch, logs=None): """Calls the `on_test_batch_end` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ if self._should_call_test_batch_hooks: self._call_batch_hook(ModeKeys.TEST, 'end', batch, logs=logs)
def on_test_begin(self, logs=None)
-
Calls the
on_test_begin
methods of its callbacks.Args
logs
- Dict. Currently no data is passed to this argument for this method but that may change in the future.
Expand source code
def on_test_begin(self, logs=None): """Calls the `on_test_begin` methods of its callbacks. Args: logs: Dict. Currently no data is passed to this argument for this method but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_test_begin(logs)
def on_test_end(self, logs=None)
-
Calls the
on_test_end
methods of its callbacks.Args
logs
- Dict. Currently, no data is passed via this argument for this method, but that may change in the future.
Expand source code
def on_test_end(self, logs=None): """Calls the `on_test_end` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_test_end(logs)
def on_train_batch_begin(self, batch, logs=None)
-
Calls the
on_train_batch_begin
methods of its callbacks.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict, contains the return value of
model.train_step
. Typically, the values of theModel
's metrics are returned. Example:{'loss': 0.2, 'accuracy': 0.7}
.
Expand source code
def on_train_batch_begin(self, batch, logs=None): """Calls the `on_train_batch_begin` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict, contains the return value of `model.train_step`. Typically, the values of the `Model`'s metrics are returned. Example: `{'loss': 0.2, 'accuracy': 0.7}`. """ if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'begin', batch, logs=logs)
def on_train_batch_end(self, batch, logs=None)
-
Calls the
on_train_batch_end
methods of its callbacks.Args
batch
- Integer, index of batch within the current epoch.
logs
- Dict. Aggregated metric results up until this batch.
Expand source code
def on_train_batch_end(self, batch, logs=None): """Calls the `on_train_batch_end` methods of its callbacks. Args: batch: Integer, index of batch within the current epoch. logs: Dict. Aggregated metric results up until this batch. """ if self._should_call_train_batch_hooks: self._call_batch_hook(ModeKeys.TRAIN, 'end', batch, logs=logs)
def on_train_begin(self, logs=None)
-
Calls the
on_train_begin
methods of its callbacks.Args
logs
- Dict. Currently, no data is passed via this argument for this method, but that may change in the future.
Expand source code
def on_train_begin(self, logs=None): """Calls the `on_train_begin` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_train_begin(logs)
def on_train_end(self, logs=None)
-
Calls the
on_train_end
methods of its callbacks.Args
logs
- Dict. Currently, no data is passed via this argument for this method, but that may change in the future.
Expand source code
def on_train_end(self, logs=None): """Calls the `on_train_end` methods of its callbacks. Args: logs: Dict. Currently, no data is passed via this argument for this method, but that may change in the future. """ logs = self._process_logs(logs) for callback in self.callbacks: callback.on_train_end(logs)
def set_model(self, model)
-
Expand source code
def set_model(self, model): self.model = model if self._history: model.history = self._history for callback in self.callbacks: callback.set_model(model)
def set_params(self, params)
-
Expand source code
def set_params(self, params): self.params = params for callback in self.callbacks: callback.set_params(params)
class EarlyStopping (monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False)
-
Stop training when a monitored metric has stopped improving.
Assuming the goal of a training is to minimize the loss. With this, the metric to be monitored would be
'loss'
, and mode would be'min'
. Amodel.fit()
training loop will check at end of every epoch whether the loss is no longer decreasing, considering themin_delta
andpatience
if applicable. Once it's found no longer decreasing,model.stop_training
is marked True and the training terminates.The quantity to be monitored needs to be available in
logs
dict. To make it so, pass the loss or metrics atmodel.compile()
.Args
monitor
- Quantity to be monitored.
min_delta
- Minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement.
patience
- Number of epochs with no improvement after which training will be stopped.
verbose
- verbosity mode.
mode
- One of
{"auto", "min", "max"}
. Inmin
mode, training will stop when the quantity monitored has stopped decreasing; in"max"
mode it will stop when the quantity monitored has stopped increasing; in"auto"
mode, the direction is automatically inferred from the name of the monitored quantity. baseline
- Baseline value for the monitored quantity. Training will stop if the model doesn't show improvement over the baseline.
restore_best_weights
- Whether to restore model weights from
the epoch with the best value of the monitored quantity.
If False, the model weights obtained at the last step of
training are used. An epoch will be restored regardless
of the performance relative to the
baseline
. If no epoch improves onbaseline
, training will run forpatience
epochs and restore weights from the best epoch in that set.
Example:
>>> callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3) >>> # This callback will stop the training when there is no improvement in >>> # the loss for three consecutive epochs. >>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)]) >>> model.compile(tf.keras.optimizers.SGD(), loss='mse') >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), ... epochs=10, batch_size=1, callbacks=[callback], ... verbose=0) >>> len(history.history['loss']) # Only 4 epochs are run. 4
Expand source code
class EarlyStopping(Callback): """Stop training when a monitored metric has stopped improving. Assuming the goal of a training is to minimize the loss. With this, the metric to be monitored would be `'loss'`, and mode would be `'min'`. A `model.fit()` training loop will check at end of every epoch whether the loss is no longer decreasing, considering the `min_delta` and `patience` if applicable. Once it's found no longer decreasing, `model.stop_training` is marked True and the training terminates. The quantity to be monitored needs to be available in `logs` dict. To make it so, pass the loss or metrics at `model.compile()`. Args: monitor: Quantity to be monitored. min_delta: Minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement. patience: Number of epochs with no improvement after which training will be stopped. verbose: verbosity mode. mode: One of `{"auto", "min", "max"}`. In `min` mode, training will stop when the quantity monitored has stopped decreasing; in `"max"` mode it will stop when the quantity monitored has stopped increasing; in `"auto"` mode, the direction is automatically inferred from the name of the monitored quantity. baseline: Baseline value for the monitored quantity. Training will stop if the model doesn't show improvement over the baseline. restore_best_weights: Whether to restore model weights from the epoch with the best value of the monitored quantity. If False, the model weights obtained at the last step of training are used. An epoch will be restored regardless of the performance relative to the `baseline`. If no epoch improves on `baseline`, training will run for `patience` epochs and restore weights from the best epoch in that set. Example: >>> callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3) >>> # This callback will stop the training when there is no improvement in >>> # the loss for three consecutive epochs. >>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)]) >>> model.compile(tf.keras.optimizers.SGD(), loss='mse') >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), ... epochs=10, batch_size=1, callbacks=[callback], ... verbose=0) >>> len(history.history['loss']) # Only 4 epochs are run. 4 """ def __init__(self, monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False): super(EarlyStopping, self).__init__() self.monitor = monitor self.patience = patience self.verbose = verbose self.baseline = baseline self.min_delta = abs(min_delta) self.wait = 0 self.stopped_epoch = 0 self.restore_best_weights = restore_best_weights self.best_weights = None if mode not in ['auto', 'min', 'max']: logging.warning('EarlyStopping mode %s is unknown, ' 'fallback to auto mode.', mode) mode = 'auto' if mode == 'min': self.monitor_op = np.less elif mode == 'max': self.monitor_op = np.greater else: if 'acc' in self.monitor: self.monitor_op = np.greater else: self.monitor_op = np.less if self.monitor_op == np.greater: self.min_delta *= 1 else: self.min_delta *= -1 def on_train_begin(self, logs=None): # Allow instances to be re-used self.wait = 0 self.stopped_epoch = 0 self.best = np.Inf if self.monitor_op == np.less else -np.Inf self.best_weights = None def on_epoch_end(self, epoch, logs=None): current = self.get_monitor_value(logs) if current is None: return if self.restore_best_weights and self.best_weights is None: # Restore the weights after first epoch if no progress is ever made. self.best_weights = self.model.get_weights() self.wait += 1 if self._is_improvement(current, self.best): self.best = current if self.restore_best_weights: self.best_weights = self.model.get_weights() # Only restart wait if we beat both the baseline and our previous best. if self.baseline is None or self._is_improvement(current, self.baseline): self.wait = 0 if self.wait >= self.patience: self.stopped_epoch = epoch self.model.stop_training = True if self.restore_best_weights and self.best_weights is not None: if self.verbose > 0: print('Restoring model weights from the end of the best epoch.') self.model.set_weights(self.best_weights) def on_train_end(self, logs=None): if self.stopped_epoch > 0 and self.verbose > 0: print('Epoch %05d: early stopping' % (self.stopped_epoch + 1)) def get_monitor_value(self, logs): logs = logs or {} monitor_value = logs.get(self.monitor) if monitor_value is None: logging.warning('Early stopping conditioned on metric `%s` ' 'which is not available. Available metrics are: %s', self.monitor, ','.join(list(logs.keys()))) return monitor_value def _is_improvement(self, monitor_value, reference_value): return self.monitor_op(monitor_value - self.min_delta, reference_value)
Ancestors
Methods
def get_monitor_value(self, logs)
-
Expand source code
def get_monitor_value(self, logs): logs = logs or {} monitor_value = logs.get(self.monitor) if monitor_value is None: logging.warning('Early stopping conditioned on metric `%s` ' 'which is not available. Available metrics are: %s', self.monitor, ','.join(list(logs.keys()))) return monitor_value
Inherited members
class History
-
Callback that records events into a
History
object.This callback is automatically applied to every Keras model. The
History
object gets returned by thefit
method of models.Example:
>>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)]) >>> model.compile(tf.keras.optimizers.SGD(), loss='mse') >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), ... epochs=10) >>> print(history.params) {'verbose': 1, 'epochs': 10, 'steps': 1} >>> # check the keys of history object >>> print(history.history.keys()) dict_keys(['loss'])
Expand source code
class History(Callback): """Callback that records events into a `History` object. This callback is automatically applied to every Keras model. The `History` object gets returned by the `fit` method of models. Example: >>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)]) >>> model.compile(tf.keras.optimizers.SGD(), loss='mse') >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), ... epochs=10) >>> print(history.params) {'verbose': 1, 'epochs': 10, 'steps': 1} >>> # check the keys of history object >>> print(history.history.keys()) dict_keys(['loss']) """ def __init__(self): super(History, self).__init__() self.history = {} def on_train_begin(self, logs=None): self.epoch = [] def on_epoch_end(self, epoch, logs=None): logs = logs or {} self.epoch.append(epoch) for k, v in logs.items(): self.history.setdefault(k, []).append(v) # Set the history attribute on the model after the epoch ends. This will # make sure that the state which is set is the latest one. self.model.history = self
Ancestors
Inherited members
class LambdaCallback (on_epoch_begin=None, on_epoch_end=None, on_batch_begin=None, on_batch_end=None, on_train_begin=None, on_train_end=None, **kwargs)
-
Callback for creating simple, custom callbacks on-the-fly.
This callback is constructed with anonymous functions that will be called at the appropriate time (during
Model.{fit | evaluate | predict}
). Note that the callbacks expects positional arguments, as:on_epoch_begin
andon_epoch_end
expect two positional arguments:epoch
,logs
on_batch_begin
andon_batch_end
expect two positional arguments:batch
,logs
on_train_begin
andon_train_end
expect one positional argument:logs
Args
on_epoch_begin
- called at the beginning of every epoch.
on_epoch_end
- called at the end of every epoch.
on_batch_begin
- called at the beginning of every batch.
on_batch_end
- called at the end of every batch.
on_train_begin
- called at the beginning of model training.
on_train_end
- called at the end of model training.
Example:
# Print the batch number at the beginning of every batch. batch_print_callback = LambdaCallback( on_batch_begin=lambda batch,logs: print(batch)) # Stream the epoch loss to a file in JSON format. The file content # is not well-formed JSON but rather has a JSON object per line. import json json_log = open('loss_log.json', mode='wt', buffering=1) json_logging_callback = LambdaCallback( on_epoch_end=lambda epoch, logs: json_log.write( json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'), on_train_end=lambda logs: json_log.close() ) # Terminate some processes after having finished model training. processes = ... cleanup_callback = LambdaCallback( on_train_end=lambda logs: [ p.terminate() for p in processes if p.is_alive()]) model.fit(..., callbacks=[batch_print_callback, json_logging_callback, cleanup_callback])
Expand source code
class LambdaCallback(Callback): r"""Callback for creating simple, custom callbacks on-the-fly. This callback is constructed with anonymous functions that will be called at the appropriate time (during `Model.{fit | evaluate | predict}`). Note that the callbacks expects positional arguments, as: - `on_epoch_begin` and `on_epoch_end` expect two positional arguments: `epoch`, `logs` - `on_batch_begin` and `on_batch_end` expect two positional arguments: `batch`, `logs` - `on_train_begin` and `on_train_end` expect one positional argument: `logs` Args: on_epoch_begin: called at the beginning of every epoch. on_epoch_end: called at the end of every epoch. on_batch_begin: called at the beginning of every batch. on_batch_end: called at the end of every batch. on_train_begin: called at the beginning of model training. on_train_end: called at the end of model training. Example: ```python # Print the batch number at the beginning of every batch. batch_print_callback = LambdaCallback( on_batch_begin=lambda batch,logs: print(batch)) # Stream the epoch loss to a file in JSON format. The file content # is not well-formed JSON but rather has a JSON object per line. import json json_log = open('loss_log.json', mode='wt', buffering=1) json_logging_callback = LambdaCallback( on_epoch_end=lambda epoch, logs: json_log.write( json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'), on_train_end=lambda logs: json_log.close() ) # Terminate some processes after having finished model training. processes = ... cleanup_callback = LambdaCallback( on_train_end=lambda logs: [ p.terminate() for p in processes if p.is_alive()]) model.fit(..., callbacks=[batch_print_callback, json_logging_callback, cleanup_callback]) ``` """ def __init__(self, on_epoch_begin=None, on_epoch_end=None, on_batch_begin=None, on_batch_end=None, on_train_begin=None, on_train_end=None, **kwargs): super(LambdaCallback, self).__init__() self.__dict__.update(kwargs) if on_epoch_begin is not None: self.on_epoch_begin = on_epoch_begin else: self.on_epoch_begin = lambda epoch, logs: None if on_epoch_end is not None: self.on_epoch_end = on_epoch_end else: self.on_epoch_end = lambda epoch, logs: None if on_batch_begin is not None: self.on_batch_begin = on_batch_begin else: self.on_batch_begin = lambda batch, logs: None if on_batch_end is not None: self.on_batch_end = on_batch_end else: self.on_batch_end = lambda batch, logs: None if on_train_begin is not None: self.on_train_begin = on_train_begin else: self.on_train_begin = lambda logs: None if on_train_end is not None: self.on_train_end = on_train_end else: self.on_train_end = lambda logs: None
Ancestors
Inherited members
class LearningRateScheduler (schedule, verbose=0)
-
Learning rate scheduler.
At the beginning of every epoch, this callback gets the updated learning rate value from
schedule
function provided at__init__
, with the current epoch and current learning rate, and applies the updated learning rate on the optimizer.Args
schedule
- a function that takes an epoch index (integer, indexed from 0) and current learning rate (float) as inputs and returns a new learning rate as output (float).
verbose
- int. 0: quiet, 1: update messages.
Example:
>>> # This function keeps the initial learning rate for the first ten epochs >>> # and decreases it exponentially after that. >>> def scheduler(epoch, lr): ... if epoch < 10: ... return lr ... else: ... return lr * tf.math.exp(-0.1) >>> >>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)]) >>> model.compile(tf.keras.optimizers.SGD(), loss='mse') >>> round(model.optimizer.lr.numpy(), 5) 0.01
>>> callback = tf.keras.callbacks.LearningRateScheduler(scheduler) >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), ... epochs=15, callbacks=[callback], verbose=0) >>> round(model.optimizer.lr.numpy(), 5) 0.00607
Expand source code
class LearningRateScheduler(Callback): """Learning rate scheduler. At the beginning of every epoch, this callback gets the updated learning rate value from `schedule` function provided at `__init__`, with the current epoch and current learning rate, and applies the updated learning rate on the optimizer. Args: schedule: a function that takes an epoch index (integer, indexed from 0) and current learning rate (float) as inputs and returns a new learning rate as output (float). verbose: int. 0: quiet, 1: update messages. Example: >>> # This function keeps the initial learning rate for the first ten epochs >>> # and decreases it exponentially after that. >>> def scheduler(epoch, lr): ... if epoch < 10: ... return lr ... else: ... return lr * tf.math.exp(-0.1) >>> >>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)]) >>> model.compile(tf.keras.optimizers.SGD(), loss='mse') >>> round(model.optimizer.lr.numpy(), 5) 0.01 >>> callback = tf.keras.callbacks.LearningRateScheduler(scheduler) >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), ... epochs=15, callbacks=[callback], verbose=0) >>> round(model.optimizer.lr.numpy(), 5) 0.00607 """ def __init__(self, schedule, verbose=0): super(LearningRateScheduler, self).__init__() self.schedule = schedule self.verbose = verbose def on_epoch_begin(self, epoch, logs=None): if not hasattr(self.model.optimizer, 'lr'): raise ValueError('Optimizer must have a "lr" attribute.') try: # new API lr = float(backend.get_value(self.model.optimizer.lr)) lr = self.schedule(epoch, lr) except TypeError: # Support for old API for backward compatibility lr = self.schedule(epoch) if not isinstance(lr, (tf.Tensor, float, np.float32, np.float64)): raise ValueError('The output of the "schedule" function ' 'should be float.') if isinstance(lr, tf.Tensor) and not lr.dtype.is_floating: raise ValueError('The dtype of Tensor should be float') backend.set_value(self.model.optimizer.lr, backend.get_value(lr)) if self.verbose > 0: print('\nEpoch %05d: LearningRateScheduler setting learning ' 'rate to %s.' % (epoch + 1, lr)) def on_epoch_end(self, epoch, logs=None): logs = logs or {} logs['lr'] = backend.get_value(self.model.optimizer.lr)
Ancestors
Inherited members
class ModelCheckpoint (filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', save_freq='epoch', options=None, **kwargs)
-
Callback to save the Keras model or model weights at some frequency.
ModelCheckpoint
callback is used in conjunction with training usingmodel.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.
Note: If you get
WARNING:tensorflow:Can save best model only with <name> available, skipping<code> see the description of the </code>monitor
argument for details on how to get this right.Example:
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).filepath
can contain named formatting options, which will be filled the value ofepoch
and keys inlogs
(passed inon_epoch_end
). For example: iffilepath
isweights.{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.compile
method. Note:- Prefix the name with
"val_
" to monitor validation metrics. - Use
"loss"
or "val_loss
" to monitor the model's total loss. - If you specify metrics as strings, like
"accuracy"
, pass the same string (with or without the"val_"
prefix). - If you pass
metrics.Metric
objects,monitor
should be set tometric.name
- If you're not sure about the metric names you can check the contents
of the
history.history
dictionary returned byhistory = model.fit()
- Multi-output models set additional prefixes on the metric names.
- Prefix the name with
verbose
- verbosity mode, 0 or 1.
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. Iffilepath
doesn't contain formatting options like{epoch}
thenfilepath
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. Forval_acc
, this should bemax
, forval_loss
this should bemin
, etc. Inauto
mode, the mode is set tomax
if the quantities monitored are 'acc' or start with 'fmeasure' and are set tomin
for 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 theModel
is 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.CheckpointOptions
object ifsave_weights_only
is true or optionaltf.saved_model.SaveOptions
object ifsave_weights_only
is false. **kwargs
- Additional arguments for backwards compatibility. Possible key
is
period
.
Expand source code
class ModelCheckpoint(Callback): """Callback to save the Keras model or model weights at some frequency. `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. Note: If you get `WARNING:tensorflow:Can save best model only with <name> available, skipping` see the description of the `monitor` argument for details on how to get this right. Example: ```python 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). `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. 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.compile` method. Note: * Prefix the name with `"val_`" to monitor validation metrics. * Use `"loss"` or "`val_loss`" to monitor the model's total loss. * If you specify metrics as strings, like `"accuracy"`, pass the same string (with or without the `"val_"` prefix). * If you pass `metrics.Metric` objects, `monitor` should be set to `metric.name` * If you're not sure about the metric names you can check the contents of the `history.history` dictionary returned by `history = model.fit()` * Multi-output models set additional prefixes on the metric names. verbose: verbosity mode, 0 or 1. 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. 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 mode is set to `max` if the quantities monitored are 'acc' or start with 'fmeasure' and are set to `min` for 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 the `Model` is compiled with `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.SaveOptions` object if `save_weights_only` is false. **kwargs: Additional arguments for backwards compatibility. Possible key is `period`. """ def __init__(self, filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', save_freq='epoch', options=None, **kwargs): super(ModelCheckpoint, self).__init__() self._supports_tf_logs = True self.monitor = monitor self.verbose = verbose self.filepath = path_to_string(filepath) self.save_best_only = save_best_only self.save_weights_only = save_weights_only self.save_freq = save_freq self.epochs_since_last_save = 0 self._batches_seen_since_last_saving = 0 self._last_batch_seen = 0 if save_weights_only: if options is None or isinstance( options, tf.train.CheckpointOptions): self._options = options or tf.train.CheckpointOptions() else: raise TypeError('If save_weights_only is True, then `options` must be ' 'either None or a tf.train.CheckpointOptions') else: if options is None or isinstance(options, tf.saved_model.SaveOptions): self._options = options or tf.saved_model.SaveOptions() else: raise TypeError('If save_weights_only is False, then `options` must be' 'either None or a tf.saved_model.SaveOptions') # Deprecated field `load_weights_on_restart` is for loading the checkpoint # file from `filepath` at the start of `model.fit()` # TODO(rchao): Remove the arg during next breaking release. if 'load_weights_on_restart' in kwargs: self.load_weights_on_restart = kwargs['load_weights_on_restart'] logging.warning('`load_weights_on_restart` argument is deprecated. ' 'Please use `model.load_weights()` for loading weights ' 'before the start of `model.fit()`.') else: self.load_weights_on_restart = False # Deprecated field `period` is for the number of epochs between which # the model is saved. if 'period' in kwargs: self.period = kwargs['period'] logging.warning('`period` argument is deprecated. Please use `save_freq` ' 'to specify the frequency in number of batches seen.') else: self.period = 1 if mode not in ['auto', 'min', 'max']: logging.warning('ModelCheckpoint mode %s is unknown, ' 'fallback to auto mode.', mode) mode = 'auto' if mode == 'min': self.monitor_op = np.less self.best = np.Inf elif mode == 'max': self.monitor_op = np.greater self.best = -np.Inf else: if 'acc' in self.monitor or self.monitor.startswith('fmeasure'): self.monitor_op = np.greater self.best = -np.Inf else: self.monitor_op = np.less self.best = np.Inf if self.save_freq != 'epoch' and not isinstance(self.save_freq, int): raise ValueError('Unrecognized save_freq: {}'.format(self.save_freq)) # Only the chief worker writes model checkpoints, but all workers # restore checkpoint at on_train_begin(). self._chief_worker_only = False def on_train_begin(self, logs=None): if self.load_weights_on_restart: filepath_to_load = ( self._get_most_recently_modified_file_matching_pattern(self.filepath)) if (filepath_to_load is not None and self._checkpoint_exists(filepath_to_load)): try: # `filepath` may contain placeholders such as `{epoch:02d}`, and # thus it attempts to load the most recently modified file with file # name matching the pattern. self.model.load_weights(filepath_to_load) except (IOError, ValueError) as e: raise ValueError('Error loading file from {}. Reason: {}'.format( filepath_to_load, e)) def _implements_train_batch_hooks(self): # Only call batch hooks when saving on batch return self.save_freq != 'epoch' def on_train_batch_end(self, batch, logs=None): if self._should_save_on_batch(batch): self._save_model(epoch=self._current_epoch, batch=batch, logs=logs) def on_epoch_begin(self, epoch, logs=None): self._current_epoch = epoch def on_epoch_end(self, epoch, logs=None): self.epochs_since_last_save += 1 # pylint: disable=protected-access if self.save_freq == 'epoch': self._save_model(epoch=epoch, batch=None, logs=logs) def _should_save_on_batch(self, batch): """Handles batch-level saving logic, supports steps_per_execution.""" if self.save_freq == 'epoch': return False if batch <= self._last_batch_seen: # New epoch. add_batches = batch + 1 # batches are zero-indexed. else: add_batches = batch - self._last_batch_seen self._batches_seen_since_last_saving += add_batches self._last_batch_seen = batch if self._batches_seen_since_last_saving >= self.save_freq: self._batches_seen_since_last_saving = 0 return True return False def _save_model(self, epoch, batch, logs): """Saves the model. Args: epoch: the epoch this iteration is in. batch: the batch this iteration is in. `None` if the `save_freq` is set to `epoch`. logs: the `logs` dict passed in to `on_batch_end` or `on_epoch_end`. """ logs = logs or {} if isinstance(self.save_freq, int) or self.epochs_since_last_save >= self.period: # Block only when saving interval is reached. logs = tf_utils.sync_to_numpy_or_python_type(logs) self.epochs_since_last_save = 0 filepath = self._get_file_path(epoch, batch, logs) try: if self.save_best_only: current = logs.get(self.monitor) if current is None: logging.warning('Can save best model only with %s available, ' 'skipping.', self.monitor) else: if self.monitor_op(current, self.best): if self.verbose > 0: print('\nEpoch %05d: %s improved from %0.5f to %0.5f,' ' saving model to %s' % (epoch + 1, self.monitor, self.best, current, filepath)) self.best = current if self.save_weights_only: self.model.save_weights( filepath, overwrite=True, options=self._options) else: self.model.save(filepath, overwrite=True, options=self._options) else: if self.verbose > 0: print('\nEpoch %05d: %s did not improve from %0.5f' % (epoch + 1, self.monitor, self.best)) else: if self.verbose > 0: print('\nEpoch %05d: saving model to %s' % (epoch + 1, filepath)) if self.save_weights_only: self.model.save_weights( filepath, overwrite=True, options=self._options) else: self.model.save(filepath, overwrite=True, options=self._options) self._maybe_remove_file() except IsADirectoryError as e: # h5py 3.x raise IOError('Please specify a non-directory filepath for ' 'ModelCheckpoint. Filepath used is an existing ' 'directory: {}'.format(filepath)) except IOError as e: # h5py 2.x # `e.errno` appears to be `None` so checking the content of `e.args[0]`. if 'is a directory' in str(e.args[0]).lower(): raise IOError('Please specify a non-directory filepath for ' 'ModelCheckpoint. Filepath used is an existing ' 'directory: {}'.format(filepath)) # Re-throw the error for any other causes. raise e def _get_file_path(self, epoch, batch, logs): """Returns the file path for checkpoint.""" # pylint: disable=protected-access try: # `filepath` may contain placeholders such as `{epoch:02d}`,`{batch:02d}` # and `{mape:.2f}`. A mismatch between logged metrics and the path's # placeholders can cause formatting to fail. if batch is None or 'batch' in logs: file_path = self.filepath.format(epoch=epoch + 1, **logs) else: file_path = self.filepath.format( epoch=epoch + 1, batch=batch + 1, **logs) except KeyError as e: raise KeyError('Failed to format this callback filepath: "{}". ' 'Reason: {}'.format(self.filepath, e)) self._write_filepath = distributed_file_utils.write_filepath( file_path, self.model.distribute_strategy) return self._write_filepath def _maybe_remove_file(self): # Remove the checkpoint directory in multi-worker training where this worker # should not checkpoint. It is a dummy directory previously saved for sync # distributed training. distributed_file_utils.remove_temp_dir_with_filepath( self._write_filepath, self.model.distribute_strategy) def _checkpoint_exists(self, filepath): """Returns whether the checkpoint `filepath` refers to exists.""" if filepath.endswith('.h5'): return tf.io.gfile.exists(filepath) tf_saved_model_exists = tf.io.gfile.exists(filepath) tf_weights_only_checkpoint_exists = tf.io.gfile.exists( filepath + '.index') return tf_saved_model_exists or tf_weights_only_checkpoint_exists def _get_most_recently_modified_file_matching_pattern(self, pattern): """Returns the most recently modified filepath matching pattern. Pattern may contain python formatting placeholder. If `tf.train.latest_checkpoint()` does not return None, use that; otherwise, check for most recently modified one that matches the pattern. In the rare case where there are more than one pattern-matching file having the same modified time that is most recent among all, return the filepath that is largest (by `>` operator, lexicographically using the numeric equivalents). This provides a tie-breaker when multiple files are most recent. Note that a larger `filepath` can sometimes indicate a later time of modification (for instance, when epoch/batch is used as formatting option), but not necessarily (when accuracy or loss is used). The tie-breaker is put in the logic as best effort to return the most recent, and to avoid undeterministic result. Modified time of a file is obtained with `os.path.getmtime()`. This utility function is best demonstrated via an example: ```python file_pattern = 'f.batch{batch:02d}epoch{epoch:02d}.h5' test_dir = self.get_temp_dir() path_pattern = os.path.join(test_dir, file_pattern) file_paths = [ os.path.join(test_dir, file_name) for file_name in ['f.batch03epoch02.h5', 'f.batch02epoch02.h5', 'f.batch01epoch01.h5'] ] for file_path in file_paths: # Write something to each of the files self.assertEqual( _get_most_recently_modified_file_matching_pattern(path_pattern), file_paths[-1]) ``` Args: pattern: The file pattern that may optionally contain python placeholder such as `{epoch:02d}`. Returns: The most recently modified file's full filepath matching `pattern`. If `pattern` does not contain any placeholder, this returns the filepath that exactly matches `pattern`. Returns `None` if no match is found. """ dir_name = os.path.dirname(pattern) base_name = os.path.basename(pattern) base_name_regex = '^' + re.sub(r'{.*}', r'.*', base_name) + '$' # If tf.train.latest_checkpoint tells us there exists a latest checkpoint, # use that as it is more robust than `os.path.getmtime()`. latest_tf_checkpoint = tf.train.latest_checkpoint(dir_name) if latest_tf_checkpoint is not None and re.match( base_name_regex, os.path.basename(latest_tf_checkpoint)): return latest_tf_checkpoint latest_mod_time = 0 file_path_with_latest_mod_time = None n_file_with_latest_mod_time = 0 file_path_with_largest_file_name = None if tf.io.gfile.exists(dir_name): for file_name in os.listdir(dir_name): # Only consider if `file_name` matches the pattern. if re.match(base_name_regex, file_name): file_path = os.path.join(dir_name, file_name) mod_time = os.path.getmtime(file_path) if (file_path_with_largest_file_name is None or file_path > file_path_with_largest_file_name): file_path_with_largest_file_name = file_path if mod_time > latest_mod_time: latest_mod_time = mod_time file_path_with_latest_mod_time = file_path # In the case a file with later modified time is found, reset # the counter for the number of files with latest modified time. n_file_with_latest_mod_time = 1 elif mod_time == latest_mod_time: # In the case a file has modified time tied with the most recent, # increment the counter for the number of files with latest modified # time by 1. n_file_with_latest_mod_time += 1 if n_file_with_latest_mod_time == 1: # Return the sole file that has most recent modified time. return file_path_with_latest_mod_time else: # If there are more than one file having latest modified time, return # the file path with the largest file name. return file_path_with_largest_file_name
Ancestors
Inherited members
class ProgbarLogger (count_mode='samples', stateful_metrics=None)
-
Callback that prints metrics to stdout.
Args
count_mode
- One of
"steps"
or"samples"
. Whether the progress bar should count samples seen or steps (batches) seen. stateful_metrics
- Iterable of string names of metrics that
should not be averaged over an epoch.
Metrics in this list will be logged as-is.
All others will be averaged over time (e.g. loss, etc).
If not provided, defaults to the
Model
's metrics.
Raises
ValueError
- In case of invalid
count_mode
.
Expand source code
class ProgbarLogger(Callback): """Callback that prints metrics to stdout. Args: count_mode: One of `"steps"` or `"samples"`. Whether the progress bar should count samples seen or steps (batches) seen. stateful_metrics: Iterable of string names of metrics that should *not* be averaged over an epoch. Metrics in this list will be logged as-is. All others will be averaged over time (e.g. loss, etc). If not provided, defaults to the `Model`'s metrics. Raises: ValueError: In case of invalid `count_mode`. """ def __init__(self, count_mode='samples', stateful_metrics=None): super(ProgbarLogger, self).__init__() self._supports_tf_logs = True if count_mode == 'samples': self.use_steps = False elif count_mode == 'steps': self.use_steps = True else: raise ValueError('Unknown `count_mode`: ' + str(count_mode)) # Defaults to all Model's metrics except for loss. self.stateful_metrics = set(stateful_metrics) if stateful_metrics else set() self.seen = 0 self.progbar = None self.target = None self.verbose = 1 self.epochs = 1 self._train_step, self._test_step, self._predict_step = None, None, None self._call_batch_hooks = True self._called_in_fit = False def set_params(self, params): self.verbose = params['verbose'] self.epochs = params['epochs'] if self.use_steps and 'steps' in params: self.target = params['steps'] elif not self.use_steps and 'samples' in params: self.target = params['samples'] else: self.target = None # Will be inferred at the end of the first epoch. self._call_batch_hooks = self.verbose == 1 if self.target is None: try: self._train_step = self.model._train_counter # pylint: disable=protected-access self._test_step = self.model._test_counter # pylint: disable=protected-access self._predict_step = self.model._predict_counter # pylint: disable=protected-access except AttributeError: self._call_batch_hooks = True def on_train_begin(self, logs=None): # When this logger is called inside `fit`, validation is silent. self._called_in_fit = True def on_test_begin(self, logs=None): if not self._called_in_fit: self._reset_progbar() self._maybe_init_progbar() def on_predict_begin(self, logs=None): self._reset_progbar() self._maybe_init_progbar() def on_epoch_begin(self, epoch, logs=None): self._reset_progbar() self._maybe_init_progbar() if self.verbose and self.epochs > 1: print('Epoch %d/%d' % (epoch + 1, self.epochs)) def on_train_batch_end(self, batch, logs=None): self._batch_update_progbar(batch, logs) def on_test_batch_end(self, batch, logs=None): if not self._called_in_fit: self._batch_update_progbar(batch, logs) def on_predict_batch_end(self, batch, logs=None): # Don't pass prediction results. self._batch_update_progbar(batch, None) def on_epoch_end(self, epoch, logs=None): self._finalize_progbar(logs, self._train_step) def on_test_end(self, logs=None): if not self._called_in_fit: self._finalize_progbar(logs, self._test_step) def on_predict_end(self, logs=None): self._finalize_progbar(logs, self._predict_step) def _reset_progbar(self): self.seen = 0 self.progbar = None def _maybe_init_progbar(self): """Instantiate a `Progbar` if not yet, and update the stateful metrics.""" # TODO(rchao): Legacy TF1 code path may use list for # `self.stateful_metrics`. Remove "cast to set" when TF1 support is dropped. self.stateful_metrics = set(self.stateful_metrics) if self.model: # Update the existing stateful metrics as `self.model.metrics` may contain # updated metrics after `MetricsContainer` is built in the first train # step. self.stateful_metrics = self.stateful_metrics.union( set(m.name for m in self.model.metrics)) if self.progbar is None: self.progbar = Progbar( target=self.target, verbose=self.verbose, stateful_metrics=self.stateful_metrics, unit_name='step' if self.use_steps else 'sample') self.progbar._update_stateful_metrics(self.stateful_metrics) # pylint: disable=protected-access def _implements_train_batch_hooks(self): return self._call_batch_hooks def _implements_test_batch_hooks(self): return self._call_batch_hooks def _implements_predict_batch_hooks(self): return self._call_batch_hooks def _batch_update_progbar(self, batch, logs=None): """Updates the progbar.""" logs = logs or {} self._maybe_init_progbar() if self.use_steps: self.seen = batch + 1 # One-indexed. else: # v1 path only. logs = copy.copy(logs) batch_size = logs.pop('size', 0) num_steps = logs.pop('num_steps', 1) logs.pop('batch', None) add_seen = num_steps * batch_size self.seen += add_seen if self.verbose == 1: # Only block async when verbose = 1. logs = tf_utils.sync_to_numpy_or_python_type(logs) self.progbar.update(self.seen, list(logs.items()), finalize=False) def _finalize_progbar(self, logs, counter): logs = tf_utils.sync_to_numpy_or_python_type(logs or {}) if self.target is None: if counter is not None: counter = counter.numpy() if not self.use_steps: counter *= logs.get('size', 1) self.target = counter or self.seen self.progbar.target = self.target self.progbar.update(self.target, list(logs.items()), finalize=True)
Ancestors
Methods
def set_params(self, params)
-
Expand source code
def set_params(self, params): self.verbose = params['verbose'] self.epochs = params['epochs'] if self.use_steps and 'steps' in params: self.target = params['steps'] elif not self.use_steps and 'samples' in params: self.target = params['samples'] else: self.target = None # Will be inferred at the end of the first epoch. self._call_batch_hooks = self.verbose == 1 if self.target is None: try: self._train_step = self.model._train_counter # pylint: disable=protected-access self._test_step = self.model._test_counter # pylint: disable=protected-access self._predict_step = self.model._predict_counter # pylint: disable=protected-access except AttributeError: self._call_batch_hooks = True
Inherited members
class ReduceLROnPlateau (monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0, **kwargs)
-
Reduce learning rate when a metric has stopped improving.
Models often benefit from reducing the learning rate by a factor of 2-10 once learning stagnates. This callback monitors a quantity and if no improvement is seen for a 'patience' number of epochs, the learning rate is reduced.
Example:
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.001) model.fit(X_train, Y_train, callbacks=[reduce_lr])
Args
monitor
- quantity to be monitored.
factor
- factor by which the learning rate will be reduced.
new_lr = lr * factor
. patience
- number of epochs with no improvement after which learning rate will be reduced.
verbose
- int. 0: quiet, 1: update messages.
mode
- one of
{'auto', 'min', 'max'}
. In'min'
mode, the learning rate will be reduced when the quantity monitored has stopped decreasing; in'max'
mode it will be reduced when the quantity monitored has stopped increasing; in'auto'
mode, the direction is automatically inferred from the name of the monitored quantity. min_delta
- threshold for measuring the new optimum, to only focus on significant changes.
cooldown
- number of epochs to wait before resuming normal operation after lr has been reduced.
min_lr
- lower bound on the learning rate.
Expand source code
class ReduceLROnPlateau(Callback): """Reduce learning rate when a metric has stopped improving. Models often benefit from reducing the learning rate by a factor of 2-10 once learning stagnates. This callback monitors a quantity and if no improvement is seen for a 'patience' number of epochs, the learning rate is reduced. Example: ```python reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.001) model.fit(X_train, Y_train, callbacks=[reduce_lr]) ``` Args: monitor: quantity to be monitored. factor: factor by which the learning rate will be reduced. `new_lr = lr * factor`. patience: number of epochs with no improvement after which learning rate will be reduced. verbose: int. 0: quiet, 1: update messages. mode: one of `{'auto', 'min', 'max'}`. In `'min'` mode, the learning rate will be reduced when the quantity monitored has stopped decreasing; in `'max'` mode it will be reduced when the quantity monitored has stopped increasing; in `'auto'` mode, the direction is automatically inferred from the name of the monitored quantity. min_delta: threshold for measuring the new optimum, to only focus on significant changes. cooldown: number of epochs to wait before resuming normal operation after lr has been reduced. min_lr: lower bound on the learning rate. """ def __init__(self, monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=1e-4, cooldown=0, min_lr=0, **kwargs): super(ReduceLROnPlateau, self).__init__() self.monitor = monitor if factor >= 1.0: raise ValueError('ReduceLROnPlateau ' 'does not support a factor >= 1.0.') if 'epsilon' in kwargs: min_delta = kwargs.pop('epsilon') logging.warning('`epsilon` argument is deprecated and ' 'will be removed, use `min_delta` instead.') self.factor = factor self.min_lr = min_lr self.min_delta = min_delta self.patience = patience self.verbose = verbose self.cooldown = cooldown self.cooldown_counter = 0 # Cooldown counter. self.wait = 0 self.best = 0 self.mode = mode self.monitor_op = None self._reset() def _reset(self): """Resets wait counter and cooldown counter. """ if self.mode not in ['auto', 'min', 'max']: logging.warning('Learning rate reduction mode %s is unknown, ' 'fallback to auto mode.', self.mode) self.mode = 'auto' if (self.mode == 'min' or (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.min_delta) self.best = np.Inf else: self.monitor_op = lambda a, b: np.greater(a, b + self.min_delta) self.best = -np.Inf self.cooldown_counter = 0 self.wait = 0 def on_train_begin(self, logs=None): self._reset() def on_epoch_end(self, epoch, logs=None): logs = logs or {} logs['lr'] = backend.get_value(self.model.optimizer.lr) current = logs.get(self.monitor) if current is None: logging.warning('Learning rate reduction is conditioned on metric `%s` ' 'which is not available. Available metrics are: %s', self.monitor, ','.join(list(logs.keys()))) else: if self.in_cooldown(): self.cooldown_counter -= 1 self.wait = 0 if self.monitor_op(current, self.best): self.best = current self.wait = 0 elif not self.in_cooldown(): self.wait += 1 if self.wait >= self.patience: old_lr = backend.get_value(self.model.optimizer.lr) if old_lr > np.float32(self.min_lr): new_lr = old_lr * self.factor new_lr = max(new_lr, self.min_lr) backend.set_value(self.model.optimizer.lr, new_lr) if self.verbose > 0: print('\nEpoch %05d: ReduceLROnPlateau reducing learning ' 'rate to %s.' % (epoch + 1, new_lr)) self.cooldown_counter = self.cooldown self.wait = 0 def in_cooldown(self): return self.cooldown_counter > 0
Ancestors
Methods
def in_cooldown(self)
-
Expand source code
def in_cooldown(self): return self.cooldown_counter > 0
Inherited members
class RemoteMonitor (root='http://localhost:9000', path='/publish/epoch/end/', field='data', headers=None, send_as_json=False)
-
Callback used to stream events to a server.
Requires the
requests
library. Events are sent toroot + '/publish/epoch/end/'
by default. Calls are HTTP POST, with adata
argument which is a JSON-encoded dictionary of event data. Ifsend_as_json=True
, the content type of the request will be"application/json"
. Otherwise the serialized JSON will be sent within a form.Args
root
- String; root url of the target server.
path
- String; path relative to
root
to which the events will be sent. field
- String; JSON field under which the data will be stored. The field is used only if the payload is sent within a form (i.e. send_as_json is set to False).
headers
- Dictionary; optional custom HTTP headers.
send_as_json
- Boolean; whether the request should be
sent as
"application/json"
.
Expand source code
class RemoteMonitor(Callback): """Callback used to stream events to a server. Requires the `requests` library. Events are sent to `root + '/publish/epoch/end/'` by default. Calls are HTTP POST, with a `data` argument which is a JSON-encoded dictionary of event data. If `send_as_json=True`, the content type of the request will be `"application/json"`. Otherwise the serialized JSON will be sent within a form. Args: root: String; root url of the target server. path: String; path relative to `root` to which the events will be sent. field: String; JSON field under which the data will be stored. The field is used only if the payload is sent within a form (i.e. send_as_json is set to False). headers: Dictionary; optional custom HTTP headers. send_as_json: Boolean; whether the request should be sent as `"application/json"`. """ def __init__(self, root='http://localhost:9000', path='/publish/epoch/end/', field='data', headers=None, send_as_json=False): super(RemoteMonitor, self).__init__() self.root = root self.path = path self.field = field self.headers = headers self.send_as_json = send_as_json def on_epoch_end(self, epoch, logs=None): if requests is None: raise ImportError('RemoteMonitor requires the `requests` library.') logs = logs or {} send = {} send['epoch'] = epoch for k, v in logs.items(): # np.ndarray and np.generic are not scalar types # therefore we must unwrap their scalar values and # pass to the json-serializable dict 'send' if isinstance(v, (np.ndarray, np.generic)): send[k] = v.item() else: send[k] = v try: if self.send_as_json: requests.post(self.root + self.path, json=send, headers=self.headers) else: requests.post( self.root + self.path, {self.field: json.dumps(send)}, headers=self.headers) except requests.exceptions.RequestException: logging.warning('Warning: could not reach RemoteMonitor ' 'root server at ' + str(self.root))
Ancestors
Inherited members
class TensorBoard (log_dir='./logs', histogram_freq=0, batch_size=32, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None, update_freq='epoch', profile_batch=2)
-
Enable visualizations for TensorBoard.
TensorBoard is a visualization tool provided with TensorFlow.
This callback logs events for TensorBoard, including: * Metrics summary plots * Training graph visualization * Activation histograms * Sampled profiling
If you have installed TensorFlow with pip, you should be able to launch TensorBoard from the command line:
tensorboard --logdir=path_to_your_logs
You can find more information about TensorBoard here.
Args
log_dir
- the path of the directory where to save the log files to be parsed by TensorBoard.
histogram_freq
- frequency (in epochs) at which to compute activation and weight histograms for the layers of the model. If set to 0, histograms won't be computed. Validation data (or split) must be specified for histogram visualizations.
write_graph
- whether to visualize the graph in TensorBoard. The log file can become quite large when write_graph is set to True.
write_grads
- whether to visualize gradient histograms in TensorBoard.
histogram_freq
must be greater than 0. batch_size
- size of batch of inputs to feed to the network for histograms computation.
write_images
- whether to write model weights to visualize as image in TensorBoard.
embeddings_freq
- frequency (in epochs) at which selected embedding layers
will be saved. If set to 0, embeddings won't be computed. Data to be
visualized in TensorBoard's Embedding tab must be passed as
embeddings_data
. embeddings_layer_names
- a list of names of layers to keep eye on. If None or empty list all the embedding layer will be watched.
embeddings_metadata
- a dictionary which maps layer name to a file name in which metadata for this embedding layer is saved. Here are details about metadata files format. In case if the same metadata file is used for all embedding layers, string can be passed.
embeddings_data
- data to be embedded at layers specified in
embeddings_layer_names
. Numpy array (if the model has a single input) or list of Numpy arrays (if the model has multiple inputs). Learn more about embeddings in this guide. update_freq
'batch'
or'epoch'
or integer. When using'batch'
, writes the losses and metrics to TensorBoard after each batch. The same applies for'epoch'
. If using an integer, let's say1000
, the callback will write the metrics and losses to TensorBoard every 1000 samples. Note that writing too frequently to TensorBoard can slow down your training.profile_batch
- Profile the batch to sample compute characteristics. By default, it will profile the second batch. Set profile_batch=0 to disable profiling.
Raises
ValueError
- If histogram_freq is set and no validation data is provided.
@compatibility(eager) Using the
TensorBoard
callback will work when eager execution is enabled, with the restriction that outputting histogram summaries of weights and gradients is not supported. Consequently,histogram_freq
will be ignored. @end_compatibilityExpand source code
class TensorBoard(callbacks.TensorBoard): # pylint: disable=line-too-long """Enable visualizations for TensorBoard. TensorBoard is a visualization tool provided with TensorFlow. This callback logs events for TensorBoard, including: * Metrics summary plots * Training graph visualization * Activation histograms * Sampled profiling If you have installed TensorFlow with pip, you should be able to launch TensorBoard from the command line: ```sh tensorboard --logdir=path_to_your_logs ``` You can find more information about TensorBoard [here](https://www.tensorflow.org/get_started/summaries_and_tensorboard). Args: log_dir: the path of the directory where to save the log files to be parsed by TensorBoard. histogram_freq: frequency (in epochs) at which to compute activation and weight histograms for the layers of the model. If set to 0, histograms won't be computed. Validation data (or split) must be specified for histogram visualizations. write_graph: whether to visualize the graph in TensorBoard. The log file can become quite large when write_graph is set to True. write_grads: whether to visualize gradient histograms in TensorBoard. `histogram_freq` must be greater than 0. batch_size: size of batch of inputs to feed to the network for histograms computation. write_images: whether to write model weights to visualize as image in TensorBoard. embeddings_freq: frequency (in epochs) at which selected embedding layers will be saved. If set to 0, embeddings won't be computed. Data to be visualized in TensorBoard's Embedding tab must be passed as `embeddings_data`. embeddings_layer_names: a list of names of layers to keep eye on. If None or empty list all the embedding layer will be watched. embeddings_metadata: a dictionary which maps layer name to a file name in which metadata for this embedding layer is saved. [Here are details]( https://www.tensorflow.org/how_tos/embedding_viz/#metadata_optional) about metadata files format. In case if the same metadata file is used for all embedding layers, string can be passed. embeddings_data: data to be embedded at layers specified in `embeddings_layer_names`. Numpy array (if the model has a single input) or list of Numpy arrays (if the model has multiple inputs). Learn more about embeddings [in this guide]( https://www.tensorflow.org/programmers_guide/embedding). update_freq: `'batch'` or `'epoch'` or integer. When using `'batch'`, writes the losses and metrics to TensorBoard after each batch. The same applies for `'epoch'`. If using an integer, let's say `1000`, the callback will write the metrics and losses to TensorBoard every 1000 samples. Note that writing too frequently to TensorBoard can slow down your training. profile_batch: Profile the batch to sample compute characteristics. By default, it will profile the second batch. Set profile_batch=0 to disable profiling. Raises: ValueError: If histogram_freq is set and no validation data is provided. @compatibility(eager) Using the `TensorBoard` callback will work when eager execution is enabled, with the restriction that outputting histogram summaries of weights and gradients is not supported. Consequently, `histogram_freq` will be ignored. @end_compatibility """ # pylint: enable=line-too-long def __init__(self, log_dir='./logs', histogram_freq=0, batch_size=32, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None, update_freq='epoch', profile_batch=2): # Don't call super's init since it is an eager-only version. callbacks.Callback.__init__(self) self.log_dir = log_dir self.histogram_freq = histogram_freq if self.histogram_freq and tf.executing_eagerly(): logging.warning( UserWarning('Weight and gradient histograms not supported for eager' 'execution, setting `histogram_freq` to `0`.')) self.histogram_freq = 0 self.merged = None self.write_graph = write_graph self.write_grads = write_grads self.write_images = write_images self.batch_size = batch_size self._current_batch = 0 self._total_batches_seen = 0 self._total_val_batches_seen = 0 self.embeddings_freq = embeddings_freq self.embeddings_layer_names = embeddings_layer_names self.embeddings_metadata = embeddings_metadata self.embeddings_data = embeddings_data if update_freq == 'batch': self.update_freq = 1 else: self.update_freq = update_freq self._samples_seen = 0 self._samples_seen_at_last_write = 0 # TODO(fishx): Add a link to the full profiler tutorial. self._profile_batch = profile_batch # True when the profiler was successfully started by this callback. # We track the status here to make sure callbacks do not interfere with # each other. The callback will only stop the profiler it started. self._profiler_started = False # TensorBoard should only write summaries on the chief when in a # Multi-Worker setting. self._chief_worker_only = True def _init_writer(self, model): """Sets file writer.""" if tf.executing_eagerly(): self.writer = tf.summary.create_file_writer(self.log_dir) if not model.run_eagerly and self.write_graph: with self.writer.as_default(): tf.summary.graph(K.get_graph()) elif self.write_graph: self.writer = tf.compat.v1.summary.FileWriter(self.log_dir, K.get_graph()) else: self.writer = tf.compat.v1.summary.FileWriter(self.log_dir) def _make_histogram_ops(self, model): """Defines histogram ops when histogram_freq > 0.""" # only make histogram summary op if it hasn't already been made if self.histogram_freq and self.merged is None: for layer in self.model.layers: for weight in layer.weights: mapped_weight_name = weight.name.replace(':', '_') tf.compat.v1.summary.histogram(mapped_weight_name, weight) if self.write_images: w_img = tf.compat.v1.squeeze(weight) shape = K.int_shape(w_img) if len(shape) == 2: # dense layer kernel case if shape[0] > shape[1]: w_img = tf.compat.v1.transpose(w_img) shape = K.int_shape(w_img) w_img = tf.reshape(w_img, [1, shape[0], shape[1], 1]) elif len(shape) == 3: # convnet case if K.image_data_format() == 'channels_last': # switch to channels_first to display # every kernel as a separate image w_img = tf.compat.v1.transpose(w_img, perm=[2, 0, 1]) shape = K.int_shape(w_img) w_img = tf.reshape(w_img, [shape[0], shape[1], shape[2], 1]) elif len(shape) == 1: # bias case w_img = tf.reshape(w_img, [1, shape[0], 1, 1]) else: # not possible to handle 3D convnets etc. continue shape = K.int_shape(w_img) assert len(shape) == 4 and shape[-1] in [1, 3, 4] tf.compat.v1.summary.image(mapped_weight_name, w_img) if self.write_grads: for weight in layer.trainable_weights: mapped_weight_name = weight.name.replace(':', '_') grads = model.optimizer.get_gradients(model.total_loss, weight) def is_indexed_slices(grad): return type(grad).__name__ == 'IndexedSlices' grads = [ grad.values if is_indexed_slices(grad) else grad for grad in grads ] tf.compat.v1.summary.histogram('{}_grad'.format(mapped_weight_name), grads) if hasattr(layer, 'output'): if isinstance(layer.output, list): for i, output in enumerate(layer.output): tf.compat.v1.summary.histogram('{}_out_{}'.format(layer.name, i), output) else: tf.compat.v1.summary.histogram('{}_out'.format(layer.name), layer.output) def set_model(self, model): """Sets Keras model and creates summary ops.""" self.model = model self._init_writer(model) # histogram summaries only enabled in graph mode if not tf.executing_eagerly(): self._make_histogram_ops(model) self.merged = tf.compat.v1.summary.merge_all() # If both embedding_freq and embeddings_data are available, we will # visualize embeddings. if self.embeddings_freq and self.embeddings_data is not None: # Avoid circular dependency. from keras.engine import training_utils_v1 # pylint: disable=g-import-not-at-top self.embeddings_data = training_utils_v1.standardize_input_data( self.embeddings_data, model.input_names) # If embedding_layer_names are not provided, get all of the embedding # layers from the model. embeddings_layer_names = self.embeddings_layer_names if not embeddings_layer_names: embeddings_layer_names = [ layer.name for layer in self.model.layers if type(layer).__name__ == 'Embedding' ] self.assign_embeddings = [] embeddings_vars = {} self.batch_id = batch_id = tf.compat.v1.placeholder(tf.int32) self.step = step = tf.compat.v1.placeholder(tf.int32) for layer in self.model.layers: if layer.name in embeddings_layer_names: embedding_input = self.model.get_layer(layer.name).output embedding_size = np.prod(embedding_input.shape[1:]) embedding_input = tf.reshape(embedding_input, (step, int(embedding_size))) shape = (self.embeddings_data[0].shape[0], int(embedding_size)) embedding = tf.Variable( tf.zeros(shape), name=layer.name + '_embedding') embeddings_vars[layer.name] = embedding batch = tf.compat.v1.assign(embedding[batch_id:batch_id + step], embedding_input) self.assign_embeddings.append(batch) self.saver = tf.compat.v1.train.Saver(list(embeddings_vars.values())) # Create embeddings_metadata dictionary if isinstance(self.embeddings_metadata, str): embeddings_metadata = { layer_name: self.embeddings_metadata for layer_name in embeddings_vars.keys() } else: # If embedding_metadata is already a dictionary embeddings_metadata = self.embeddings_metadata try: from tensorboard.plugins import projector except ImportError: raise ImportError('Failed to import TensorBoard. Please make sure that ' 'TensorBoard integration is complete."') # TODO(psv): Add integration tests to test embedding visualization # with TensorBoard callback. We are unable to write a unit test for this # because TensorBoard dependency assumes TensorFlow package is installed. config = projector.ProjectorConfig() for layer_name, tensor in embeddings_vars.items(): embedding = config.embeddings.add() embedding.tensor_name = tensor.name if (embeddings_metadata is not None and layer_name in embeddings_metadata): embedding.metadata_path = embeddings_metadata[layer_name] projector.visualize_embeddings(self.writer, config) def _fetch_callback(self, summary): self.writer.add_summary(summary, self._total_val_batches_seen) self._total_val_batches_seen += 1 def _write_custom_summaries(self, step, logs=None): """Writes metrics out as custom scalar summaries. Args: step: the global step to use for TensorBoard. logs: dict. Keys are scalar summary names, values are NumPy scalars. """ logs = logs or {} if tf.executing_eagerly(): # use v2 summary ops with self.writer.as_default(), tf.summary.record_if(True): for name, value in logs.items(): if isinstance(value, np.ndarray): value = value.item() tf.summary.scalar(name, value, step=step) else: # use FileWriter from v1 summary for name, value in logs.items(): if isinstance(value, np.ndarray): value = value.item() summary = tf.compat.v1.Summary() summary_value = summary.value.add() summary_value.simple_value = value summary_value.tag = name self.writer.add_summary(summary, step) self.writer.flush() def on_train_batch_begin(self, batch, logs=None): if self._total_batches_seen == self._profile_batch - 1: self._start_profiler() def on_train_batch_end(self, batch, logs=None): return self.on_batch_end(batch, logs) def on_test_begin(self, logs=None): pass def on_test_end(self, logs=None): pass def on_batch_end(self, batch, logs=None): """Writes scalar summaries for metrics on every training batch. Performs profiling if current batch is in profiler_batches. """ # Don't output batch_size and batch number as TensorBoard summaries logs = logs or {} self._samples_seen += logs.get('size', 1) samples_seen_since = self._samples_seen - self._samples_seen_at_last_write if self.update_freq != 'epoch' and samples_seen_since >= self.update_freq: batch_logs = {('batch_' + k): v for k, v in logs.items() if k not in ['batch', 'size', 'num_steps']} self._write_custom_summaries(self._total_batches_seen, batch_logs) self._samples_seen_at_last_write = self._samples_seen self._total_batches_seen += 1 self._stop_profiler() def on_train_begin(self, logs=None): pass def on_epoch_begin(self, epoch, logs=None): """Add histogram op to Model eval_function callbacks, reset batch count.""" # check if histogram summary should be run for this epoch if self.histogram_freq and epoch % self.histogram_freq == 0: # pylint: disable=protected-access # add the histogram summary op if it should run this epoch self.model._make_test_function() if self.merged not in self.model.test_function.fetches: self.model.test_function.fetches.append(self.merged) self.model.test_function.fetch_callbacks[ self.merged] = self._fetch_callback # pylint: enable=protected-access def on_epoch_end(self, epoch, logs=None): """Checks if summary ops should run next epoch, logs scalar summaries.""" # don't output batch_size and # batch number as TensorBoard summaries logs = {('epoch_' + k): v for k, v in logs.items() if k not in ['batch', 'size', 'num_steps']} if self.update_freq == 'epoch': step = epoch else: step = self._samples_seen self._write_custom_summaries(step, logs) # pop the histogram summary op after each epoch if self.histogram_freq: # pylint: disable=protected-access if self.merged in self.model.test_function.fetches: self.model.test_function.fetches.remove(self.merged) if self.merged in self.model.test_function.fetch_callbacks: self.model.test_function.fetch_callbacks.pop(self.merged) # pylint: enable=protected-access if self.embeddings_data is None and self.embeddings_freq: raise ValueError('To visualize embeddings, embeddings_data must ' 'be provided.') if self.embeddings_freq and self.embeddings_data is not None: if epoch % self.embeddings_freq == 0: # We need a second forward-pass here because we're passing # the `embeddings_data` explicitly. This design allows to pass # arbitrary data as `embeddings_data` and results from the fact # that we need to know the size of the `tf.Variable`s which # hold the embeddings in `set_model`. At this point, however, # the `validation_data` is not yet set. embeddings_data = self.embeddings_data n_samples = embeddings_data[0].shape[0] i = 0 sess = K.get_session() while i < n_samples: step = min(self.batch_size, n_samples - i) batch = slice(i, i + step) if isinstance(self.model.input, list): feed_dict = { model_input: embeddings_data[idx][batch] for idx, model_input in enumerate(self.model.input) } else: feed_dict = {self.model.input: embeddings_data[0][batch]} feed_dict.update({self.batch_id: i, self.step: step}) if not isinstance(K.learning_phase(), int): feed_dict[K.learning_phase()] = False sess.run(self.assign_embeddings, feed_dict=feed_dict) self.saver.save(sess, os.path.join(self.log_dir, 'keras_embedding.ckpt'), epoch) i += self.batch_size def on_train_end(self, logs=None): self._stop_profiler() self.writer.close() def _start_profiler(self): """Starts the profiler if currently inactive.""" if self._profiler_started: return try: tf.profiler.experimental.start(logdir=self.log_dir) self._profiler_started = True except tf.errors.AlreadyExistsError as e: # Profiler errors should not be fatal. logging.error('Failed to start profiler: %s', e.message) def _stop_profiler(self): """Stops the profiler if currently active.""" if not self._profiler_started: return try: tf.profiler.experimental.stop() except tf.errors.UnavailableError as e: # Profiler errors should not be fatal. logging.error('Failed to stop profiler: %s', e.message) finally: self._profiler_started = False
Ancestors
Methods
def on_batch_end(self, batch, logs=None)
-
Writes scalar summaries for metrics on every training batch.
Performs profiling if current batch is in profiler_batches.
Expand source code
def on_batch_end(self, batch, logs=None): """Writes scalar summaries for metrics on every training batch. Performs profiling if current batch is in profiler_batches. """ # Don't output batch_size and batch number as TensorBoard summaries logs = logs or {} self._samples_seen += logs.get('size', 1) samples_seen_since = self._samples_seen - self._samples_seen_at_last_write if self.update_freq != 'epoch' and samples_seen_since >= self.update_freq: batch_logs = {('batch_' + k): v for k, v in logs.items() if k not in ['batch', 'size', 'num_steps']} self._write_custom_summaries(self._total_batches_seen, batch_logs) self._samples_seen_at_last_write = self._samples_seen self._total_batches_seen += 1 self._stop_profiler()
def on_epoch_begin(self, epoch, logs=None)
-
Add histogram op to Model eval_function callbacks, reset batch count.
Expand source code
def on_epoch_begin(self, epoch, logs=None): """Add histogram op to Model eval_function callbacks, reset batch count.""" # check if histogram summary should be run for this epoch if self.histogram_freq and epoch % self.histogram_freq == 0: # pylint: disable=protected-access # add the histogram summary op if it should run this epoch self.model._make_test_function() if self.merged not in self.model.test_function.fetches: self.model.test_function.fetches.append(self.merged) self.model.test_function.fetch_callbacks[ self.merged] = self._fetch_callback # pylint: enable=protected-access
def on_epoch_end(self, epoch, logs=None)
-
Checks if summary ops should run next epoch, logs scalar summaries.
Expand source code
def on_epoch_end(self, epoch, logs=None): """Checks if summary ops should run next epoch, logs scalar summaries.""" # don't output batch_size and # batch number as TensorBoard summaries logs = {('epoch_' + k): v for k, v in logs.items() if k not in ['batch', 'size', 'num_steps']} if self.update_freq == 'epoch': step = epoch else: step = self._samples_seen self._write_custom_summaries(step, logs) # pop the histogram summary op after each epoch if self.histogram_freq: # pylint: disable=protected-access if self.merged in self.model.test_function.fetches: self.model.test_function.fetches.remove(self.merged) if self.merged in self.model.test_function.fetch_callbacks: self.model.test_function.fetch_callbacks.pop(self.merged) # pylint: enable=protected-access if self.embeddings_data is None and self.embeddings_freq: raise ValueError('To visualize embeddings, embeddings_data must ' 'be provided.') if self.embeddings_freq and self.embeddings_data is not None: if epoch % self.embeddings_freq == 0: # We need a second forward-pass here because we're passing # the `embeddings_data` explicitly. This design allows to pass # arbitrary data as `embeddings_data` and results from the fact # that we need to know the size of the `tf.Variable`s which # hold the embeddings in `set_model`. At this point, however, # the `validation_data` is not yet set. embeddings_data = self.embeddings_data n_samples = embeddings_data[0].shape[0] i = 0 sess = K.get_session() while i < n_samples: step = min(self.batch_size, n_samples - i) batch = slice(i, i + step) if isinstance(self.model.input, list): feed_dict = { model_input: embeddings_data[idx][batch] for idx, model_input in enumerate(self.model.input) } else: feed_dict = {self.model.input: embeddings_data[0][batch]} feed_dict.update({self.batch_id: i, self.step: step}) if not isinstance(K.learning_phase(), int): feed_dict[K.learning_phase()] = False sess.run(self.assign_embeddings, feed_dict=feed_dict) self.saver.save(sess, os.path.join(self.log_dir, 'keras_embedding.ckpt'), epoch) i += self.batch_size
def set_model(self, model)
-
Sets Keras model and creates summary ops.
Expand source code
def set_model(self, model): """Sets Keras model and creates summary ops.""" self.model = model self._init_writer(model) # histogram summaries only enabled in graph mode if not tf.executing_eagerly(): self._make_histogram_ops(model) self.merged = tf.compat.v1.summary.merge_all() # If both embedding_freq and embeddings_data are available, we will # visualize embeddings. if self.embeddings_freq and self.embeddings_data is not None: # Avoid circular dependency. from keras.engine import training_utils_v1 # pylint: disable=g-import-not-at-top self.embeddings_data = training_utils_v1.standardize_input_data( self.embeddings_data, model.input_names) # If embedding_layer_names are not provided, get all of the embedding # layers from the model. embeddings_layer_names = self.embeddings_layer_names if not embeddings_layer_names: embeddings_layer_names = [ layer.name for layer in self.model.layers if type(layer).__name__ == 'Embedding' ] self.assign_embeddings = [] embeddings_vars = {} self.batch_id = batch_id = tf.compat.v1.placeholder(tf.int32) self.step = step = tf.compat.v1.placeholder(tf.int32) for layer in self.model.layers: if layer.name in embeddings_layer_names: embedding_input = self.model.get_layer(layer.name).output embedding_size = np.prod(embedding_input.shape[1:]) embedding_input = tf.reshape(embedding_input, (step, int(embedding_size))) shape = (self.embeddings_data[0].shape[0], int(embedding_size)) embedding = tf.Variable( tf.zeros(shape), name=layer.name + '_embedding') embeddings_vars[layer.name] = embedding batch = tf.compat.v1.assign(embedding[batch_id:batch_id + step], embedding_input) self.assign_embeddings.append(batch) self.saver = tf.compat.v1.train.Saver(list(embeddings_vars.values())) # Create embeddings_metadata dictionary if isinstance(self.embeddings_metadata, str): embeddings_metadata = { layer_name: self.embeddings_metadata for layer_name in embeddings_vars.keys() } else: # If embedding_metadata is already a dictionary embeddings_metadata = self.embeddings_metadata try: from tensorboard.plugins import projector except ImportError: raise ImportError('Failed to import TensorBoard. Please make sure that ' 'TensorBoard integration is complete."') # TODO(psv): Add integration tests to test embedding visualization # with TensorBoard callback. We are unable to write a unit test for this # because TensorBoard dependency assumes TensorFlow package is installed. config = projector.ProjectorConfig() for layer_name, tensor in embeddings_vars.items(): embedding = config.embeddings.add() embedding.tensor_name = tensor.name if (embeddings_metadata is not None and layer_name in embeddings_metadata): embedding.metadata_path = embeddings_metadata[layer_name] projector.visualize_embeddings(self.writer, config)
Inherited members
class TerminateOnNaN
-
Callback that terminates training when a NaN loss is encountered.
Expand source code
class TerminateOnNaN(Callback): """Callback that terminates training when a NaN loss is encountered. """ def __init__(self): super(TerminateOnNaN, self).__init__() self._supports_tf_logs = True def on_batch_end(self, batch, logs=None): logs = logs or {} loss = logs.get('loss') if loss is not None: loss = tf_utils.sync_to_numpy_or_python_type(loss) if np.isnan(loss) or np.isinf(loss): print('Batch %d: Invalid loss, terminating training' % (batch)) self.model.stop_training = True
Ancestors
Inherited members