This wrapper allows to apply a layer to every temporal slice of an input.
Inherits From: Wrapper, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.TimeDistributed
The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.
Consider a batch of 32 video samples, where each sample is a 128x128 RGB image with channels_last data format, across 10 timesteps. The batch input shape is (32, 10, 128, 128, 3).
You can then use TimeDistributed to apply the same Conv2D layer to each of the 10 timesteps, independently:
Because TimeDistributed applies the same instance of Conv2D to each of the timestamps, the same set of weights are used at each timestamp.


Upsampling layer for 1D inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.UpSampling1D
Repeats each temporal step size times along the time axis.
3D tensor with shape: (batch_size, steps, features).
3D tensor with shape: (batch_size, upsampled_steps, features).


Upsampling layer for 2D inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.UpSampling2D
Repeats the rows and columns of the data by size[0] and size[1] respectively.
4D tensor with shape:
4D tensor with shape:


Upsampling layer for 3D inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.UpSampling3D
Repeats the 1st, 2nd and 3rd dimensions of the data by size[0], size[1] and size[2] respectively.
5D tensor with shape:
5D tensor with shape:


Abstract wrapper base class.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Wrapper
Wrappers take another layer and augment it in various ways. Do not use this class as a layer, it is only an abstract base class. Two usable wrappers are the TimeDistributed and Bidirectional wrappers.


Zero-padding layer for 1D input (e.g. temporal sequence).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ZeroPadding1D
3D tensor with shape (batch_size, axis_to_pad, features)
3D tensor with shape (batch_size, padded_axis, features)


Zero-padding layer for 2D input (e.g. picture).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ZeroPadding2D
This layer can add rows and columns of zeros at the top, bottom, left and right side of an image tensor.
4D tensor with shape:
4D tensor with shape:


Zero-padding layer for 3D data (spatial or spatio-temporal).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ZeroPadding3D
5D tensor with shape:
5D tensor with shape:


Built-in loss functions.
Main aliases
tf.losses
class BinaryCrossentropy: Computes the cross-entropy loss between true labels and predicted labels.
class CategoricalCrossentropy: Computes the crossentropy loss between the labels and predictions.
class CategoricalHinge: Computes the categorical hinge loss between y_true and y_pred.
class CosineSimilarity: Computes the cosine similarity between labels and predictions.
class Hinge: Computes the hinge loss between y_true and y_pred.
class Huber: Computes the Huber loss between y_true and y_pred.
class KLDivergence: Computes Kullback-Leibler divergence loss between y_true and y_pred.
class LogCosh: Computes the logarithm of the hyperbolic cosine of the prediction error.
class Loss: Loss base class.
class MeanAbsoluteError: Computes the mean of absolute difference between labels and predictions.
class MeanAbsolutePercentageError: Computes the mean absolute percentage error between y_true and y_pred.
class MeanSquaredError: Computes the mean of squares of errors between labels and predictions.
class MeanSquaredLogarithmicError: Computes the mean squared logarithmic error between y_true and y_pred.
class Poisson: Computes the Poisson loss between y_true and y_pred.
class Reduction: Types of loss reduction.
class SparseCategoricalCrossentropy: Computes the crossentropy loss between the labels and predictions.
class SquaredHinge: Computes the squared hinge loss between y_true and y_pred.
KLD(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
MAE(...): Computes the mean absolute error between labels and predictions.
MAPE(...): Computes the mean absolute percentage error between y_true and y_pred.
MSE(...): Computes the mean squared error between labels and predictions.
MSLE(...): Computes the mean squared logarithmic error between y_true and y_pred.
binary_crossentropy(...): Computes the binary crossentropy loss.
categorical_crossentropy(...): Computes the categorical crossentropy loss.
categorical_hinge(...): Computes the categorical hinge loss between y_true and y_pred.
cosine_similarity(...): Computes the cosine similarity between labels and predictions.
deserialize(...): Deserializes a serialized loss class/function instance.
get(...): Retrieves a Keras loss as a function/Loss class instance.
hinge(...): Computes the hinge loss between y_true and y_pred.
huber(...): Computes Huber loss value.
kl_divergence(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
kld(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
kullback_leibler_divergence(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
log_cosh(...): Logarithm of the hyperbolic cosine of the prediction error.
logcosh(...): Logarithm of the hyperbolic cosine of the prediction error.
mae(...): Computes the mean absolute error between labels and predictions.
mape(...): Computes the mean absolute percentage error between y_true and y_pred.
mean_absolute_error(...): Computes the mean absolute error between labels and predictions.
mean_absolute_percentage_error(...): Computes the mean absolute percentage error between y_true and y_pred.
mean_squared_error(...): Computes the mean squared error between labels and predictions.
mean_squared_logarithmic_error(...): Computes the mean squared logarithmic error between y_true and y_pred.
mse(...): Computes the mean squared error between labels and predictions.
msle(...): Computes the mean squared logarithmic error between y_true and y_pred.
poisson(...): Computes the Poisson loss between y_true and y_pred.
serialize(...): Serializes loss function or Loss instance.
sparse_categorical_crossentropy(...): Computes the sparse categorical crossentropy loss.
squared_hinge(...): Computes the squared hinge loss between y_true and y_pred.


Computes the cross-entropy loss between true labels and predicted labels.
Inherits From: Loss
Main aliases
tf.losses.BinaryCrossentropy
See Migration guide for more details.
tf.compat.v1.keras.losses.BinaryCrossentropy
Use this cross-entropy loss when there are only two label classes (assumed to be 0 and 1). For each example, there should be a single floating-point value per prediction.
In the snippet below, each of the four examples has only a single floating-pointing value, and both y_pred and y_true have the shape [batch_size].
Usage with the tf.keras API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the binary crossentropy loss.
Main aliases
tf.keras.metrics.binary_crossentropy, tf.losses.binary_crossentropy, tf.metrics.binary_crossentropy
See Migration guide for more details.
tf.compat.v1.keras.losses.binary_crossentropy, tf.compat.v1.keras.metrics.binary_crossentropy


Computes the crossentropy loss between the labels and predictions.
Inherits From: Loss
Main aliases
tf.losses.CategoricalCrossentropy
See Migration guide for more details.
tf.compat.v1.keras.losses.CategoricalCrossentropy
Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided in a one_hot representation. If you want to provide labels as integers, please use SparseCategoricalCrossentropy loss. There should be # classes floating point values per feature.
In the snippet below, there is # classes floating pointing values per example. The shape of both y_pred and y_true are [batch_size, num_classes].
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the categorical hinge loss between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.CategoricalHinge
See Migration guide for more details.
tf.compat.v1.keras.losses.CategoricalHinge
loss = maximum(neg - pos + 1, 0) where neg=maximum((1-y_true)*y_pred) and pos=sum(y_true*y_pred)
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the categorical crossentropy loss.
Main aliases
tf.keras.metrics.categorical_crossentropy, tf.losses.categorical_crossentropy, tf.metrics.categorical_crossentropy
See Migration guide for more details.
tf.compat.v1.keras.losses.categorical_crossentropy, tf.compat.v1.keras.metrics.categorical_crossentropy


Computes the categorical hinge loss between y_true and y_pred.
Main aliases
tf.losses.categorical_hinge
See Migration guide for more details.
tf.compat.v1.keras.losses.categorical_hinge
loss = maximum(neg - pos + 1, 0) where neg=maximum((1-y_true)*y_pred) and pos=sum(y_true*y_pred)


Computes the cosine similarity between labels and predictions.
Inherits From: Loss
Main aliases
tf.losses.CosineSimilarity
See Migration guide for more details.
tf.compat.v1.keras.losses.CosineSimilarity
Note that it is a number between -1 and 1. When it is a negative number between -1 and 0, 0 indicates orthogonality and values closer to -1 indicate greater similarity. The values closer to 1 indicate greater dissimilarity. This makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets. If either y_true or y_pred is a zero vector, cosine similarity will be 0 regardless of the proximity between predictions and targets.
loss = -sum(l2_norm(y_true) * l2_norm(y_pred))
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the cosine similarity between labels and predictions.
Main aliases
tf.losses.cosine_similarity
See Migration guide for more details.
tf.compat.v1.keras.losses.cosine, tf.compat.v1.keras.losses.cosine_proximity, tf.compat.v1.keras.losses.cosine_similarity, tf.compat.v1.keras.metrics.cosine, tf.compat.v1.keras.metrics.cosine_proximity
Note that it is a number between -1 and 1. When it is a negative number between -1 and 0, 0 indicates orthogonality and values closer to -1 indicate greater similarity. The values closer to 1 indicate greater dissimilarity. This makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets. If either y_true or y_pred is a zero vector, cosine similarity will be 0 regardless of the proximity between predictions and targets.
loss = -sum(l2_norm(y_true) * l2_norm(y_pred))


Deserializes a serialized loss class/function instance.
Main aliases
tf.losses.deserialize
See Migration guide for more details.
tf.compat.v1.keras.losses.deserialize


Retrieves a Keras loss as a function/Loss class instance.
Main aliases
tf.losses.get
See Migration guide for more details.
tf.compat.v1.keras.losses.get
The identifier may be the string name of a loss function or Loss class.
You can also specify config of the loss to this function by passing dict containing class_name and config as an identifier. Also note that the class_name must map to a Loss class


Computes the hinge loss between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.Hinge
See Migration guide for more details.
tf.compat.v1.keras.losses.Hinge
loss = maximum(1 - y_true * y_pred, 0)
y_true values are expected to be -1 or 1. If binary (0 or 1) labels are provided we will convert them to -1 or 1.
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the Huber loss between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.Huber
See Migration guide for more details.
tf.compat.v1.keras.losses.Huber
For each value x in error = y_true - y_pred:
where d is delta. See: https://en.wikipedia.org/wiki/Huber_loss
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes Kullback-Leibler divergence loss between y_true and y_pred.
Main aliases
tf.keras.losses.kl_divergence, tf.keras.losses.kld, tf.keras.losses.kullback_leibler_divergence, tf.keras.metrics.KLD, tf.keras.metrics.kl_divergence, tf.keras.metrics.kld, tf.keras.metrics.kullback_leibler_divergence, tf.losses.KLD, tf.losses.kl_divergence, tf.losses.kld, tf.losses.kullback_leibler_divergence, tf.metrics.KLD, tf.metrics.kl_divergence, tf.metrics.kld, tf.metrics.kullback_leibler_divergence
See Migration guide for more details.
tf.compat.v1.keras.losses.KLD, tf.compat.v1.keras.losses.kl_divergence, tf.compat.v1.keras.losses.kld, tf.compat.v1.keras.losses.kullback_leibler_divergence, tf.compat.v1.keras.metrics.KLD, tf.compat.v1.keras.metrics.kl_divergence, tf.compat.v1.keras.metrics.kld, tf.compat.v1.keras.metrics.kullback_leibler_divergence
loss = y_true * log(y_true / y_pred)
See: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence


Computes Kullback-Leibler divergence loss between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.KLDivergence
See Migration guide for more details.
tf.compat.v1.keras.losses.KLDivergence
loss = y_true * log(y_true / y_pred)
See: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the logarithm of the hyperbolic cosine of the prediction error.
Inherits From: Loss
Main aliases
tf.losses.LogCosh
See Migration guide for more details.
tf.compat.v1.keras.losses.LogCosh
logcosh = log((exp(x) + exp(-x))/2), where x is the error y_pred - y_true.
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Logarithm of the hyperbolic cosine of the prediction error.
Main aliases
tf.keras.losses.logcosh, tf.keras.metrics.log_cosh, tf.keras.metrics.logcosh, tf.losses.log_cosh, tf.losses.logcosh, tf.metrics.log_cosh, tf.metrics.logcosh
See Migration guide for more details.
tf.compat.v1.keras.losses.log_cosh, tf.compat.v1.keras.losses.logcosh, tf.compat.v1.keras.metrics.log_cosh, tf.compat.v1.keras.metrics.logcosh
log(cosh(x)) is approximately equal to (x ** 2) / 2 for small x and to abs(x) - log(2) for large x. This means that 'logcosh' works mostly like the mean squared error, but will not be so strongly affected by the occasional wildly incorrect prediction.


Loss base class.
Main aliases
tf.losses.Loss
See Migration guide for more details.
tf.compat.v1.keras.losses.Loss
To be implemented by subclasses:
Example subclass implementation:
When used with tf.distribute.Strategy, outside of built-in training loops such as tf.keras compile and fit, please use 'SUM' or 'NONE' reduction types, and reduce losses explicitly in your training loop. Using 'AUTO' or 'SUM_OVER_BATCH_SIZE' will raise an error.
Please see this custom training tutorial for more details on this.
You can implement 'SUM_OVER_BATCH_SIZE' using global batch size like:
View source
Invokes the Loss instance.
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the mean absolute error between labels and predictions.
Main aliases
tf.keras.losses.mae, tf.keras.losses.mean_absolute_error, tf.keras.metrics.MAE, tf.keras.metrics.mae, tf.keras.metrics.mean_absolute_error, tf.losses.MAE, tf.losses.mae, tf.losses.mean_absolute_error, tf.metrics.MAE, tf.metrics.mae, tf.metrics.mean_absolute_error
See Migration guide for more details.
tf.compat.v1.keras.losses.MAE, tf.compat.v1.keras.losses.mae, tf.compat.v1.keras.losses.mean_absolute_error, tf.compat.v1.keras.metrics.MAE, tf.compat.v1.keras.metrics.mae, tf.compat.v1.keras.metrics.mean_absolute_error
loss = mean(abs(y_true - y_pred), axis=-1)


Computes the mean absolute percentage error between y_true and y_pred.
Main aliases
tf.keras.losses.mape, tf.keras.losses.mean_absolute_percentage_error, tf.keras.metrics.MAPE, tf.keras.metrics.mape, tf.keras.metrics.mean_absolute_percentage_error, tf.losses.MAPE, tf.losses.mape, tf.losses.mean_absolute_percentage_error, tf.metrics.MAPE, tf.metrics.mape, tf.metrics.mean_absolute_percentage_error
See Migration guide for more details.
tf.compat.v1.keras.losses.MAPE, tf.compat.v1.keras.losses.mape, tf.compat.v1.keras.losses.mean_absolute_percentage_error, tf.compat.v1.keras.metrics.MAPE, tf.compat.v1.keras.metrics.mape, tf.compat.v1.keras.metrics.mean_absolute_percentage_error
loss = 100 * mean(abs((y_true - y_pred) / y_true), axis=-1)


Computes the mean of absolute difference between labels and predictions.
Inherits From: Loss
Main aliases
tf.losses.MeanAbsoluteError
See Migration guide for more details.
tf.compat.v1.keras.losses.MeanAbsoluteError
loss = abs(y_true - y_pred)
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the mean absolute percentage error between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.MeanAbsolutePercentageError
See Migration guide for more details.
tf.compat.v1.keras.losses.MeanAbsolutePercentageError
loss = 100 * abs(y_true - y_pred) / y_true
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the mean of squares of errors between labels and predictions.
Inherits From: Loss
Main aliases
tf.losses.MeanSquaredError
See Migration guide for more details.
tf.compat.v1.keras.losses.MeanSquaredError
loss = square(y_true - y_pred)
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the mean squared logarithmic error between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.MeanSquaredLogarithmicError
See Migration guide for more details.
tf.compat.v1.keras.losses.MeanSquaredLogarithmicError
loss = square(log(y_true + 1.) - log(y_pred + 1.))
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the mean squared error between labels and predictions.
Main aliases
tf.keras.losses.mean_squared_error, tf.keras.losses.mse, tf.keras.metrics.MSE, tf.keras.metrics.mean_squared_error, tf.keras.metrics.mse, tf.losses.MSE, tf.losses.mean_squared_error, tf.losses.mse, tf.metrics.MSE, tf.metrics.mean_squared_error, tf.metrics.mse
See Migration guide for more details.
tf.compat.v1.keras.losses.MSE, tf.compat.v1.keras.losses.mean_squared_error, tf.compat.v1.keras.losses.mse, tf.compat.v1.keras.metrics.MSE, tf.compat.v1.keras.metrics.mean_squared_error, tf.compat.v1.keras.metrics.mse
After computing the squared distance between the inputs, the mean value over the last dimension is returned.
loss = mean(square(y_true - y_pred), axis=-1)


Computes the mean squared logarithmic error between y_true and y_pred.
Main aliases
tf.keras.losses.mean_squared_logarithmic_error, tf.keras.losses.msle, tf.keras.metrics.MSLE, tf.keras.metrics.mean_squared_logarithmic_error, tf.keras.metrics.msle, tf.losses.MSLE, tf.losses.mean_squared_logarithmic_error, tf.losses.msle, tf.metrics.MSLE, tf.metrics.mean_squared_logarithmic_error, tf.metrics.msle
See Migration guide for more details.
tf.compat.v1.keras.losses.MSLE, tf.compat.v1.keras.losses.mean_squared_logarithmic_error, tf.compat.v1.keras.losses.msle, tf.compat.v1.keras.metrics.MSLE, tf.compat.v1.keras.metrics.mean_squared_logarithmic_error, tf.compat.v1.keras.metrics.msle
loss = mean(square(log(y_true + 1) - log(y_pred + 1)), axis=-1)


Computes the Poisson loss between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.Poisson
See Migration guide for more details.
tf.compat.v1.keras.losses.Poisson
loss = y_pred - y_true * log(y_pred)
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Types of loss reduction.
Main aliases
tf.losses.Reduction
Contains the following values:
SUM_OVER_BATCH_SIZE: Scalar SUM divided by number of elements in losses. This reduction type is not supported when used with tf.distribute.Strategy outside of built-in training loops like tf.keras compile/fit.
You can implement 'SUM_OVER_BATCH_SIZE' using global batch size like:
Please see the custom training guide for more details on this.
View source
View source


Serializes loss function or Loss instance.
Main aliases
tf.losses.serialize
See Migration guide for more details.
tf.compat.v1.keras.losses.serialize


Computes the crossentropy loss between the labels and predictions.
Inherits From: Loss
Main aliases
tf.losses.SparseCategoricalCrossentropy
See Migration guide for more details.
tf.compat.v1.keras.losses.SparseCategoricalCrossentropy
Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided as integers. If you want to provide labels using one-hot representation, please use CategoricalCrossentropy loss. There should be # classes floating point values per feature for y_pred and a single floating point value per feature for y_true.
In the snippet below, there is a single floating point value per example for y_true and # classes floating pointing values per example for y_pred. The shape of y_true is [batch_size] and the shape of y_pred is [batch_size, num_classes].
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the sparse categorical crossentropy loss.
Main aliases
tf.keras.metrics.sparse_categorical_crossentropy, tf.losses.sparse_categorical_crossentropy, tf.metrics.sparse_categorical_crossentropy
See Migration guide for more details.
tf.compat.v1.keras.losses.sparse_categorical_crossentropy, tf.compat.v1.keras.metrics.sparse_categorical_crossentropy


Computes the squared hinge loss between y_true and y_pred.
Inherits From: Loss
Main aliases
tf.losses.SquaredHinge
See Migration guide for more details.
tf.compat.v1.keras.losses.SquaredHinge
loss = square(maximum(1 - y_true * y_pred, 0))
y_true values are expected to be -1 or 1. If binary (0 or 1) labels are provided we will convert them to -1 or 1.
Usage with the compile() API:
View source
Instantiates a Loss from its config (output of get_config()).
View source
Returns the config dictionary for a Loss instance.
View source
Invokes the Loss instance.


Computes the squared hinge loss between y_true and y_pred.
Main aliases
tf.keras.metrics.squared_hinge, tf.losses.squared_hinge, tf.metrics.squared_hinge
See Migration guide for more details.
tf.compat.v1.keras.losses.squared_hinge, tf.compat.v1.keras.metrics.squared_hinge
loss = mean(square(maximum(1 - y_true * y_pred, 0)), axis=-1)


Built-in metrics.
Main aliases
tf.metrics
class AUC: Computes the approximate AUC (Area under the curve) via a Riemann sum.
class Accuracy: Calculates how often predictions equal labels.
class BinaryAccuracy: Calculates how often predictions match binary labels.
class BinaryCrossentropy: Computes the crossentropy metric between the labels and predictions.
class CategoricalAccuracy: Calculates how often predictions matches one-hot labels.
class CategoricalCrossentropy: Computes the crossentropy metric between the labels and predictions.
class CategoricalHinge: Computes the categorical hinge metric between y_true and y_pred.
class CosineSimilarity: Computes the cosine similarity between the labels and predictions.
class FalseNegatives: Calculates the number of false negatives.
class FalsePositives: Calculates the number of false positives.
class Hinge: Computes the hinge metric between y_true and y_pred.
class KLDivergence: Computes Kullback-Leibler divergence metric between y_true and y_pred.
class LogCoshError: Computes the logarithm of the hyperbolic cosine of the prediction error.
class Mean: Computes the (weighted) mean of the given values.
class MeanAbsoluteError: Computes the mean absolute error between the labels and predictions.
class MeanAbsolutePercentageError: Computes the mean absolute percentage error between y_true and y_pred.
class MeanIoU: Computes the mean Intersection-Over-Union metric.
class MeanRelativeError: Computes the mean relative error by normalizing with the given values.
class MeanSquaredError: Computes the mean squared error between y_true and y_pred.
class MeanSquaredLogarithmicError: Computes the mean squared logarithmic error between y_true and y_pred.
class MeanTensor: Computes the element-wise (weighted) mean of the given tensors.
class Metric: Encapsulates metric logic and state.
class Poisson: Computes the Poisson metric between y_true and y_pred.
class Precision: Computes the precision of the predictions with respect to the labels.
class PrecisionAtRecall: Computes best precision where recall is >= specified value.
class Recall: Computes the recall of the predictions with respect to the labels.
class RecallAtPrecision: Computes best recall where precision is >= specified value.
class RootMeanSquaredError: Computes root mean squared error metric between y_true and y_pred.
class SensitivityAtSpecificity: Computes best sensitivity where specificity is >= specified value.
class SparseCategoricalAccuracy: Calculates how often predictions matches integer labels.
class SparseCategoricalCrossentropy: Computes the crossentropy metric between the labels and predictions.
class SparseTopKCategoricalAccuracy: Computes how often integer targets are in the top K predictions.
class SpecificityAtSensitivity: Computes best specificity where sensitivity is >= specified value.
class SquaredHinge: Computes the squared hinge metric between y_true and y_pred.
class Sum: Computes the (weighted) sum of the given values.
class TopKCategoricalAccuracy: Computes how often targets are in the top K predictions.
class TrueNegatives: Calculates the number of true negatives.
class TruePositives: Calculates the number of true positives.
KLD(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
MAE(...): Computes the mean absolute error between labels and predictions.
MAPE(...): Computes the mean absolute percentage error between y_true and y_pred.
MSE(...): Computes the mean squared error between labels and predictions.
MSLE(...): Computes the mean squared logarithmic error between y_true and y_pred.
binary_accuracy(...): Calculates how often predictions matches binary labels.
binary_crossentropy(...): Computes the binary crossentropy loss.
categorical_accuracy(...): Calculates how often predictions matches one-hot labels.
categorical_crossentropy(...): Computes the categorical crossentropy loss.
deserialize(...): Deserializes a serialized metric class/function instance.
get(...): Retrieves a Keras metric as a function/Metric class instance.
hinge(...): Computes the hinge loss between y_true and y_pred.
kl_divergence(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
kld(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
kullback_leibler_divergence(...): Computes Kullback-Leibler divergence loss between y_true and y_pred.
log_cosh(...): Logarithm of the hyperbolic cosine of the prediction error.
logcosh(...): Logarithm of the hyperbolic cosine of the prediction error.
mae(...): Computes the mean absolute error between labels and predictions.
mape(...): Computes the mean absolute percentage error between y_true and y_pred.
mean_absolute_error(...): Computes the mean absolute error between labels and predictions.
mean_absolute_percentage_error(...): Computes the mean absolute percentage error between y_true and y_pred.
mean_squared_error(...): Computes the mean squared error between labels and predictions.
mean_squared_logarithmic_error(...): Computes the mean squared logarithmic error between y_true and y_pred.
mse(...): Computes the mean squared error between labels and predictions.
msle(...): Computes the mean squared logarithmic error between y_true and y_pred.
poisson(...): Computes the Poisson loss between y_true and y_pred.
serialize(...): Serializes metric function or Metric instance.
sparse_categorical_accuracy(...): Calculates how often predictions matches integer labels.
sparse_categorical_crossentropy(...): Computes the sparse categorical crossentropy loss.
sparse_top_k_categorical_accuracy(...): Computes how often integer targets are in the top K predictions.
squared_hinge(...): Computes the squared hinge loss between y_true and y_pred.
top_k_categorical_accuracy(...): Computes how often targets are in the top K predictions.


Calculates how often predictions equal labels.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.Accuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.Accuracy
This metric creates two local variables, total and count that are used to compute the frequency with which y_pred matches y_true. This frequency is ultimately returned as binary accuracy: an idempotent operation that simply divides total by count.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the approximate AUC (Area under the curve) via a Riemann sum.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.AUC
See Migration guide for more details.
tf.compat.v1.keras.metrics.AUC
This metric creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the AUC. To discretize the AUC curve, a linearly spaced set of thresholds is used to compute pairs of recall and precision values. The area under the ROC-curve is therefore computed using the height of the recall values by the false positive rate, while the area under the PR-curve is the computed using the height of the precision values by the recall.
This value is ultimately returned as auc, an idempotent operation that computes the area under a discretized curve of precision versus recall values (computed using the aforementioned variables). The num_thresholds variable controls the degree of discretization with larger numbers of thresholds more closely approximating the true AUC. The quality of the approximation may vary dramatically depending on num_thresholds. The thresholds parameter can be used to manually specify thresholds which split the predictions more evenly.
For best results, predictions should be distributed approximately uniformly in the range [0, 1] and not peaked around 0 or 1. The quality of the AUC approximation may be poor if this is not the case. Setting summation_method to 'minoring' or 'majoring' can help quantify the error in the approximation by providing lower or upper bound estimate of the AUC.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Interpolation formula inspired by section 4 of Davis & Goadrich 2006.
https://www.biostat.wisc.edu/~page/rocpr.pdf
Note here we derive & use a closed formula not present in the paper as follows:
Precision = TP / (TP + FP) = TP / P
Modeling all of TP (true positive), FP (false positive) and their sum P = TP + FP (predicted positive) as varying linearly within each interval [A, B] between successive thresholds, we get
Precision slope = dTP / dP = (TP_B - TP_A) / (P_B - P_A) = (TP - TP_A) / (P - P_A) Precision = (TP_A + slope * (P - P_A)) / P
The area within the interval is (slope / total_pos_weight) times
int_A^B{Precision.dP} = int_A^B{(TP_A + slope * (P - P_A)) * dP / P} int_A^B{Precision.dP} = int_A^B{slope * dP + intercept * dP / P}
where intercept = TP_A - slope * P_A = TP_B - slope * P_B, resulting in
int_A^B{Precision.dP} = TP_B - TP_A + intercept * log(P_B / P_A)
Bringing back the factor (slope / total_pos_weight) we'd put aside, we get
slope * [dTP + intercept * log(P_B / P_A)] / total_pos_weight
where dTP == TP_B - TP_A.
Note that when P_A == 0 the above calculation simplifies into
int_A^B{Precision.dTP} = int_A^B{slope * dTP} = slope * (TP_B - TP_A)
which is really equivalent to imputing constant precision throughout the first bucket having >0 true positives.
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates confusion matrix statistics.


Calculates how often predictions match binary labels.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.BinaryAccuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.BinaryAccuracy
This metric creates two local variables, total and count that are used to compute the frequency with which y_pred matches y_true. This frequency is ultimately returned as binary accuracy: an idempotent operation that simply divides total by count.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the crossentropy metric between the labels and predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.BinaryCrossentropy
See Migration guide for more details.
tf.compat.v1.keras.metrics.BinaryCrossentropy
This is the crossentropy metric class to be used when there are only two label classes (0 and 1).
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Calculates how often predictions matches binary labels.
Main aliases
tf.metrics.binary_accuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.binary_accuracy


Calculates how often predictions matches one-hot labels.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.CategoricalAccuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.CategoricalAccuracy
You can provide logits of classes as y_pred, since argmax of logits and probabilities are same.
This metric creates two local variables, total and count that are used to compute the frequency with which y_pred matches y_true. This frequency is ultimately returned as categorical accuracy: an idempotent operation that simply divides total by count.
y_pred and y_true should be passed in as vectors of probabilities, rather than as labels. If necessary, use tf.one_hot to expand y_true as a vector.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the crossentropy metric between the labels and predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.CategoricalCrossentropy
See Migration guide for more details.
tf.compat.v1.keras.metrics.CategoricalCrossentropy
This is the crossentropy metric class to be used when there are multiple label classes (2 or more). Here we assume that labels are given as a one_hot representation. eg., When labels values are [2, 0, 1], y_true = [[0, 0, 1], [1, 0, 0], [0, 1, 0]].
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the categorical hinge metric between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.CategoricalHinge
See Migration guide for more details.
tf.compat.v1.keras.metrics.CategoricalHinge
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Calculates how often predictions matches one-hot labels.
Main aliases
tf.metrics.categorical_accuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.categorical_accuracy
You can provide logits of classes as y_pred, since argmax of logits and probabilities are same.


Computes the cosine similarity between the labels and predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.CosineSimilarity
See Migration guide for more details.
tf.compat.v1.keras.metrics.CosineSimilarity
cosine similarity = (a . b) / ||a|| ||b||
See: Cosine Similarity.
This metric keeps the average cosine similarity between predictions and labels over a stream of data.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Deserializes a serialized metric class/function instance.
Main aliases
tf.metrics.deserialize
See Migration guide for more details.
tf.compat.v1.keras.metrics.deserialize


Calculates the number of false negatives.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.FalseNegatives
See Migration guide for more details.
tf.compat.v1.keras.metrics.FalseNegatives
If sample_weight is given, calculates the sum of the weights of false negatives. This metric creates one local variable, accumulator that is used to keep track of the number of false negatives.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates the metric statistics.


Calculates the number of false positives.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.FalsePositives
See Migration guide for more details.
tf.compat.v1.keras.metrics.FalsePositives
If sample_weight is given, calculates the sum of the weights of false positives. This metric creates one local variable, accumulator that is used to keep track of the number of false positives.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates the metric statistics.


Retrieves a Keras metric as a function/Metric class instance.
Main aliases
tf.metrics.get
See Migration guide for more details.
tf.compat.v1.keras.metrics.get
The identifier may be the string name of a metric function or class.
You can also specify config of the metric to this function by passing dict containing class_name and config as an identifier. Also note that the class_name must map to a Metric class


Computes the hinge metric between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.Hinge
See Migration guide for more details.
tf.compat.v1.keras.metrics.Hinge
y_true values are expected to be -1 or 1. If binary (0 or 1) labels are provided we will convert them to -1 or 1.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes Kullback-Leibler divergence metric between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.KLDivergence
See Migration guide for more details.
tf.compat.v1.keras.metrics.KLDivergence
metric = y_true * log(y_true / y_pred)
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the logarithm of the hyperbolic cosine of the prediction error.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.LogCoshError
See Migration guide for more details.
tf.compat.v1.keras.metrics.LogCoshError
logcosh = log((exp(x) + exp(-x))/2), where x is the error (y_pred - y_true)
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the (weighted) mean of the given values.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.Mean
See Migration guide for more details.
tf.compat.v1.keras.metrics.Mean
For example, if values is [1, 3, 5, 7] then the mean is 4. If the weights were specified as [1, 1, 0, 0] then the mean would be 2.
This metric creates two variables, total and count that are used to compute the average of values. This average is ultimately returned as mean which is an idempotent operation that simply divides total by count.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates statistics for computing the metric.


Computes the mean absolute error between the labels and predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.MeanAbsoluteError
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanAbsoluteError
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the mean absolute percentage error between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.MeanAbsolutePercentageError
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanAbsolutePercentageError
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the mean Intersection-Over-Union metric.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.MeanIoU
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanIoU
Mean Intersection-Over-Union is a common evaluation metric for semantic image segmentation, which first computes the IOU for each semantic class and then computes the average over classes. IOU is defined as follows: IOU = true_positive / (true_positive + false_positive + false_negative). The predictions are accumulated in a confusion matrix, weighted by sample_weight and the metric is then calculated from it.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Compute the mean intersection-over-union via the confusion matrix.
View source
Accumulates the confusion matrix statistics.


Computes the mean relative error by normalizing with the given values.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.MeanRelativeError
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanRelativeError
This metric creates two local variables, total and count that are used to compute the mean relative error. This is weighted by sample_weight, and it is ultimately returned as mean_relative_error: an idempotent operation that simply divides total by count.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.


Computes the mean squared error between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.MeanSquaredError
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanSquaredError
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the mean squared logarithmic error between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.MeanSquaredLogarithmicError
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanSquaredLogarithmicError
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the element-wise (weighted) mean of the given tensors.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.MeanTensor
See Migration guide for more details.
tf.compat.v1.keras.metrics.MeanTensor
MeanTensor returns a tensor with the same shape of the input tensors. The mean value is updated by keeping local variables total and count. The total tracks the sum of the weighted values, and count stores the sum of the weighted counts.
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates statistics for computing the element-wise mean.


Encapsulates metric logic and state.
Inherits From: Layer, Module
Main aliases
tf.metrics.Metric
See Migration guide for more details.
tf.compat.v1.keras.metrics.Metric
Usage with compile() API:
To be implemented by subclasses:
Example subclass implementation:
View source
Adds state variable. Only for use by subclasses.
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates statistics for the metric.


Computes the Poisson metric between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.Poisson
See Migration guide for more details.
tf.compat.v1.keras.metrics.Poisson
metric = y_pred - y_true * log(y_pred)
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the precision of the predictions with respect to the labels.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.Precision
See Migration guide for more details.
tf.compat.v1.keras.metrics.Precision
The metric creates two local variables, true_positives and false_positives that are used to compute the precision. This value is ultimately returned as precision, an idempotent operation that simply divides true_positives by the sum of true_positives and false_positives.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
If top_k is set, we'll calculate precision as how often on average a class among the top-k classes with the highest predicted values of a batch entry is correct and can be found in the label for that entry.
If class_id is specified, we calculate precision by considering only the entries in the batch for which class_id is above the threshold and/or in the top-k highest predictions, and computing the fraction of them for which class_id is indeed a correct label.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates true positive and false positive statistics.


Computes best precision where recall is >= specified value.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.PrecisionAtRecall
See Migration guide for more details.
tf.compat.v1.keras.metrics.PrecisionAtRecall
This metric creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the precision at the given recall. The threshold for the given recall value is computed and used to evaluate the corresponding precision.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates confusion matrix statistics.


Computes the recall of the predictions with respect to the labels.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.Recall
See Migration guide for more details.
tf.compat.v1.keras.metrics.Recall
This metric creates two local variables, true_positives and false_negatives, that are used to compute the recall. This value is ultimately returned as recall, an idempotent operation that simply divides true_positives by the sum of true_positives and false_negatives.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
If top_k is set, recall will be computed as how often on average a class among the labels of a batch entry is in the top-k predictions.
If class_id is specified, we calculate recall by considering only the entries in the batch for which class_id is in the label, and computing the fraction of them for which class_id is above the threshold and/or in the top-k predictions.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates true positive and false negative statistics.


Computes best recall where precision is >= specified value.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.RecallAtPrecision
See Migration guide for more details.
tf.compat.v1.keras.metrics.RecallAtPrecision
For a given score-label-distribution the required precision might not be achievable, in this case 0.0 is returned as recall.
This metric creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the recall at the given precision. The threshold for the given precision value is computed and used to evaluate the corresponding recall.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates confusion matrix statistics.


Computes root mean squared error metric between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.RootMeanSquaredError
See Migration guide for more details.
tf.compat.v1.keras.metrics.RootMeanSquaredError
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates root mean squared error statistics.


Computes best sensitivity where specificity is >= specified value.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.SensitivityAtSpecificity
See Migration guide for more details.
tf.compat.v1.keras.metrics.SensitivityAtSpecificity
the sensitivity at a given specificity.
Sensitivity measures the proportion of actual positives that are correctly identified as such (tp / (tp + fn)). Specificity measures the proportion of actual negatives that are correctly identified as such (tn / (tn + fp)).
This metric creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the sensitivity at the given specificity. The threshold for the given specificity value is computed and used to evaluate the corresponding sensitivity.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
For additional information about specificity and sensitivity, see the following.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates confusion matrix statistics.


Serializes metric function or Metric instance.
Main aliases
tf.metrics.serialize
See Migration guide for more details.
tf.compat.v1.keras.metrics.serialize


Calculates how often predictions matches integer labels.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.SparseCategoricalAccuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.SparseCategoricalAccuracy
You can provide logits of classes as y_pred, since argmax of logits and probabilities are same.
This metric creates two local variables, total and count that are used to compute the frequency with which y_pred matches y_true. This frequency is ultimately returned as sparse categorical accuracy: an idempotent operation that simply divides total by count.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the crossentropy metric between the labels and predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.SparseCategoricalCrossentropy
See Migration guide for more details.
tf.compat.v1.keras.metrics.SparseCategoricalCrossentropy
Use this crossentropy metric when there are two or more label classes. We expect labels to be provided as integers. If you want to provide labels using one-hot representation, please use CategoricalCrossentropy metric. There should be # classes floating point values per feature for y_pred and a single floating point value per feature for y_true.
In the snippet below, there is a single floating point value per example for y_true and # classes floating pointing values per example for y_pred. The shape of y_true is [batch_size] and the shape of y_pred is [batch_size, num_classes].
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes how often integer targets are in the top K predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.SparseTopKCategoricalAccuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.SparseTopKCategoricalAccuracy
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Calculates how often predictions matches integer labels.
Main aliases
tf.metrics.sparse_categorical_accuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.sparse_categorical_accuracy
You can provide logits of classes as y_pred, since argmax of logits and probabilities are same.


Computes how often integer targets are in the top K predictions.
Main aliases
tf.metrics.sparse_top_k_categorical_accuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.sparse_top_k_categorical_accuracy


Computes best specificity where sensitivity is >= specified value.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.SpecificityAtSensitivity
See Migration guide for more details.
tf.compat.v1.keras.metrics.SpecificityAtSensitivity
Sensitivity measures the proportion of actual positives that are correctly identified as such (tp / (tp + fn)). Specificity measures the proportion of actual negatives that are correctly identified as such (tn / (tn + fp)).
This metric creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the specificity at the given sensitivity. The threshold for the given sensitivity value is computed and used to evaluate the corresponding specificity.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
For additional information about specificity and sensitivity, see the following.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates confusion matrix statistics.


Computes the squared hinge metric between y_true and y_pred.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.SquaredHinge
See Migration guide for more details.
tf.compat.v1.keras.metrics.SquaredHinge
y_true values are expected to be -1 or 1. If binary (0 or 1) labels are provided we will convert them to -1 or 1.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes the (weighted) sum of the given values.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.Sum
See Migration guide for more details.
tf.compat.v1.keras.metrics.Sum
For example, if values is [1, 3, 5, 7] then the sum is 16. If the weights were specified as [1, 1, 0, 0] then the sum would be 4.
This metric creates one variable, total, that is used to compute the sum of values. This is ultimately returned as sum.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates statistics for computing the metric.


Computes how often targets are in the top K predictions.
Inherits From: Mean, Metric, Layer, Module
Main aliases
tf.metrics.TopKCategoricalAccuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.TopKCategoricalAccuracy
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates metric statistics.
y_true and y_pred should have the same shape.


Computes how often targets are in the top K predictions.
Main aliases
tf.metrics.top_k_categorical_accuracy
See Migration guide for more details.
tf.compat.v1.keras.metrics.top_k_categorical_accuracy


Calculates the number of true negatives.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.TrueNegatives
See Migration guide for more details.
tf.compat.v1.keras.metrics.TrueNegatives
If sample_weight is given, calculates the sum of the weights of true negatives. This metric creates one local variable, accumulator that is used to keep track of the number of true negatives.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates the metric statistics.


Calculates the number of true positives.
Inherits From: Metric, Layer, Module
Main aliases
tf.metrics.TruePositives
See Migration guide for more details.
tf.compat.v1.keras.metrics.TruePositives
If sample_weight is given, calculates the sum of the weights of true positives. This metric creates one local variable, true_positives that is used to keep track of the number of true positives.
If sample_weight is None, weights default to 1. Use sample_weight of 0 to mask values.
Usage with compile() API:
View source
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
View source
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
View source
Accumulates the metric statistics.


Keras mixed precision API.
See the mixed precision guide to learn how to use the API.
experimental module: Public API for tf.keras.mixed_precision.experimental namespace.
class LossScaleOptimizer: An optimizer that applies loss scaling to prevent numeric underflow.
class Policy: A dtype policy for a Keras layer.
global_policy(...): Returns the global dtype policy.
set_global_policy(...): Sets the global dtype policy.


Public API for tf.keras.mixed_precision.experimental namespace.
class LossScaleOptimizer: An deprecated optimizer that applies loss scaling.
class Policy: A deprecated dtype policy for a Keras layer.
get_layer_policy(...): Returns the dtype policy of a layer.
global_policy(...): Returns the global dtype policy.
set_policy(...): Sets the global dtype policy.


An deprecated optimizer that applies loss scaling.
Inherits From: LossScaleOptimizer, Optimizer
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.mixed_precision.experimental.LossScaleOptimizer
This class is identical to the non-experimental keras.mixed_precision.LossScaleOptimizer except its constructor takes different arguments. For this class (the experimental version), the constructor takes a loss_scale argument. For the non-experimental class, the constructor encodes the loss scaling information in multiple arguments. Note that unlike this class, the non-experimental class does not accept a tf.compat.v1.mixed_precision.LossScale, which is deprecated.
If you currently use this class, you should switch to the non-experimental tf.keras.mixed_precision.LossScaleOptimizer instead. We show several examples of converting the use of the experimental class to the equivalent non-experimental class.
Make sure to also switch from this class to the non-experimental class in isinstance checks, if you have any. If you do not do this, your model may run into hard-to-debug issues, as the experimental LossScaleOptimizer subclasses the non-experimental LossScaleOptimizer, but not vice versa. It is safe to switch isinstance checks to the non-experimental LossScaleOptimizer even before using the non-experimental LossScaleOptimizer.
This is None if LossScaleOptimizer.dynamic is False.
The counter is incremented every step. Once it reaches LossScaleOptimizer.dynamic_growth_steps, the loss scale will be doubled and the counter will be reset back to zero. If nonfinite gradients are encountered, the loss scale will be halved and the counter will be reset back to zero.
This is None if LossScaleOptimizer.dynamic is False.
Every dynamic_growth_steps consecutive steps with finite gradients, the loss scale is increased.
If LossScaleOptimizer.dynamic is False, this is the same number as LossScaleOptimizer.loss_scale, as the loss scale never changes.
View source
Scales the loss by the loss scale.
This method is only needed if you compute gradients manually, e.g. with tf.GradientTape. In that case, call this method to scale the loss before passing the loss to tf.GradientTape. If you use LossScaleOptimizer.minimize or LossScaleOptimizer.get_gradients, loss scaling is automatically applied and this method is unneeded.
If this method is called, get_unscaled_gradients should also be called. See the tf.keras.mixed_precision.LossScaleOptimizer doc for an example.
View source
Unscales the gradients by the loss scale.
This method is only needed if you compute gradients manually, e.g. with tf.GradientTape. In that case, call this method to unscale the gradients after computing them with tf.GradientTape. If you use LossScaleOptimizer.minimize or LossScaleOptimizer.get_gradients, loss scaling is automatically applied and this method is unneeded.
If this method is called, get_scaled_loss should also be called. See the tf.keras.mixed_precision.LossScaleOptimizer doc for an example.


A deprecated dtype policy for a Keras layer.
Inherits From: Policy
The difference between this class and the non-experimental class is that this class has a loss_scale field and the non-experimental class does not. The loss scale is only used by tf.keras.Model.compile, which automatically wraps the optimizer with a LossScaleOptimizer if the optimzier is not already a LossScaleOptimizer. For the non-experimental Policy class, Model.compile instead wraps the optimizer with a LossScaleOptimizer if Policy.name is "mixed_float16".
When deserializing objects with an experimental policy using functions like tf.keras.utils.deserialize_keras_object, the policy will be deserialized as the non-experimental tf.keras.mixed_precision.Policy, and the loss scale will silently be dropped. This is so that SavedModels that are generated with an expeirmental policy can be restored after the experimental policy is removed.
This is the dtype layers will do their computations in. Typically layers output tensors with the compute dtype as well.
Note that even if the compute dtype is float16 or bfloat16, hardware devices may not do individual adds, multiplies, and other fundamental operations in float16 or bfloat16, but instead may do some of them in float32 for numeric stability. The compute dtype is the dtype of the inputs and outputs of the TensorFlow ops that the layer executes. Internally, many TensorFlow ops will do certain internal calculations in float32 or some other device-internal intermediate format with higher precision than float16/bfloat16, to increase numeric stability.
For example, a tf.keras.layers.Dense layer, when run on a GPU with a float16 compute dtype, will pass float16 inputs to tf.linalg.matmul. But, tf.linalg.matmul will do use float32 intermediate math. The performance benefit of float16 is still apparent, due to increased memory bandwidth and the fact modern GPUs have specialized hardware for computing matmuls on float16 inputs while still keeping intermediate computations in float32.
This is the dtype layers will create their variables in, unless a layer explicitly chooses a different dtype. If this is different than Policy.compute_dtype, Layers will cast variables to the compute dtype to avoid type errors.
Variable regularizers are run in the variable dtype, not the compute dtype.
View source
View source


Returns the global dtype policy.
Main aliases
tf.keras.mixed_precision.experimental.global_policy
The global policy is the default tf.keras.mixed_precision.Policy used for layers, if no policy is passed to the layer constructor. If no policy has been set with keras.mixed_precision.set_global_policy, this will return a policy constructed from tf.keras.backend.floatx() (floatx defaults to float32).
If TensorFlow 2 behavior has been disabled with tf.compat.v1.disable_v2_behavior(), this will instead return a special "_infer" policy which infers the dtype from the dtype of the first input the first time the layer is called. This behavior matches the behavior that existed in TensorFlow 1.
See tf.keras.mixed_precision.Policy for more information on policies.


An optimizer that applies loss scaling to prevent numeric underflow.
Inherits From: Optimizer
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.mixed_precision.LossScaleOptimizer
Loss scaling is a technique to prevent numeric underflow in intermediate gradients when float16 is used. To prevent underflow, the loss is multiplied (or "scaled") by a certain factor called the "loss scale", which causes intermediate gradients to be scaled by the loss scale as well. The final gradients are divided (or "unscaled") by the loss scale to bring them back to their original value.
LossScaleOptimizer wraps another optimizer and applies loss scaling to it. By default, the loss scale is dynamically updated over time so you do not have to choose the loss scale. The minimize method automatically scales the loss, unscales the gradients, and updates the loss scale so all you have to do is wrap your optimizer with a LossScaleOptimizer if you use minimize. For example:
If a tf.GradientTape is used to compute gradients instead of minimize, you must scale the loss and gradients manually. This can be done with the LossScaleOptimizer.get_scaled_loss and LossScaleOptimizer.get_unscaled_gradients methods. For example:
When mixed precision with float16 is used, there is typically no risk of underflow affecting model quality if loss scaling is properly used. See the mixed precision guide for more information on how to use mixed precision.
LossScaleOptimizer will occasionally skip applying gradients to the variables, in which case the trainable variables will not change that step. This is done because the dynamic loss scale will sometimes be raised too high, causing overflow in the gradients. Typically, the first 2 to 15 steps of the model are skipped as the initial loss scale is very high, but afterwards steps will only be skipped on average 0.05% of the time (the fraction of steps skipped is 1 / dynamic_growth_steps).
LossScaleOptimizer delegates all public Optimizer methods to the inner optimizer. Additionally, in methods minimize and get_gradients, it scales the loss and unscales the gradients. In methodsminimizeandapply_gradients`, it additionally updates the loss scale and skips applying gradients if any gradient has a nonfinite value.
Hyperparameters can be accessed and set on the LossScaleOptimizer, which will be delegated to the wrapped optimizer.
However, accessing or setting non-hyperparameters is not delegated to the LossScaleOptimizer. In an Adam optimizer, beta_1 is a hyperparameter but epsilon is not, as the Adam optimizer only calls Optimizer._set_hyper on beta_1.
In the above example, despite epsilon being set on the LossScaleOptimizer, the old epsilon value will still be used when training as epsilon was not set on the inner optimizer.
This is None if LossScaleOptimizer.dynamic is False.
The counter is incremented every step. Once it reaches LossScaleOptimizer.dynamic_growth_steps, the loss scale will be doubled and the counter will be reset back to zero. If nonfinite gradients are encountered, the loss scale will be halved and the counter will be reset back to zero.
This is None if LossScaleOptimizer.dynamic is False.
Every dynamic_growth_steps consecutive steps with finite gradients, the loss scale is increased.
If LossScaleOptimizer.dynamic is False, this is the same number as LossScaleOptimizer.loss_scale, as the loss scale never changes.
View source
Scales the loss by the loss scale.
This method is only needed if you compute gradients manually, e.g. with tf.GradientTape. In that case, call this method to scale the loss before passing the loss to tf.GradientTape. If you use LossScaleOptimizer.minimize or LossScaleOptimizer.get_gradients, loss scaling is automatically applied and this method is unneeded.
If this method is called, get_unscaled_gradients should also be called. See the tf.keras.mixed_precision.LossScaleOptimizer doc for an example.
View source
Unscales the gradients by the loss scale.
This method is only needed if you compute gradients manually, e.g. with tf.GradientTape. In that case, call this method to unscale the gradients after computing them with tf.GradientTape. If you use LossScaleOptimizer.minimize or LossScaleOptimizer.get_gradients, loss scaling is automatically applied and this method is unneeded.
If this method is called, get_scaled_loss should also be called. See the tf.keras.mixed_precision.LossScaleOptimizer doc for an example.


A dtype policy for a Keras layer.
A dtype policy determines a layer's computation and variable dtypes. Each layer has a policy. Policies can be passed to the dtype argument of layer constructors, or a global policy can be set with tf.keras.mixed_precision.set_global_policy.
Typically you only need to interact with dtype policies when using mixed precision, which is the use of float16 or bfloat16 for computations and float32 for variables. This is why the term mixed_precision appears in the API name. Mixed precision can be enabled by passing 'mixed_float16' or 'mixed_bfloat16' to tf.keras.mixed_precision.set_global_policy. See the mixed precision guide for more information on how to use mixed precision.
In the example above, passing dtype='float32' to the layer is equivalent to passing dtype=tf.keras.mixed_precision.Policy('float32'). In general, passing a dtype to a layer is equivalent to passing the corresponding policy, so it is never necessary to explicitly construct a Policy object.
A layer casts its inputs to its compute dtype. This causes the layer's computations and output to also be in the compute dtype. For example:
Note that the base tf.keras.layers.Layer class inserts the casts. If subclassing your own layer, you do not have to insert any casts.
Currently, only tensors in the first argument to the layer's call method are casted (although this will likely be changed in a future minor release). For example:
If writing your own layer with multiple inputs, you should either explicitly cast other tensors to self.compute_dtype in call or accept all tensors in the first argument as a list.
The casting only occurs in TensorFlow 2. If tf.compat.v1.disable_v2_behavior() has been called, you can enable the casting behavior with tf.compat.v1.keras.layers.enable_v2_dtype_behavior().
The default dtype of variables created by tf.keras.layers.Layer.add_weight is the layer's policy's variable dtype.
If a layer's compute and variable dtypes differ, add_weight will wrap floating-point variables with a special wrapper called an AutoCastVariable. AutoCastVariable is identical to the original variable except it casts itself to the layer's compute dtype when used within Layer.call. This means if you are writing a layer, you do not have to explicitly cast the variables to the layer's compute dtype. For example:
A layer author can prevent a variable from being wrapped with an AutoCastVariable by passing experimental_autocast=False to add_weight, which is useful if the float32 value of the variable must be accessed within the layer.
For the most part, layers will automatically support mixed precision and float64 without any additional work, due to the fact the base layer automatically casts inputs, creates variables of the correct type, and in the case of mixed precision, wraps variables with AutoCastVariables.
The primary case where you need extra work to support mixed precision or float64 is when you create a new tensor, such as with tf.ones or tf.random.normal, In such cases, you must create the tensor of the correct dtype. For example, if you call tf.random.normal, you must pass the compute dtype, which is the dtype the inputs have been casted to:
If you did not pass dtype=inputs.dtype to tf.random.normal, a TypeError would have occurred. This is because the tf.random.normal's dtype defaults to "float32", but the input dtype is float16. You cannot add a float32 tensor with a float16 tensor.
This is the dtype layers will do their computations in. Typically layers output tensors with the compute dtype as well.
Note that even if the compute dtype is float16 or bfloat16, hardware devices may not do individual adds, multiplies, and other fundamental operations in float16 or bfloat16, but instead may do some of them in float32 for numeric stability. The compute dtype is the dtype of the inputs and outputs of the TensorFlow ops that the layer executes. Internally, many TensorFlow ops will do certain internal calculations in float32 or some other device-internal intermediate format with higher precision than float16/bfloat16, to increase numeric stability.
For example, a tf.keras.layers.Dense layer, when run on a GPU with a float16 compute dtype, will pass float16 inputs to tf.linalg.matmul. But, tf.linalg.matmul will do use float32 intermediate math. The performance benefit of float16 is still apparent, due to increased memory bandwidth and the fact modern GPUs have specialized hardware for computing matmuls on float16 inputs while still keeping intermediate computations in float32.
This is the dtype layers will create their variables in, unless a layer explicitly chooses a different dtype. If this is different than Policy.compute_dtype, Layers will cast variables to the compute dtype to avoid type errors.
Variable regularizers are run in the variable dtype, not the compute dtype.
View source
View source


Sets the global dtype policy.
Main aliases
tf.keras.mixed_precision.experimental.set_policy
The global policy is the default tf.keras.mixed_precision.Policy used for layers, if no policy is passed to the layer constructor.
If no global policy is set, layers will instead default to a Policy constructed from tf.keras.backend.floatx().
To use mixed precision, the global policy should be set to 'mixed_float16' or 'mixed_bfloat16', so that every layer uses a 16-bit compute dtype and float32 variable dtype by default.
Only floating point policies can be set as the global policy, such as 'float32' and 'mixed_float16'. Non-floating point policies such as 'int32' and 'complex64' cannot be set as the global policy because most layers do not support such policies.
See tf.keras.mixed_precision.Policy for more information.


Model groups layers into an object with training and inference features.
Inherits From: Layer, Module
Main aliases
tf.keras.models.Model
See Migration guide for more details.
tf.compat.v1.keras.Model, tf.compat.v1.keras.models.Model
There are two ways to instantiate a Model:
1 - With the "Functional API", where you start from Input, you chain layer calls to specify the model's forward pass, and finally you create your model from inputs and outputs:
2 - By subclassing the Model class: in that case, you should define your layers in __init__ and you should implement the model's forward pass in call.
If you subclass Model, you can optionally have a training argument (boolean) in call, which you can use to specify a different behavior in training and inference:
Once the model is created, you can config the model with losses and metrics with model.compile(), train the model with model.fit(), or use the model to do prediction with model.predict().
Running eagerly means that your model will be run step by step, like Python code. Your model might run slower, but it should become easier for you to debug it by stepping into individual layer calls.
By default, we will attempt to compile your model to a static graph to deliver the best execution performance.
View source
Configures the model for training.
View source
Returns the loss value & metrics values for the model in test mode.
Computation is done in batches (see the batch_size arg.)
See the discussion of Unpacking behavior for iterator-like inputs for Model.fit.
View source
Evaluates the model on a data generator.
Model.evaluate now supports generators, so there is no longer any need to use this endpoint.
View source
Trains the model for a fixed number of epochs (iterations on a dataset).
Unpacking behavior for iterator-like inputs: A common pattern is to pass a tf.data.Dataset, generator, or tf.keras.utils.Sequence to the x argument of fit, which will in fact yield not only features (x) but optionally targets (y) and sample weights. Keras requires that the output of such iterator-likes be unambiguous. The iterator should return a tuple of length 1, 2, or 3, where the optional second and third elements will be used for y and sample_weight respectively. Any other type provided will be wrapped in a length one tuple, effectively treating everything as 'x'. When yielding dicts, they should still adhere to the top-level tuple structure. e.g. ({"x0": x0, "x1": x1}, y). Keras will not attempt to separate features, targets, and weights from the keys of a single dict. A notable unsupported data type is the namedtuple. The reason is that it behaves like both an ordered datatype (tuple) and a mapping datatype (dict). So given a namedtuple of the form: namedtuple("example_tuple", ["y", "x"]) it is ambiguous whether to reverse the order of the elements when interpreting the value. Even worse is a tuple of the form: namedtuple("other_tuple", ["x", "y", "z"]) where it is unclear if the tuple was intended to be unpacked into x, y, and sample_weight or passed through as a single element to x. As a result the data processing code will simply raise a ValueError if it encounters a namedtuple. (Along with instructions to remedy the issue.)
View source
Fits the model on data yielded batch-by-batch by a Python generator.
Model.fit now supports generators, so there is no longer any need to use this endpoint.
View source
Retrieves a layer based on either its name (unique) or index.
If name and index are both provided, index will take precedence. Indices are based on order of horizontal graph traversal (bottom-up).
View source
Loads all layer weights, either from a TensorFlow or an HDF5 weight file.
If by_name is False weights are loaded based on the network's topology. This means the architecture should be the same as when the weights were saved. Note that layers that don't have weights are not taken into account in the topological ordering, so adding or removing layers is fine as long as they don't have weights.
If by_name is True, weights are loaded into layers only if they share the same name. This is useful for fine-tuning or transfer-learning models where some of the layers have changed.
Only topological loading (by_name=False) is supported when loading weights from the TensorFlow format. Note that topological loading differs slightly between TensorFlow and HDF5 formats for user-defined classes inheriting from tf.keras.Model: HDF5 loads based on a flattened list of weights, while the TensorFlow format loads based on the object-local names of attributes to which layers are assigned in the Model's constructor.
When loading weights in HDF5 format, returns None.
View source
Creates a function that executes one step of inference.
This method can be overridden to support custom inference logic. This method is called by Model.predict and Model.predict_on_batch.
Typically, this method directly controls tf.function and tf.distribute.Strategy settings, and delegates the actual evaluation logic to Model.predict_step.
This function is cached the first time Model.predict or Model.predict_on_batch is called. The cache is cleared whenever Model.compile is called.
View source
Creates a function that executes one step of evaluation.
This method can be overridden to support custom evaluation logic. This method is called by Model.evaluate and Model.test_on_batch.
Typically, this method directly controls tf.function and tf.distribute.Strategy settings, and delegates the actual evaluation logic to Model.test_step.
This function is cached the first time Model.evaluate or Model.test_on_batch is called. The cache is cleared whenever Model.compile is called.
View source
Creates a function that executes one step of training.
This method can be overridden to support custom training logic. This method is called by Model.fit and Model.train_on_batch.
Typically, this method directly controls tf.function and tf.distribute.Strategy settings, and delegates the actual training logic to Model.train_step.
This function is cached the first time Model.fit or Model.train_on_batch is called. The cache is cleared whenever Model.compile is called.
View source
Generates output predictions for the input samples.
Computation is done in batches. This method is designed for performance in large scale inputs. For small amount of inputs that fit in one batch, directly using __call__ is recommended for faster execution, e.g., model(x), or model(x, training=False) if you have layers such as tf.keras.layers.BatchNormalization that behaves differently during inference. Also, note the fact that test loss is not affected by regularization layers like noise and dropout.
See the discussion of Unpacking behavior for iterator-like inputs for Model.fit. Note that Model.predict uses the same interpretation rules as Model.fit and Model.evaluate, so inputs must be unambiguous for all three methods.
View source
Generates predictions for the input samples from a data generator.
Model.predict now supports generators, so there is no longer any need to use this endpoint.
View source
Returns predictions for a single batch of samples.
View source
The logic for one inference step.
This method can be overridden to support custom inference logic. This method is called by Model.make_predict_function.
This method should contain the mathematical logic for one step of inference. This typically includes the forward pass.
Configuration details for how this logic is run (e.g. tf.function and tf.distribute.Strategy settings), should be left to Model.make_predict_function, which can also be overridden.
View source
Resets the state of all the metrics in the model.
View source
View source
Saves the model to Tensorflow SavedModel or a single HDF5 file.
Please see tf.keras.models.save_model or the Serialization and Saving guide for details.
View source
Saves all layer weights.
Either saves in HDF5 or in TensorFlow format based on the save_format argument.
When saving in HDF5 format, the weight file has:
When saving in TensorFlow format, all objects referenced by the network are saved in the same format as tf.train.Checkpoint, including any Layer instances or Optimizer instances assigned to object attributes. For networks constructed from inputs and outputs using tf.keras.Model(inputs, outputs), Layer instances used by the network are tracked/saved automatically. For user-defined classes which inherit from tf.keras.Model, Layer instances must be assigned to object attributes, typically in the constructor. See the documentation of tf.train.Checkpoint and tf.keras.Model for details.
While the formats are the same, do not mix save_weights and tf.train.Checkpoint. Checkpoints saved by Model.save_weights should be loaded using Model.load_weights. Checkpoints saved using tf.train.Checkpoint.save should be restored using the corresponding tf.train.Checkpoint.restore. Prefer tf.train.Checkpoint over save_weights for training checkpoints.
The TensorFlow format matches objects and variables by starting at a root object, self for save_weights, and greedily matching attribute names. For Model.save this is the Model, and for Checkpoint.save this is the Checkpoint even if the Checkpoint has a model attached. This means saving a tf.keras.Model using save_weights and loading into a tf.train.Checkpoint with a Model attached (or vice versa) will not match the Model's variables. See the guide to training checkpoints for details on the TensorFlow format.
View source
Prints a string summary of the network.
View source
Test the model on a single batch of samples.
View source
The logic for one evaluation step.
This method can be overridden to support custom evaluation logic. This method is called by Model.make_test_function.
This function should contain the mathematical logic for one step of evaluation. This typically includes the forward pass, loss calculation, and metrics updates.
Configuration details for how this logic is run (e.g. tf.function and tf.distribute.Strategy settings), should be left to Model.make_test_function, which can also be overridden.
View source
Returns a JSON string containing the network configuration.
To load a network from a JSON save file, use keras.models.model_from_json(json_string, custom_objects={}).
View source
Returns a yaml string containing the network configuration.
To load a network from a yaml save file, use keras.models.model_from_yaml(yaml_string, custom_objects={}).
custom_objects should be a dictionary mapping the names of custom losses / layers / etc to the corresponding functions / classes.
View source
Runs a single gradient update on a single batch of data.
View source
The logic for one training step.
This method can be overridden to support custom training logic. This method is called by Model.make_train_function.
This method should contain the mathematical logic for one step of training. This typically includes the forward pass, loss calculation, backpropagation, and metric updates.
Configuration details for how this logic is run (e.g. tf.function and tf.distribute.Strategy settings), should be left to Model.make_train_function, which can also be overridden.


Code for model cloning, plus model-related API entries.
class Model: Model groups layers into an object with training and inference features.
class Sequential: Sequential groups a linear stack of layers into a tf.keras.Model.
clone_model(...): Clone any Model instance.
load_model(...): Loads a model saved via model.save().
model_from_config(...): Instantiates a Keras model from its config.
model_from_json(...): Parses a JSON model configuration string and returns a model instance.
model_from_yaml(...): Parses a yaml model configuration file and returns a model instance.
save_model(...): Saves a model as a TensorFlow SavedModel or HDF5 file.


Clone any Model instance.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.models.clone_model
Model cloning is similar to calling a model on new inputs, except that it creates new layers (and thus new weights) instead of sharing the weights of the existing layers.


Loads a model saved via model.save().
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.models.load_model
Note that the model weights may have different scoped names after being loaded. Scoped names include the model/layer names, such as "dense_1/kernel:0". It is recommended that you use the layer properties to access specific variables, e.g. model.get_layer("dense_1").kernel.


Instantiates a Keras model from its config.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.models.model_from_config


Parses a JSON model configuration string and returns a model instance.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.models.model_from_json


Parses a yaml model configuration file and returns a model instance.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.models.model_from_yaml