tf.keras.callbacks.Callback     View source on GitHub    Abstract base class used to build new callbacks.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.Callback
tf.keras.callbacks.Callback()
 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).

 Attributes
  params   Dict. Training parameters (eg. verbosity, batch size, number of epochs...).
  model   Instance of keras.models.Model. Reference of the model being trained.    Methods on_batch_begin View source
on_batch_begin(
    batch, logs=None
)
 A backwards compatibility alias for on_train_batch_begin. on_batch_end View source
on_batch_end(
    batch, logs=None
)
 A backwards compatibility alias for on_train_batch_end. on_epoch_begin View source
on_epoch_begin(
    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.

 Arguments
  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.    on_epoch_end View source
on_epoch_end(
    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.

 Arguments
  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, 'acc': 0.7}.    on_predict_batch_begin View source
on_predict_batch_begin(
    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.

 Arguments
  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.    on_predict_batch_end View source
on_predict_batch_end(
    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.

 Arguments
  batch   Integer, index of batch within the current epoch.
  logs   Dict. Aggregated metric results up until this batch.    on_predict_begin View source
on_predict_begin(
    logs=None
)
 Called at the beginning of prediction. Subclasses should override for any actions to run.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_predict_end View source
on_predict_end(
    logs=None
)
 Called at the end of prediction. Subclasses should override for any actions to run.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_test_batch_begin View source
on_test_batch_begin(
    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.

 Arguments
  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}.    on_test_batch_end View source
on_test_batch_end(
    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.

 Arguments
  batch   Integer, index of batch within the current epoch.
  logs   Dict. Aggregated metric results up until this batch.    on_test_begin View source
on_test_begin(
    logs=None
)
 Called at the beginning of evaluation or validation. Subclasses should override for any actions to run.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_test_end View source
on_test_end(
    logs=None
)
 Called at the end of evaluation or validation. Subclasses should override for any actions to run.

 Arguments
  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.    on_train_batch_begin View source
on_train_batch_begin(
    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.

 Arguments
  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}.    on_train_batch_end View source
on_train_batch_end(
    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.

 Arguments
  batch   Integer, index of batch within the current epoch.
  logs   Dict. Aggregated metric results up until this batch.    on_train_begin View source
on_train_begin(
    logs=None
)
 Called at the beginning of training. Subclasses should override for any actions to run.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_train_end View source
on_train_end(
    logs=None
)
 Called at the end of training. Subclasses should override for any actions to run.

 Arguments
  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.    set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.CallbackList Container abstracting a list of callbacks.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.CallbackList
tf.keras.callbacks.CallbackList(
    callbacks=None, add_history=False, add_progbar=False, model=None, **params
)

 Arguments
  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.    Methods append View source
append(
    callback
)
 on_batch_begin View source
on_batch_begin(
    batch, logs=None
)
 on_batch_end View source
on_batch_end(
    batch, logs=None
)
 on_epoch_begin View source
on_epoch_begin(
    epoch, logs=None
)
 Calls the on_epoch_begin methods of its callbacks. This function should only be called during TRAIN mode.

 Arguments
  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.    on_epoch_end View source
on_epoch_end(
    epoch, logs=None
)
 Calls the on_epoch_end methods of its callbacks. This function should only be called during TRAIN mode.

 Arguments
  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_.    on_predict_batch_begin View source
on_predict_batch_begin(
    batch, logs=None
)
 Calls the on_predict_batch_begin methods of its callbacks.

 Arguments
  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.    on_predict_batch_end View source
on_predict_batch_end(
    batch, logs=None
)
 Calls the on_predict_batch_end methods of its callbacks.

 Arguments
  batch   Integer, index of batch within the current epoch.
  logs   Dict. Aggregated metric results up until this batch.    on_predict_begin View source
on_predict_begin(
    logs=None
)
 Calls the 'on_predict_begin` methods of its callbacks.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_predict_end View source
on_predict_end(
    logs=None
)
 Calls the on_predict_end methods of its callbacks.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_test_batch_begin View source
on_test_batch_begin(
    batch, logs=None
)
 Calls the on_test_batch_begin methods of its callbacks.

 Arguments
  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}.    on_test_batch_end View source
on_test_batch_end(
    batch, logs=None
)
 Calls the on_test_batch_end methods of its callbacks.

 Arguments
  batch   Integer, index of batch within the current epoch.
  logs   Dict. Aggregated metric results up until this batch.    on_test_begin View source
on_test_begin(
    logs=None
)
 Calls the on_test_begin methods of its callbacks.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_test_end View source
on_test_end(
    logs=None
)
 Calls the on_test_end methods of its callbacks.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_train_batch_begin View source
on_train_batch_begin(
    batch, logs=None
)
 Calls the on_train_batch_begin methods of its callbacks.

 Arguments
  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}.    on_train_batch_end View source
on_train_batch_end(
    batch, logs=None
)
 Calls the on_train_batch_end methods of its callbacks.

 Arguments
  batch   Integer, index of batch within the current epoch.
  logs   Dict. Aggregated metric results up until this batch.    on_train_begin View source
on_train_begin(
    logs=None
)
 Calls the on_train_begin methods of its callbacks.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    on_train_end View source
on_train_end(
    logs=None
)
 Calls the on_train_end methods of its callbacks.

 Arguments
  logs   Dict. Currently no data is passed to this argument for this method but that may change in the future.    set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)
 __iter__ View source
__iter__()


tf.keras.callbacks.CSVLogger     View source on GitHub    Callback that streams epoch results to a CSV file. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.CSVLogger
tf.keras.callbacks.CSVLogger(
    filename, separator=',', append=False
)
 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])

 Arguments
  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.    Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.EarlyStopping     View source on GitHub    Stop training when a monitored metric has stopped improving. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.EarlyStopping
tf.keras.callbacks.EarlyStopping(
    monitor='val_loss', min_delta=0, patience=0, verbose=0,
    mode='auto', baseline=None, restore_best_weights=False
)
 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().

 Arguments
  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.    Example:
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)
# This callback will stop the training when there is no improvement in
# the validation 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
 Methods get_monitor_value View source
get_monitor_value(
    logs
)
 set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


Module: tf.keras.callbacks.experimental Public API for tf.keras.callbacks.experimental namespace. Classes class BackupAndRestore: Callback to back up and restore the training state.


tf.keras.callbacks.experimental.BackupAndRestore Callback to back up and restore the training state. Inherits From: Callback
tf.keras.callbacks.experimental.BackupAndRestore(
    backup_dir
)
 BackupAndRestore callback is intended to recover from interruptions that happened in the middle of a model.fit execution by backing up the training states in a temporary checkpoint file (based on TF CheckpointManager) at the end of each epoch. If training restarted before completion, the training state and model are restored to the most recently saved state at the beginning of a new model.fit() run. Note that user is responsible to bring jobs back up. This callback is important for the backup and restore mechanism for fault tolerance purpose. And the model to be restored from an previous checkpoint is expected to be the same as the one used to back up. If user changes arguments passed to compile or fit, the checkpoint saved for fault tolerance can become invalid. Note:  This callback is not compatible with disabling eager execution. A checkpoint is saved at the end of each epoch, when restoring we'll redo any partial work from an unfinished epoch in which the training got restarted (so the work done before a interruption doesn't affect the final model state). This works for both single worker and multi-worker mode, only MirroredStrategy and MultiWorkerMirroredStrategy are supported for now.  Example:
class InterruptingCallback(tf.keras.callbacks.Callback):
  def on_epoch_begin(self, epoch, logs=None):
    if epoch == 4:
      raise RuntimeError('Interrupting!')
callback = tf.keras.callbacks.experimental.BackupAndRestore(
backup_dir="/tmp")
model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)])
model.compile(tf.keras.optimizers.SGD(), loss='mse')
try:
  model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10,
            batch_size=1, callbacks=[callback, InterruptingCallback()],
            verbose=0)
except:
  pass
history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10,
            batch_size=1, callbacks=[callback], verbose=0)
# Only 6 more epochs are run, since first trainning got interrupted at
# zero-indexed epoch 4, second training will continue from 4 to 9.
len(history.history['loss'])
6

 Arguments
  backup_dir   String, path to save the model file. This is the directory in which the system stores temporary files to recover the model from jobs terminated unexpectedly. The directory cannot be reused elsewhere to store other checkpoints, e.g. by BackupAndRestore callback of another training, or by another callback (ModelCheckpoint) of the same training.    Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.History     View source on GitHub    Callback that records events into a History object. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.History
tf.keras.callbacks.History()
 This callback is automatically applied to every Keras model. The History object gets returned by the fit method of models. Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.LambdaCallback     View source on GitHub    Callback for creating simple, custom callbacks on-the-fly. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.LambdaCallback
tf.keras.callbacks.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
)
 This callback is constructed with anonymous functions that will be called at the appropriate time. 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

 Arguments
  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])
 Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.LearningRateScheduler     View source on GitHub    Learning rate scheduler. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.LearningRateScheduler
tf.keras.callbacks.LearningRateScheduler(
    schedule, verbose=0
)
 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.

 Arguments
  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
 Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.ModelCheckpoint     View source on GitHub    Callback to save the Keras model or model weights at some frequency. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.ModelCheckpoint
tf.keras.callbacks.ModelCheckpoint(
    filepath, monitor='val_loss', verbose=0, save_best_only=False,
    save_weights_only=False, mode='auto', save_freq='epoch',
    options=None, **kwargs
)
 ModelCheckpoint callback is used in conjunction with training using model.fit() to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved. A few options this callback provides include:  Whether to only keep the model that has achieved the "best performance" so far, or whether to save the model at the end of every epoch regardless of performance. Definition of 'best'; which quantity to monitor and whether it should be maximized or minimized. The frequency it should save at. Currently, the callback supports saving at the end of every epoch, or after a fixed number of training batches. Whether only weights are saved, or the whole model is saved.
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: 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)

 Arguments
  filepath   string or PathLike, path to save the model file. filepath can contain named formatting options, which will be filled the value of epoch and keys in logs (passed in on_epoch_end). For example: if filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5, then the model checkpoints will be saved with the epoch number and the validation loss in the filename.
  monitor   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 direction is automatically inferred from the name of the monitored quantity.
  save_weights_only   if True, then only the model's weights will be saved (model.save_weights(filepath)), else the full model is saved (model.save(filepath)).
  save_freq   'epoch' or integer. When using 'epoch', the callback saves the model after each epoch. When using integer, the callback saves the model at end of this many batches. If the Model is compiled with 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.    Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.ProgbarLogger     View source on GitHub    Callback that prints metrics to stdout. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.ProgbarLogger
tf.keras.callbacks.ProgbarLogger(
    count_mode='samples', stateful_metrics=None
)

 Arguments
  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.    Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.ReduceLROnPlateau     View source on GitHub    Reduce learning rate when a metric has stopped improving. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.ReduceLROnPlateau
tf.keras.callbacks.ReduceLROnPlateau(
    monitor='val_loss', factor=0.1, patience=10, verbose=0,
    mode='auto', min_delta=0.0001, cooldown=0, min_lr=0, **kwargs
)
 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])

 Arguments
  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.    Methods in_cooldown View source
in_cooldown()
 set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.RemoteMonitor     View source on GitHub    Callback used to stream events to a server. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.RemoteMonitor
tf.keras.callbacks.RemoteMonitor(
    root='http://localhost:9000', path='/publish/epoch/end/',
    field='data', headers=None, send_as_json=False
)
 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.

 Arguments
  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".    Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


tf.keras.callbacks.TensorBoard     View source on GitHub    Enable visualizations for TensorBoard. Inherits From: Callback
tf.keras.callbacks.TensorBoard(
    log_dir='logs', histogram_freq=0, write_graph=True,
    write_images=False, update_freq='epoch', profile_batch=2,
    embeddings_freq=0, embeddings_metadata=None, **kwargs
)
 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.

 Arguments
  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_images   whether to write model weights to visualize as image in TensorBoard.
  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 batches. Note that writing too frequently to TensorBoard can slow down your training.
  profile_batch   Profile the batch(es) to sample compute characteristics. profile_batch must be a non-negative integer or a tuple of integers. A pair of positive integers signify a range of batches to profile. By default, it will profile the second batch. Set profile_batch=0 to disable profiling.
  embeddings_freq   frequency (in epochs) at which embedding layers will be visualized. If set to 0, embeddings won't be visualized.
  embeddings_metadata   a dictionary which maps layer name to a file name in which metadata for this embedding layer is saved. See the details about metadata files format. In case if the same metadata file is used for all embedding layers, string can be passed.    Examples: Basic usage: tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./logs")
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])
# Then run the tensorboard command to view the visualizations.
 Custom batch-level summaries in a subclassed Model: class MyModel(tf.keras.Model):

  def build(self, _):
    self.dense = tf.keras.layers.Dense(10)

  def call(self, x):
    outputs = self.dense(x)
    tf.summary.histogram('outputs', outputs)
    return outputs

model = MyModel()
model.compile('sgd', 'mse')

# Make sure to set `update_freq=N` to log a batch-level summary every N batches.
# In addition to any `tf.summary` contained in `Model.call`, metrics added in
# `Model.compile` will be logged every N batches.
tb_callback = tf.keras.callbacks.TensorBoard('./logs', update_freq=1)
model.fit(x_train, y_train, callbacks=[tb_callback])
 Custom batch-level summaries in a Functional API Model: def my_summary(x):
  tf.summary.histogram('x', x)
  return x

inputs = tf.keras.Input(10)
x = tf.keras.layers.Dense(10)(inputs)
outputs = tf.keras.layers.Lambda(my_summary)(x)
model = tf.keras.Model(inputs, outputs)
model.compile('sgd', 'mse')

# Make sure to set `update_freq=N` to log a batch-level summary every N batches.
# In addition to any `tf.summary` contained in `Model.call`, metrics added in
# `Model.compile` will be logged every N batches.
tb_callback = tf.keras.callbacks.TensorBoard('./logs', update_freq=1)
model.fit(x_train, y_train, callbacks=[tb_callback])
 Profiling: # Profile a single batch, e.g. the 5th batch.
tensorboard_callback = tf.keras.callbacks.TensorBoard(
    log_dir='./logs', profile_batch=5)
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])

# Profile a range of batches, e.g. from 10 to 20.
tensorboard_callback = tf.keras.callbacks.TensorBoard(
    log_dir='./logs', profile_batch=(10,20))
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])
 Methods set_model View source
set_model(
    model
)
 Sets Keras model and writes graph if specified. set_params View source
set_params(
    params
)


tf.keras.callbacks.TerminateOnNaN     View source on GitHub    Callback that terminates training when a NaN loss is encountered. Inherits From: Callback  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.callbacks.TerminateOnNaN
tf.keras.callbacks.TerminateOnNaN()
 Methods set_model View source
set_model(
    model
)
 set_params View source
set_params(
    params
)


Module: tf.keras.constraints Constraints: functions that impose constraints on weight values. Classes class Constraint class MaxNorm: MaxNorm weight constraint. class MinMaxNorm: MinMaxNorm weight constraint. class NonNeg: Constrains the weights to be non-negative. class RadialConstraint: Constrains Conv2D kernel weights to be the same for each radius. class UnitNorm: Constrains the weights incident to each hidden unit to have unit norm. class max_norm: MaxNorm weight constraint. class min_max_norm: MinMaxNorm weight constraint. class non_neg: Constrains the weights to be non-negative. class radial_constraint: Constrains Conv2D kernel weights to be the same for each radius. class unit_norm: Constrains the weights incident to each hidden unit to have unit norm. Functions deserialize(...) get(...) serialize(...)


tf.keras.constraints.Constraint     View source on GitHub     View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.constraints.Constraint  Methods get_config View source
get_config()
 __call__ View source
__call__(
    w
)
 Call self as a function.


tf.keras.constraints.deserialize     View source on GitHub     View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.constraints.deserialize
tf.keras.constraints.deserialize(
    config, custom_objects=None
)


tf.keras.constraints.get     View source on GitHub     View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.constraints.get
tf.keras.constraints.get(
    identifier
)


tf.keras.constraints.MaxNorm     View source on GitHub    MaxNorm weight constraint. Inherits From: Constraint  View aliases  Main aliases
tf.keras.constraints.max_norm Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.constraints.MaxNorm, tf.compat.v1.keras.constraints.max_norm
tf.keras.constraints.MaxNorm(
    max_value=2, axis=0
)
 Constrains the weights incident to each hidden unit to have a norm less than or equal to a desired value. Also available via the shortcut function tf.keras.constraints.max_norm.

 Arguments
  max_value   the maximum norm value for the incoming weights.
  axis   integer, axis along which to calculate weight norms. For instance, in a Dense layer the weight matrix has shape (input_dim, output_dim), set axis to 0 to constrain each weight vector of length (input_dim,). In a Conv2D layer with data_format="channels_last", the weight tensor has shape (rows, cols, input_depth, output_depth), set axis to [0, 1, 2] to constrain the weights of each filter tensor of size (rows, cols, input_depth).


tf.keras.constraints.MinMaxNorm     View source on GitHub    MinMaxNorm weight constraint. Inherits From: Constraint  View aliases  Main aliases
tf.keras.constraints.min_max_norm Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.constraints.MinMaxNorm, tf.compat.v1.keras.constraints.min_max_norm
tf.keras.constraints.MinMaxNorm(
    min_value=0.0, max_value=1.0, rate=1.0, axis=0
)
 Constrains the weights incident to each hidden unit to have the norm between a lower bound and an upper bound. Also available via the shortcut function tf.keras.constraints.min_max_norm.

 Arguments
  min_value   the minimum norm for the incoming weights.
  max_value   the maximum norm for the incoming weights.
  rate   rate for enforcing the constraint: weights will be rescaled to yield (1 - rate) * norm + rate * norm.clip(min_value, max_value). Effectively, this means that rate=1.0 stands for strict enforcement of the constraint, while rate<1.0 means that weights will be rescaled at each step to slowly move towards a value inside the desired interval.
  axis   integer, axis along which to calculate weight norms. For instance, in a Dense layer the weight matrix has shape (input_dim, output_dim), set axis to 0 to constrain each weight vector of length (input_dim,). In a Conv2D layer with data_format="channels_last", the weight tensor has shape (rows, cols, input_depth, output_depth), set axis to [0, 1, 2] to constrain the weights of each filter tensor of size (rows, cols, input_depth).


tf.keras.constraints.NonNeg     View source on GitHub    Constrains the weights to be non-negative. Inherits From: Constraint  View aliases  Main aliases
tf.keras.constraints.non_neg Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.constraints.NonNeg, tf.compat.v1.keras.constraints.non_neg  Also available via the shortcut function tf.keras.constraints.non_neg. Methods get_config View source
get_config()
 __call__ View source
__call__(
    w
)
 Call self as a function.


tf.keras.constraints.RadialConstraint     View source on GitHub    Constrains Conv2D kernel weights to be the same for each radius. Inherits From: Constraint  View aliases  Main aliases
tf.keras.constraints.radial_constraint Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.constraints.RadialConstraint, tf.compat.v1.keras.constraints.radial_constraint  Also available via the shortcut function tf.keras.constraints.radial_constraint. For example, the desired output for the following 4-by-4 kernel: kernel = [[v_00, v_01, v_02, v_03],
          [v_10, v_11, v_12, v_13],
          [v_20, v_21, v_22, v_23],
          [v_30, v_31, v_32, v_33]]
 is this:: kernel = [[v_11, v_11, v_11, v_11],
          [v_11, v_33, v_33, v_11],
          [v_11, v_33, v_33, v_11],
          [v_11, v_11, v_11, v_11]]
 This constraint can be applied to any Conv2D layer version, including Conv2DTranspose and SeparableConv2D, and with either "channels_last" or "channels_first" data format. The method assumes the weight tensor is of shape (rows, cols, input_depth, output_depth). Methods get_config View source
get_config()


tf.keras.constraints.serialize     View source on GitHub     View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.constraints.serialize
tf.keras.constraints.serialize(
    constraint
)


tf.keras.constraints.UnitNorm     View source on GitHub    Constrains the weights incident to each hidden unit to have unit norm. Inherits From: Constraint  View aliases  Main aliases
tf.keras.constraints.unit_norm Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.constraints.UnitNorm, tf.compat.v1.keras.constraints.unit_norm
tf.keras.constraints.UnitNorm(
    axis=0
)
 Also available via the shortcut function tf.keras.constraints.unit_norm.

 Arguments
  axis   integer, axis along which to calculate weight norms. For instance, in a Dense layer the weight matrix has shape (input_dim, output_dim), set axis to 0 to constrain each weight vector of length (input_dim,). In a Conv2D layer with data_format="channels_last", the weight tensor has shape (rows, cols, input_depth, output_depth), set axis to [0, 1, 2] to constrain the weights of each filter tensor of size (rows, cols, input_depth).


Module: tf.keras.datasets Public API for tf.keras.datasets namespace. Modules boston_housing module: Boston housing price regression dataset. cifar10 module: CIFAR10 small images classification dataset. cifar100 module: CIFAR100 small images classification dataset. fashion_mnist module: Fashion-MNIST dataset. imdb module: IMDB sentiment classification dataset. mnist module: MNIST handwritten digits dataset. reuters module: Reuters topic classification dataset.


Module: tf.keras.datasets.boston_housing Boston housing price regression dataset. Functions load_data(...): Loads the Boston Housing dataset.


tf.keras.datasets.boston_housing.load_data       View source on GitHub    Loads the Boston Housing dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.boston_housing.load_data
tf.keras.datasets.boston_housing.load_data(
    path='boston_housing.npz', test_split=0.2, seed=113
)
 This is a dataset taken from the StatLib library which is maintained at Carnegie Mellon University. Samples contain 13 attributes of houses at different locations around the Boston suburbs in the late 1970s. Targets are the median values of the houses at a location (in k$). The attributes themselves are defined in the StatLib website.

 Arguments
  path   path where to cache the dataset locally (relative to ~/.keras/datasets).
  test_split   fraction of the data to reserve as test set.
  seed   Random seed for shuffling the data before computing the test split.

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: numpy arrays with shape (num_samples, 13) containing either the training samples (for x_train), or test samples (for y_train). y_train, y_test: numpy arrays of shape (num_samples,) containing the target scalars. The targets are float scalars typically between 10 and 50 that represent the home prices in k$.


Module: tf.keras.datasets.cifar10 CIFAR10 small images classification dataset. Functions load_data(...): Loads CIFAR10 dataset.


tf.keras.datasets.cifar10.load_data     View source on GitHub    Loads CIFAR10 dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.cifar10.load_data
tf.keras.datasets.cifar10.load_data()
 This is a dataset of 50,000 32x32 color training images and 10,000 test images, labeled over 10 categories. See more info at the CIFAR homepage.

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: uint8 arrays of RGB image data with shape (num_samples, 3, 32, 32) if tf.keras.backend.image_data_format() is 'channels_first', or (num_samples, 32, 32, 3) if the data format is 'channels_last'. y_train, y_test: uint8 arrays of category labels (integers in range 0-9) each with shape (num_samples, 1).


Module: tf.keras.datasets.cifar100 CIFAR100 small images classification dataset. Functions load_data(...): Loads CIFAR100 dataset.


tf.keras.datasets.cifar100.load_data     View source on GitHub    Loads CIFAR100 dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.cifar100.load_data
tf.keras.datasets.cifar100.load_data(
    label_mode='fine'
)
 This is a dataset of 50,000 32x32 color training images and 10,000 test images, labeled over 100 fine-grained classes that are grouped into 20 coarse-grained classes. See more info at the CIFAR homepage.

 Arguments
  label_mode   one of "fine", "coarse". If it is "fine" the category labels are the fine-grained labels, if it is "coarse" the output labels are the coarse-grained superclasses.

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: uint8 arrays of RGB image data with shape (num_samples, 3, 32, 32) if tf.keras.backend.image_data_format() is 'channels_first', or (num_samples, 32, 32, 3) if the data format is 'channels_last'. y_train, y_test: uint8 arrays of category labels with shape (num_samples, 1).

 Raises
  ValueError   in case of invalid label_mode.


Module: tf.keras.datasets.fashion_mnist Fashion-MNIST dataset. Functions load_data(...): Loads the Fashion-MNIST dataset.


tf.keras.datasets.fashion_mnist.load_data     View source on GitHub    Loads the Fashion-MNIST dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.fashion_mnist.load_data
tf.keras.datasets.fashion_mnist.load_data()
 This is a dataset of 60,000 28x28 grayscale images of 10 fashion categories, along with a test set of 10,000 images. This dataset can be used as a drop-in replacement for MNIST. The class labels are:    Label Description     0 T-shirt/top   1 Trouser   2 Pullover   3 Dress   4 Coat   5 Sandal   6 Shirt   7 Sneaker   8 Bag   9 Ankle boot

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: uint8 arrays of grayscale image data with shape (num_samples, 28, 28). y_train, y_test: uint8 arrays of labels (integers in range 0-9) with shape (num_samples,).

 License: The copyright for Fashion-MNIST is held by Zalando SE. Fashion-MNIST is licensed under the MIT license.


Module: tf.keras.datasets.imdb IMDB sentiment classification dataset. Functions get_word_index(...): Retrieves a dict mapping words to their index in the IMDB dataset. load_data(...): Loads the IMDB dataset.


tf.keras.datasets.imdb.get_word_index     View source on GitHub    Retrieves a dict mapping words to their index in the IMDB dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.imdb.get_word_index
tf.keras.datasets.imdb.get_word_index(
    path='imdb_word_index.json'
)

 Arguments
  path   where to cache the data (relative to ~/.keras/dataset).

 Returns   The word index dictionary. Keys are word strings, values are their index.


tf.keras.datasets.imdb.load_data     View source on GitHub    Loads the IMDB dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.imdb.load_data
tf.keras.datasets.imdb.load_data(
    path='imdb.npz', num_words=None, skip_top=0, maxlen=None, seed=113,
    start_char=1, oov_char=2, index_from=3, **kwargs
)
 This is a dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a list of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words". As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.

 Arguments
  path   where to cache the data (relative to ~/.keras/dataset).
  num_words   integer or None. Words are ranked by how often they occur (in the training set) and only the num_words most frequent words are kept. Any less frequent word will appear as oov_char value in the sequence data. If None, all words are kept. Defaults to None, so all words are kept.
  skip_top   skip the top N most frequently occurring words (which may not be informative). These words will appear as oov_char value in the dataset. Defaults to 0, so no words are skipped.
  maxlen   int or None. Maximum sequence length. Any longer sequence will be truncated. Defaults to None, which means no truncation.
  seed   int. Seed for reproducible data shuffling.
  start_char   int. The start of a sequence will be marked with this character. Defaults to 1 because 0 is usually the padding character.
  oov_char   int. The out-of-vocabulary character. Words that were cut out because of the num_words or skip_top limits will be replaced with this character.
  index_from   int. Index actual words with this index and higher.
  **kwargs   Used for backwards compatibility.

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: lists of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is num_words - 1. If the maxlen argument was specified, the largest possible sequence length is maxlen. y_train, y_test: lists of integer labels (1 or 0).

 Raises
  ValueError   in case maxlen is so low that no input sequence could be kept.    Note that the 'out of vocabulary' character is only used for words that were present in the training set but are not included because they're not making the num_words cut here. Words that were not seen in the training set but are in the test set have simply been skipped.


Module: tf.keras.datasets.mnist MNIST handwritten digits dataset. Functions load_data(...): Loads the MNIST dataset.


tf.keras.datasets.mnist.load_data     View source on GitHub    Loads the MNIST dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.mnist.load_data
tf.keras.datasets.mnist.load_data(
    path='mnist.npz'
)
 This is a dataset of 60,000 28x28 grayscale images of the 10 digits, along with a test set of 10,000 images. More info can be found at the MNIST homepage.

 Arguments
  path   path where to cache the dataset locally (relative to ~/.keras/datasets).

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: uint8 arrays of grayscale image data with shapes (num_samples, 28, 28). y_train, y_test: uint8 arrays of digit labels (integers in range 0-9) with shapes (num_samples,).

 License: Yann LeCun and Corinna Cortes hold the copyright of MNIST dataset, which is a derivative work from original NIST datasets. MNIST dataset is made available under the terms of the Creative Commons Attribution-Share Alike 3.0 license.


Module: tf.keras.datasets.reuters Reuters topic classification dataset. Functions get_word_index(...): Retrieves a dict mapping words to their index in the Reuters dataset. load_data(...): Loads the Reuters newswire classification dataset.


tf.keras.datasets.reuters.get_word_index     View source on GitHub    Retrieves a dict mapping words to their index in the Reuters dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.reuters.get_word_index
tf.keras.datasets.reuters.get_word_index(
    path='reuters_word_index.json'
)

 Arguments
  path   where to cache the data (relative to ~/.keras/dataset).

 Returns   The word index dictionary. Keys are word strings, values are their index.


tf.keras.datasets.reuters.load_data     View source on GitHub    Loads the Reuters newswire classification dataset.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.datasets.reuters.load_data
tf.keras.datasets.reuters.load_data(
    path='reuters.npz', num_words=None, skip_top=0, maxlen=None,
    test_split=0.2, seed=113, start_char=1, oov_char=2, index_from=3, **kwargs
)
 This is a dataset of 11,228 newswires from Reuters, labeled over 46 topics. This was originally generated by parsing and preprocessing the classic Reuters-21578 dataset, but the preprocessing code is no longer packaged with Keras. See this github discussion for more info. Each newswire is encoded as a list of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words". As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.

 Arguments
  path   where to cache the data (relative to ~/.keras/dataset).
  num_words   integer or None. Words are ranked by how often they occur (in the training set) and only the num_words most frequent words are kept. Any less frequent word will appear as oov_char value in the sequence data. If None, all words are kept. Defaults to None, so all words are kept.
  skip_top   skip the top N most frequently occurring words (which may not be informative). These words will appear as oov_char value in the dataset. Defaults to 0, so no words are skipped.
  maxlen   int or None. Maximum sequence length. Any longer sequence will be truncated. Defaults to None, which means no truncation.
  test_split   Float between 0 and 1. Fraction of the dataset to be used as test data. Defaults to 0.2, meaning 20% of the dataset is used as test data.
  seed   int. Seed for reproducible data shuffling.
  start_char   int. The start of a sequence will be marked with this character. Defaults to 1 because 0 is usually the padding character.
  oov_char   int. The out-of-vocabulary character. Words that were cut out because of the num_words or skip_top limits will be replaced with this character.
  index_from   int. Index actual words with this index and higher.
  **kwargs   Used for backwards compatibility.

 Returns   Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test). x_train, x_test: lists of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is num_words - 1. If the maxlen argument was specified, the largest possible sequence length is maxlen. y_train, y_test: lists of integer labels (1 or 0).

Note: The 'out of vocabulary' character is only used for words that were present in the training set but are not included because they're not making the num_words cut here. Words that were not seen in the training set but are in the test set have simply been skipped.


Module: tf.keras.estimator Keras estimator API. Functions model_to_estimator(...): Constructs an Estimator instance from given keras model.


tf.keras.estimator.model_to_estimator     View source on GitHub    Constructs an Estimator instance from given keras model.
tf.keras.estimator.model_to_estimator(
    keras_model=None, keras_model_path=None, custom_objects=None, model_dir=None,
    config=None, checkpoint_format='checkpoint', metric_names_map=None,
    export_outputs=None
)
 If you use infrastructure or other tooling that relies on Estimators, you can still build a Keras model and use model_to_estimator to convert the Keras model to an Estimator for use with downstream systems. For usage example, please see: Creating estimators from Keras Models. Sample Weights: Estimators returned by model_to_estimator are configured so that they can handle sample weights (similar to keras_model.fit(x, y, sample_weights)). To pass sample weights when training or evaluating the Estimator, the first item returned by the input function should be a dictionary with keys features and sample_weights. Example below: keras_model = tf.keras.Model(...)
keras_model.compile(...)

estimator = tf.keras.estimator.model_to_estimator(keras_model)

def input_fn():
  return dataset_ops.Dataset.from_tensors(
      ({'features': features, 'sample_weights': sample_weights},
       targets))

estimator.train(input_fn, steps=1)
 Example with customized export signature: inputs = {'a': tf.keras.Input(..., name='a'),
          'b': tf.keras.Input(..., name='b')}
outputs = {'c': tf.keras.layers.Dense(..., name='c')(inputs['a']),
           'd': tf.keras.layers.Dense(..., name='d')(inputs['b'])}
keras_model = tf.keras.Model(inputs, outputs)
keras_model.compile(...)
export_outputs = {'c': tf.estimator.export.RegressionOutput,
                  'd': tf.estimator.export.ClassificationOutput}

estimator = tf.keras.estimator.model_to_estimator(
    keras_model, export_outputs=export_outputs)

def input_fn():
  return dataset_ops.Dataset.from_tensors(
      ({'features': features, 'sample_weights': sample_weights},
       targets))

estimator.train(input_fn, steps=1)

Note: We do not support creating weighted metrics in Keras and converting them to weighted metrics in the Estimator API using model_to_estimator. You will have to create these metrics directly on the estimator spec using the add_metrics function.
 To customize the estimator eval_metric_ops names, you can pass in the metric_names_map dictionary mapping the keras model output metric names to the custom names as follows: input_a = tf.keras.layers.Input(shape=(16,), name='input_a')
input_b = tf.keras.layers.Input(shape=(16,), name='input_b')
dense = tf.keras.layers.Dense(8, name='dense_1')
interm_a = dense(input_a)
interm_b = dense(input_b)
merged = tf.keras.layers.concatenate([interm_a, interm_b], name='merge')
output_a = tf.keras.layers.Dense(3, activation='softmax', name='dense_2')(
        merged)
output_b = tf.keras.layers.Dense(2, activation='softmax', name='dense_3')(
        merged)
keras_model = tf.keras.models.Model(
    inputs=[input_a, input_b], outputs=[output_a, output_b])
keras_model.compile(
    loss='categorical_crossentropy',
    optimizer='rmsprop',
    metrics={
        'dense_2': 'categorical_accuracy',
        'dense_3': 'categorical_accuracy'
    })

metric_names_map = {
    'dense_2_categorical_accuracy': 'acc_1',
    'dense_3_categorical_accuracy': 'acc_2',
}
keras_est = tf.keras.estimator.model_to_estimator(
    keras_model=keras_model,
    config=config,
    metric_names_map=metric_names_map)

 Args
  keras_model   A compiled Keras model object. This argument is mutually exclusive with keras_model_path. Estimator's model_fn uses the structure of the model to clone the model. Defaults to None.
  keras_model_path   Path to a compiled Keras model saved on disk, in HDF5 format, which can be generated with the save() method of a Keras model. This argument is mutually exclusive with keras_model. Defaults to None.
  custom_objects   Dictionary for cloning customized objects. This is used with classes that is not part of this pip package. For example, if user maintains a relu6 class that inherits from tf.keras.layers.Layer, then pass custom_objects={'relu6': relu6}. Defaults to None.
  model_dir   Directory to save Estimator model parameters, graph, summary files for TensorBoard, etc. If unset a directory will be created with tempfile.mkdtemp
  config   RunConfig to config Estimator. Allows setting up things in model_fn based on configuration such as num_ps_replicas, or model_dir. Defaults to None. If both config.model_dir and the model_dir argument (above) are specified the model_dir argument takes precedence.
  checkpoint_format   Sets the format of the checkpoint saved by the estimator when training. May be saver or checkpoint, depending on whether to save checkpoints from tf.compat.v1.train.Saver or tf.train.Checkpoint. The default is checkpoint. Estimators use name-based tf.train.Saver checkpoints, while Keras models use object-based checkpoints from tf.train.Checkpoint. Currently, saving object-based checkpoints from model_to_estimator is only supported by Functional and Sequential models. Defaults to 'checkpoint'.
  metric_names_map   Optional dictionary mapping Keras model output metric names to custom names. This can be used to override the default Keras model output metrics names in a multi IO model use case and provide custom names for the eval_metric_ops in Estimator. The Keras model metric names can be obtained using model.metrics_names excluding any loss metrics such as total loss and output losses. For example, if your Keras model has two outputs out_1 and out_2, with mse loss and acc metric, then model.metrics_names will be ['loss', 'out_1_loss', 'out_2_loss', 'out_1_acc', 'out_2_acc']. The model metric names excluding the loss metrics will be ['out_1_acc', 'out_2_acc'].
  export_outputs   Optional dictionary. This can be used to override the default Keras model output exports in a multi IO model use case and provide custom names for the export_outputs in tf.estimator.EstimatorSpec. Default is None, which is equivalent to {'serving_default': tf.estimator.export.PredictOutput}. If not None, the keys must match the keys of model.output_names. A dict {name: output} where:  name: An arbitrary name for this output. output: an ExportOutput class such as ClassificationOutput, RegressionOutput, or PredictOutput. Single-headed models only need to specify one entry in this dictionary. Multi-headed models should specify one entry for each head, one of which must be named using tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY If no entry is provided, a default PredictOutput mapping to predictions will be created.

 Returns   An Estimator from given keras model.

 Raises
  ValueError   If neither keras_model nor keras_model_path was given.
  ValueError   If both keras_model and keras_model_path was given.
  ValueError   If the keras_model_path is a GCS URI.
  ValueError   If keras_model has not been compiled.
  ValueError   If an invalid checkpoint_format was given.


Module: tf.keras.experimental Public API for tf.keras.experimental namespace. Classes class CosineDecay: A LearningRateSchedule that uses a cosine decay schedule. class CosineDecayRestarts: A LearningRateSchedule that uses a cosine decay schedule with restarts. class LinearCosineDecay: A LearningRateSchedule that uses a linear cosine decay schedule. class LinearModel: Linear Model for regression and classification problems. class NoisyLinearCosineDecay: A LearningRateSchedule that uses a noisy linear cosine decay schedule. class PeepholeLSTMCell: Equivalent to LSTMCell class but adds peephole connections. class SequenceFeatures: A layer for sequence input. class WideDeepModel: Wide & Deep Model for regression and classification problems.


tf.keras.experimental.CosineDecay     View source on GitHub    A LearningRateSchedule that uses a cosine decay schedule. Inherits From: LearningRateSchedule  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.experimental.CosineDecay
tf.keras.experimental.CosineDecay(
    initial_learning_rate, decay_steps, alpha=0.0, name=None
)
 See [Loshchilov & Hutter, ICLR2016], SGDR: Stochastic Gradient Descent with Warm Restarts. https://arxiv.org/abs/1608.03983 When training a model, it is often recommended to lower the learning rate as the training progresses. This schedule applies a cosine decay function to an optimizer step, given a provided initial learning rate. It requires a step value to compute the decayed learning rate. You can just pass a TensorFlow variable that you increment at each training step. The schedule a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as: def decayed_learning_rate(step):
  step = min(step, decay_steps)
  cosine_decay = 0.5 * (1 + cos(pi * step / decay_steps))
  decayed = (1 - alpha) * cosine_decay + alpha
  return initial_learning_rate * decayed
 Example usage: decay_steps = 1000
lr_decayed_fn = tf.keras.experimental.CosineDecay(
    initial_learning_rate, decay_steps)
 You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. The learning rate schedule is also serializable and deserializable using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.

 Returns   A 1-arg callable learning rate schedule that takes the current optimizer step and outputs the decayed learning rate, a scalar Tensor of the same type as initial_learning_rate.

 Args
  initial_learning_rate   A scalar float32 or float64 Tensor or a Python number. The initial learning rate.
  decay_steps   A scalar int32 or int64 Tensor or a Python number. Number of steps to decay over.
  alpha   A scalar float32 or float64 Tensor or a Python number. Minimum learning rate value as a fraction of initial_learning_rate.
  name   String. Optional name of the operation. Defaults to 'CosineDecay'.    Methods from_config View source
@classmethod
from_config(
    config
)
 Instantiates a LearningRateSchedule from its config.

 Args
  config   Output of get_config().

 Returns   A LearningRateSchedule instance.
 get_config View source
get_config()
 __call__ View source
__call__(
    step
)
 Call self as a function.


tf.keras.experimental.CosineDecayRestarts     View source on GitHub    A LearningRateSchedule that uses a cosine decay schedule with restarts. Inherits From: LearningRateSchedule  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.experimental.CosineDecayRestarts
tf.keras.experimental.CosineDecayRestarts(
    initial_learning_rate, first_decay_steps, t_mul=2.0, m_mul=1.0, alpha=0.0,
    name=None
)
 See [Loshchilov & Hutter, ICLR2016], SGDR: Stochastic Gradient Descent with Warm Restarts. https://arxiv.org/abs/1608.03983 When training a model, it is often recommended to lower the learning rate as the training progresses. This schedule applies a cosine decay function with restarts to an optimizer step, given a provided initial learning rate. It requires a step value to compute the decayed learning rate. You can just pass a TensorFlow variable that you increment at each training step. The schedule a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. The learning rate multiplier first decays from 1 to alpha for first_decay_steps steps. Then, a warm restart is performed. Each new warm restart runs for t_mul times more steps and with m_mul times smaller initial learning rate. Example usage: first_decay_steps = 1000
lr_decayed_fn = (
  tf.keras.experimental.CosineDecayRestarts(
      initial_learning_rate,
      first_decay_steps))
 You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. The learning rate schedule is also serializable and deserializable using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.

 Returns   A 1-arg callable learning rate schedule that takes the current optimizer step and outputs the decayed learning rate, a scalar Tensor of the same type as initial_learning_rate.

 Args
  initial_learning_rate   A scalar float32 or float64 Tensor or a Python number. The initial learning rate.
  first_decay_steps   A scalar int32 or int64 Tensor or a Python number. Number of steps to decay over.
  t_mul   A scalar float32 or float64 Tensor or a Python number. Used to derive the number of iterations in the i-th period
  m_mul   A scalar float32 or float64 Tensor or a Python number. Used to derive the initial learning rate of the i-th period:
  alpha   A scalar float32 or float64 Tensor or a Python number. Minimum learning rate value as a fraction of the initial_learning_rate.
  name   String. Optional name of the operation. Defaults to 'SGDRDecay'.    Methods from_config View source
@classmethod
from_config(
    config
)
 Instantiates a LearningRateSchedule from its config.

 Args
  config   Output of get_config().

 Returns   A LearningRateSchedule instance.
 get_config View source
get_config()
 __call__ View source
__call__(
    step
)
 Call self as a function.


tf.keras.experimental.LinearCosineDecay     View source on GitHub    A LearningRateSchedule that uses a linear cosine decay schedule. Inherits From: LearningRateSchedule  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.experimental.LinearCosineDecay
tf.keras.experimental.LinearCosineDecay(
    initial_learning_rate, decay_steps, num_periods=0.5, alpha=0.0, beta=0.001,
    name=None
)
 See [Bello et al., ICML2017] Neural Optimizer Search with RL. https://arxiv.org/abs/1709.07417 For the idea of warm starts here controlled by num_periods, see [Loshchilov & Hutter, ICLR2016] SGDR: Stochastic Gradient Descent with Warm Restarts. https://arxiv.org/abs/1608.03983 Note that linear cosine decay is more aggressive than cosine decay and larger initial learning rates can typically be used. When training a model, it is often recommended to lower the learning rate as the training progresses. This schedule applies a linear cosine decay function to an optimizer step, given a provided initial learning rate. It requires a step value to compute the decayed learning rate. You can just pass a TensorFlow variable that you increment at each training step. The schedule a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as: def decayed_learning_rate(step):
  step = min(step, decay_steps)
  linear_decay = (decay_steps - step) / decay_steps
  cosine_decay = 0.5 * (
      1 + cos(pi * 2 * num_periods * step / decay_steps))
  decayed = (alpha + linear_decay) * cosine_decay + beta
  return initial_learning_rate * decayed
 Example usage: decay_steps = 1000
lr_decayed_fn = (
  tf.keras.experimental.LinearCosineDecay(
    initial_learning_rate, decay_steps))
 You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. The learning rate schedule is also serializable and deserializable using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.

 Returns   A 1-arg callable learning rate schedule that takes the current optimizer step and outputs the decayed learning rate, a scalar Tensor of the same type as initial_learning_rate.

 Args
  initial_learning_rate   A scalar float32 or float64 Tensor or a Python number. The initial learning rate.
  decay_steps   A scalar int32 or int64 Tensor or a Python number. Number of steps to decay over.
  num_periods   Number of periods in the cosine part of the decay. See computation above.
  alpha   See computation above.
  beta   See computation above.
  name   String. Optional name of the operation. Defaults to 'LinearCosineDecay'.    Methods from_config View source
@classmethod
from_config(
    config
)
 Instantiates a LearningRateSchedule from its config.

 Args
  config   Output of get_config().

 Returns   A LearningRateSchedule instance.
 get_config View source
get_config()
 __call__ View source
__call__(
    step
)
 Call self as a function.