tf.keras.metrics.get     View source on GitHub    Retrieves a Keras metric as a function/Metric class instance.  View aliases  Main aliases
tf.metrics.get Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.get
tf.keras.metrics.get(
    identifier
)
 The identifier may be the string name of a metric function or class.
metric = tf.keras.metrics.get("categorical_crossentropy")
type(metric)
<class 'function'>
metric = tf.keras.metrics.get("CategoricalCrossentropy")
type(metric)
<class '...tensorflow.python.keras.metrics.CategoricalCrossentropy'>
 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
identifier = {"class_name": "CategoricalCrossentropy",
              "config": {"from_logits": True} }
metric = tf.keras.metrics.get(identifier)
type(metric)
<class '...tensorflow.python.keras.metrics.CategoricalCrossentropy'>

 Arguments
  identifier   A metric identifier. One of None or string name of a metric function/class or metric configuration dictionary or a metric function or a metric class instance

 Returns   A Keras metric as a function/ Metric class instance.

 Raises
  ValueError   If identifier cannot be interpreted.


tf.keras.metrics.Hinge     View source on GitHub    Computes the hinge metric between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.Hinge Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Hinge
tf.keras.metrics.Hinge(
    name='hinge', dtype=None
)
 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.

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.Hinge()
m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
m.result().numpy()
1.3

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
               sample_weight=[1, 0])
m.result().numpy()
1.1
 Usage with compile() API: model.compile(optimizer='sgd', loss='mse', metrics=[tf.keras.metrics.Hinge()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.KLDivergence     View source on GitHub    Computes Kullback-Leibler divergence metric between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.KLDivergence Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.KLDivergence
tf.keras.metrics.KLDivergence(
    name='kullback_leibler_divergence', dtype=None
)
 metric = y_true * log(y_true / y_pred)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.KLDivergence()
m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
m.result().numpy()
0.45814306

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
               sample_weight=[1, 0])
m.result().numpy()
0.9162892
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.KLDivergence()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.LogCoshError     View source on GitHub    Computes the logarithm of the hyperbolic cosine of the prediction error. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.LogCoshError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.LogCoshError
tf.keras.metrics.LogCoshError(
    name='logcosh', dtype=None
)
 logcosh = log((exp(x) + exp(-x))/2), where x is the error (y_pred - y_true)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.LogCoshError()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
0.10844523

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
0.21689045
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.LogCoshError()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.Mean     View source on GitHub    Computes the (weighted) mean of the given values. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.Mean Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Mean
tf.keras.metrics.Mean(
    name='mean', dtype=None
)
 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.

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.Mean()
m.update_state([1, 3, 5, 7])
m.result().numpy()
4.0
m.reset_states()
m.update_state([1, 3, 5, 7], sample_weight=[1, 1, 0, 0])
m.result().numpy()
2.0
 Usage with compile() API: model.add_metric(tf.keras.metrics.Mean(name='mean_1')(outputs))
model.compile(optimizer='sgd', loss='mse')
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    values, sample_weight=None
)
 Accumulates statistics for computing the metric.

 Args
  values   Per-example value.
  sample_weight   Optional weighting of each example. Defaults to 1.

 Returns   Update op.


tf.keras.metrics.MeanAbsoluteError     View source on GitHub    Computes the mean absolute error between the labels and predictions. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanAbsoluteError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanAbsoluteError
tf.keras.metrics.MeanAbsoluteError(
    name='mean_absolute_error', dtype=None
)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.MeanAbsoluteError()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
0.25

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
0.5
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.MeanAbsoluteError()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.MeanAbsolutePercentageError     View source on GitHub    Computes the mean absolute percentage error between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanAbsolutePercentageError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanAbsolutePercentageError
tf.keras.metrics.MeanAbsolutePercentageError(
    name='mean_absolute_percentage_error', dtype=None
)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.MeanAbsolutePercentageError()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
250000000.0

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
500000000.0
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.MeanAbsolutePercentageError()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.MeanIoU     View source on GitHub    Computes the mean Intersection-Over-Union metric. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanIoU Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanIoU
tf.keras.metrics.MeanIoU(
    num_classes, name=None, dtype=None
)
 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.

 Args
  num_classes   The possible number of labels the prediction task can have. This value must be provided, since a confusion matrix of dimension = [num_classes, num_classes] will be allocated.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
# cm = [[1, 1],
#        [1, 1]]
# sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
# iou = true_positives / (sum_row + sum_col - true_positives))
# result = (1 / (2 + 2 - 1) + 1 / (2 + 2 - 1)) / 2 = 0.33
m = tf.keras.metrics.MeanIoU(num_classes=2)
m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
m.result().numpy()
0.33333334

m.reset_states()
m.update_state([0, 0, 1, 1], [0, 1, 0, 1],
               sample_weight=[0.3, 0.3, 0.3, 0.1])
m.result().numpy()
0.23809525
 Usage with compile() API: model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.MeanIoU(num_classes=2)])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Compute the mean intersection-over-union via the confusion matrix. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates the confusion matrix statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.MeanRelativeError     View source on GitHub    Computes the mean relative error by normalizing with the given values. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanRelativeError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanRelativeError
tf.keras.metrics.MeanRelativeError(
    normalizer, name=None, dtype=None
)
 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.

 Args
  normalizer   The normalizer values with same shape as predictions.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.MeanRelativeError(normalizer=[1, 3, 2, 3])
m.update_state([1, 3, 2, 3], [2, 4, 6, 8])

# metric = mean(|y_pred - y_true| / normalizer)
#        = mean([1, 1, 4, 5] / [1, 3, 2, 3]) = mean([1, 1/3, 2, 5/3])
#        = 5/4 = 1.25
m.result().numpy()
1.25
 Usage with compile() API: model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.MeanRelativeError(normalizer=[1, 3])])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.MeanSquaredError     View source on GitHub    Computes the mean squared error between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanSquaredError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanSquaredError
tf.keras.metrics.MeanSquaredError(
    name='mean_squared_error', dtype=None
)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.MeanSquaredError()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
0.25

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
0.5
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.MeanSquaredError()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.MeanSquaredLogarithmicError     View source on GitHub    Computes the mean squared logarithmic error between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanSquaredLogarithmicError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanSquaredLogarithmicError
tf.keras.metrics.MeanSquaredLogarithmicError(
    name='mean_squared_logarithmic_error', dtype=None
)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.MeanSquaredLogarithmicError()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
0.12011322

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
0.24022643
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.MeanSquaredLogarithmicError()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.MeanTensor     View source on GitHub    Computes the element-wise (weighted) mean of the given tensors. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.MeanTensor Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.MeanTensor
tf.keras.metrics.MeanTensor(
    name='mean_tensor', dtype=None
)
 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.

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.MeanTensor()
m.update_state([0, 1, 2, 3])
m.update_state([4, 5, 6, 7])
m.result().numpy()
array([2., 3., 4., 5.], dtype=float32)

m.update_state([12, 10, 8, 6], sample_weight= [0, 0.2, 0.5, 1])
m.result().numpy()
array([2.       , 3.6363635, 4.8      , 5.3333335], dtype=float32)

 Attributes
  count

  total
   Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    values, sample_weight=None
)
 Accumulates statistics for computing the element-wise mean.

 Args
  values   Per-example value.
  sample_weight   Optional weighting of each example. Defaults to 1.

 Returns   Update op.


tf.keras.metrics.Metric     View source on GitHub    Encapsulates metric logic and state. Inherits From: Layer, Module  View aliases  Main aliases
tf.metrics.Metric Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Metric
tf.keras.metrics.Metric(
    name=None, dtype=None, **kwargs
)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.
  **kwargs   Additional layer keywords arguments.    Standalone usage: m = SomeMetric(...)
for input in ...:
  m.update_state(input)
print('Final result: ', m.result().numpy())
 Usage with compile() API: model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.keras.optimizers.RMSprop(0.01),
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=[tf.keras.metrics.CategoricalAccuracy()])

data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))

dataset = tf.data.Dataset.from_tensor_slices((data, labels))
dataset = dataset.batch(32)

model.fit(dataset, epochs=10)
 To be implemented by subclasses:
__init__(): All state variables should be created in this method by calling self.add_weight() like: self.var = self.add_weight(...)

update_state(): Has all updates to the state variables like: self.var.assign_add(...).
result(): Computes and returns a value for the metric from the state variables.  Example subclass implementation: class BinaryTruePositives(tf.keras.metrics.Metric):

  def __init__(self, name='binary_true_positives', **kwargs):
    super(BinaryTruePositives, self).__init__(name=name, **kwargs)
    self.true_positives = self.add_weight(name='tp', initializer='zeros')

  def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = tf.cast(y_true, tf.bool)
    y_pred = tf.cast(y_pred, tf.bool)

    values = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
    values = tf.cast(values, self.dtype)
    if sample_weight is not None:
      sample_weight = tf.cast(sample_weight, self.dtype)
      sample_weight = tf.broadcast_to(sample_weight, values.shape)
      values = tf.multiply(values, sample_weight)
    self.true_positives.assign_add(tf.reduce_sum(values))

  def result(self):
    return self.true_positives
 Methods add_weight View source
add_weight(
    name, shape=(), aggregation=tf.compat.v1.VariableAggregation.SUM,
    synchronization=tf.VariableSynchronization.ON_READ, initializer=None, dtype=None
)
 Adds state variable. Only for use by subclasses. reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
@abc.abstractmethod
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
@abc.abstractmethod
update_state(
    *args, **kwargs
)
 Accumulates statistics for the metric.
Note: This function is executed as a graph function in graph mode. This means: a) Operations on the same resource are executed in textual order. This should make it easier to do things like add the updated value of a variable to another, for example. b) You don't need to worry about collecting the update ops to execute. All update ops added to the graph by this function will be executed. As a result, code should generally work the same way with graph or eager execution.

 Args
  *args

  **kwargs   A mini-batch of inputs to the Metric.


tf.keras.metrics.Poisson     View source on GitHub    Computes the Poisson metric between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.Poisson Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Poisson
tf.keras.metrics.Poisson(
    name='poisson', dtype=None
)
 metric = y_pred - y_true * log(y_pred)

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.Poisson()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
0.49999997

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
0.99999994
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.Poisson()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.Precision     View source on GitHub    Computes the precision of the predictions with respect to the labels. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.Precision Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Precision
tf.keras.metrics.Precision(
    thresholds=None, top_k=None, class_id=None, name=None, dtype=None
)
 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.

 Args
  thresholds   (Optional) A float value or a python list/tuple of float threshold values in [0, 1]. A threshold is compared with prediction values to determine the truth value of predictions (i.e., above the threshold is true, below is false). One metric value is generated for each threshold value. If neither thresholds nor top_k are set, the default is to calculate precision with thresholds=0.5.
  top_k   (Optional) Unset by default. An int value specifying the top-k predictions to consider when calculating precision.
  class_id   (Optional) Integer class ID for which we want binary metrics. This must be in the half-open interval [0, num_classes), where num_classes is the last dimension of predictions.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.Precision()
m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
m.result().numpy()
0.6666667

m.reset_states()
m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
m.result().numpy()
1.0

# With top_k=2, it will calculate precision over y_true[:2] and y_pred[:2]
m = tf.keras.metrics.Precision(top_k=2)
m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
m.result().numpy()
0.0

# With top_k=4, it will calculate precision over y_true[:4] and y_pred[:4]
m = tf.keras.metrics.Precision(top_k=4)
m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
m.result().numpy()
0.5
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.Precision()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates true positive and false positive statistics.

 Args
  y_true   The ground truth values, with the same dimensions as y_pred. Will be cast to bool.
  y_pred   The predicted values. Each element must be in the range [0, 1].
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.PrecisionAtRecall Computes best precision where recall is >= specified value. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.PrecisionAtRecall Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.PrecisionAtRecall
tf.keras.metrics.PrecisionAtRecall(
    recall, num_thresholds=200, name=None, dtype=None
)
 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.

 Args
  recall   A scalar value in range [0, 1].
  num_thresholds   (Optional) Defaults to 200. The number of thresholds to use for matching the given recall.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.PrecisionAtRecall(0.5)
m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
m.result().numpy()
0.5

m.reset_states()
m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
               sample_weight=[2, 2, 2, 1, 1])
m.result().numpy()
0.33333333
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.PrecisionAtRecall(recall=0.8)])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates confusion matrix statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.Recall     View source on GitHub    Computes the recall of the predictions with respect to the labels. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.Recall Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Recall
tf.keras.metrics.Recall(
    thresholds=None, top_k=None, class_id=None, name=None, dtype=None
)
 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.

 Args
  thresholds   (Optional) A float value or a python list/tuple of float threshold values in [0, 1]. A threshold is compared with prediction values to determine the truth value of predictions (i.e., above the threshold is true, below is false). One metric value is generated for each threshold value. If neither thresholds nor top_k are set, the default is to calculate recall with thresholds=0.5.
  top_k   (Optional) Unset by default. An int value specifying the top-k predictions to consider when calculating recall.
  class_id   (Optional) Integer class ID for which we want binary metrics. This must be in the half-open interval [0, num_classes), where num_classes is the last dimension of predictions.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.Recall()
m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
m.result().numpy()
0.6666667

m.reset_states()
m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
m.result().numpy()
1.0
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.Recall()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates true positive and false negative statistics.

 Args
  y_true   The ground truth values, with the same dimensions as y_pred. Will be cast to bool.
  y_pred   The predicted values. Each element must be in the range [0, 1].
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.RecallAtPrecision Computes best recall where precision is >= specified value. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.RecallAtPrecision Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.RecallAtPrecision
tf.keras.metrics.RecallAtPrecision(
    precision, num_thresholds=200, name=None, dtype=None
)
 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.

 Args
  precision   A scalar value in range [0, 1].
  num_thresholds   (Optional) Defaults to 200. The number of thresholds to use for matching the given precision.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.RecallAtPrecision(0.8)
m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9])
m.result().numpy()
0.5

m.reset_states()
m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9],
               sample_weight=[1, 0, 0, 1])
m.result().numpy()
1.0
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.RecallAtPrecision(precision=0.8)])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates confusion matrix statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.RootMeanSquaredError     View source on GitHub    Computes root mean squared error metric between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.RootMeanSquaredError Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.RootMeanSquaredError
tf.keras.metrics.RootMeanSquaredError(
    name='root_mean_squared_error', dtype=None
)
 Standalone usage:
m = tf.keras.metrics.RootMeanSquaredError()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
m.result().numpy()
0.5

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
               sample_weight=[1, 0])
m.result().numpy()
0.70710677
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.RootMeanSquaredError()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates root mean squared error statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.SensitivityAtSpecificity     View source on GitHub    Computes best sensitivity where specificity is >= specified value. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.SensitivityAtSpecificity Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.SensitivityAtSpecificity
tf.keras.metrics.SensitivityAtSpecificity(
    specificity, num_thresholds=200, name=None, dtype=None
)
 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.

 Args
  specificity   A scalar value in range [0, 1].
  num_thresholds   (Optional) Defaults to 200. The number of thresholds to use for matching the given specificity.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.SensitivityAtSpecificity(0.5)
m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
m.result().numpy()
0.5

m.reset_states()
m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
               sample_weight=[1, 1, 2, 2, 1])
m.result().numpy()
0.333333
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.SensitivityAtSpecificity()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates confusion matrix statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.serialize     View source on GitHub    Serializes metric function or Metric instance.  View aliases  Main aliases
tf.metrics.serialize Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.serialize
tf.keras.metrics.serialize(
    metric
)

 Arguments
  metric   A Keras Metric instance or a metric function.

 Returns   Metric configuration dictionary.


tf.keras.metrics.SparseCategoricalAccuracy     View source on GitHub    Calculates how often predictions matches integer labels. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.SparseCategoricalAccuracy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.SparseCategoricalAccuracy
tf.keras.metrics.SparseCategoricalAccuracy(
    name='sparse_categorical_accuracy', dtype=None
)
 acc = np.dot(sample_weight, np.equal(y_true, np.argmax(y_pred, axis=1))
 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.

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.SparseCategoricalAccuracy()
m.update_state([[2], [1]], [[0.1, 0.6, 0.3], [0.05, 0.95, 0]])
m.result().numpy()
0.5

m.reset_states()
m.update_state([[2], [1]], [[0.1, 0.6, 0.3], [0.05, 0.95, 0]],
               sample_weight=[0.7, 0.3])
m.result().numpy()
0.3
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.SparseCategoricalCrossentropy     View source on GitHub    Computes the crossentropy metric between the labels and predictions. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.SparseCategoricalCrossentropy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.SparseCategoricalCrossentropy
tf.keras.metrics.SparseCategoricalCrossentropy(
    name='sparse_categorical_crossentropy', dtype=None, from_logits=False,
    axis=-1
)
 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].

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.
  from_logits   (Optional) Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution.
  axis   (Optional) Defaults to -1. The dimension along which the metric is computed.    Standalone usage:
# y_true = one_hot(y_true) = [[0, 1, 0], [0, 0, 1]]
# logits = log(y_pred)
# softmax = exp(logits) / sum(exp(logits), axis=-1)
# softmax = [[0.05, 0.95, EPSILON], [0.1, 0.8, 0.1]]
# xent = -sum(y * log(softmax), 1)
# log(softmax) = [[-2.9957, -0.0513, -16.1181],
#                [-2.3026, -0.2231, -2.3026]]
# y_true * log(softmax) = [[0, -0.0513, 0], [0, 0, -2.3026]]
# xent = [0.0513, 2.3026]
# Reduced xent = (0.0513 + 2.3026) / 2
m = tf.keras.metrics.SparseCategoricalCrossentropy()
m.update_state([1, 2],
               [[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
m.result().numpy()
1.1769392

m.reset_states()
m.update_state([1, 2],
               [[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
               sample_weight=tf.constant([0.3, 0.7]))
m.result().numpy()
1.6271976
 Usage with compile() API: model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.SparseCategoricalCrossentropy()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.SparseTopKCategoricalAccuracy     View source on GitHub    Computes how often integer targets are in the top K predictions. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.SparseTopKCategoricalAccuracy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.SparseTopKCategoricalAccuracy
tf.keras.metrics.SparseTopKCategoricalAccuracy(
    k=5, name='sparse_top_k_categorical_accuracy', dtype=None
)

 Args
  k   (Optional) Number of top elements to look at for computing accuracy. Defaults to 5.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.SparseTopKCategoricalAccuracy(k=1)
m.update_state([2, 1], [[0.1, 0.9, 0.8], [0.05, 0.95, 0]])
m.result().numpy()
0.5

m.reset_states()
m.update_state([2, 1], [[0.1, 0.9, 0.8], [0.05, 0.95, 0]],
               sample_weight=[0.7, 0.3])
m.result().numpy()
0.3
 Usage with compile() API: model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.SparseTopKCategoricalAccuracy()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.sparse_categorical_accuracy     View source on GitHub    Calculates how often predictions matches integer labels.  View aliases  Main aliases
tf.metrics.sparse_categorical_accuracy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.sparse_categorical_accuracy
tf.keras.metrics.sparse_categorical_accuracy(
    y_true, y_pred
)
 Standalone usage:
y_true = [2, 1]
y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
m = tf.keras.metrics.sparse_categorical_accuracy(y_true, y_pred)
assert m.shape == (2,)
m.numpy()
array([0., 1.], dtype=float32)
 You can provide logits of classes as y_pred, since argmax of logits and probabilities are same.

 Args
  y_true   Integer ground truth values.
  y_pred   The prediction values.

 Returns   Sparse categorical accuracy values.


tf.keras.metrics.sparse_top_k_categorical_accuracy     View source on GitHub    Computes how often integer targets are in the top K predictions.  View aliases  Main aliases
tf.metrics.sparse_top_k_categorical_accuracy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.sparse_top_k_categorical_accuracy
tf.keras.metrics.sparse_top_k_categorical_accuracy(
    y_true, y_pred, k=5
)
 Standalone usage:
y_true = [2, 1]
y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
m = tf.keras.metrics.sparse_top_k_categorical_accuracy(
    y_true, y_pred, k=3)
assert m.shape == (2,)
m.numpy()
array([1., 1.], dtype=float32)

 Args
  y_true   tensor of true targets.
  y_pred   tensor of predicted targets.
  k   (Optional) Number of top elements to look at for computing accuracy. Defaults to 5.

 Returns   Sparse top K categorical accuracy value.


tf.keras.metrics.SpecificityAtSensitivity     View source on GitHub    Computes best specificity where sensitivity is >= specified value. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.SpecificityAtSensitivity Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.SpecificityAtSensitivity
tf.keras.metrics.SpecificityAtSensitivity(
    sensitivity, num_thresholds=200, name=None, dtype=None
)
 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.

 Args
  sensitivity   A scalar value in range [0, 1].
  num_thresholds   (Optional) Defaults to 200. The number of thresholds to use for matching the given sensitivity.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.SpecificityAtSensitivity(0.5)
m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
m.result().numpy()
0.66666667

m.reset_states()
m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
               sample_weight=[1, 1, 2, 2, 2])
m.result().numpy()
0.5
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.SpecificityAtSensitivity()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates confusion matrix statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.SquaredHinge     View source on GitHub    Computes the squared hinge metric between y_true and y_pred. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.SquaredHinge Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.SquaredHinge
tf.keras.metrics.SquaredHinge(
    name='squared_hinge', dtype=None
)
 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.

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.SquaredHinge()
m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
m.result().numpy()
1.86

m.reset_states()
m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
               sample_weight=[1, 0])
m.result().numpy()
1.46
 Usage with compile() API: model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[tf.keras.metrics.SquaredHinge()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.Sum     View source on GitHub    Computes the (weighted) sum of the given values. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.Sum Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.Sum
tf.keras.metrics.Sum(
    name='sum', dtype=None
)
 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.

 Args
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.Sum()
m.update_state([1, 3, 5, 7])
m.result().numpy()
16.0
 Usage with compile() API: model.add_metric(tf.keras.metrics.Sum(name='sum_1')(outputs))
model.compile(optimizer='sgd', loss='mse')
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    values, sample_weight=None
)
 Accumulates statistics for computing the metric.

 Args
  values   Per-example value.
  sample_weight   Optional weighting of each example. Defaults to 1.

 Returns   Update op.


tf.keras.metrics.TopKCategoricalAccuracy     View source on GitHub    Computes how often targets are in the top K predictions. Inherits From: Mean, Metric, Layer, Module  View aliases  Main aliases
tf.metrics.TopKCategoricalAccuracy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.TopKCategoricalAccuracy
tf.keras.metrics.TopKCategoricalAccuracy(
    k=5, name='top_k_categorical_accuracy', dtype=None
)

 Args
  k   (Optional) Number of top elements to look at for computing accuracy. Defaults to 5.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.TopKCategoricalAccuracy(k=1)
m.update_state([[0, 0, 1], [0, 1, 0]],
               [[0.1, 0.9, 0.8], [0.05, 0.95, 0]])
m.result().numpy()
0.5

m.reset_states()
m.update_state([[0, 0, 1], [0, 1, 0]],
               [[0.1, 0.9, 0.8], [0.05, 0.95, 0]],
               sample_weight=[0.7, 0.3])
m.result().numpy()
0.3
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.TopKCategoricalAccuracy()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates metric statistics. y_true and y_pred should have the same shape.

 Args
  y_true   Ground truth values. shape = [batch_size, d0, .. dN].
  y_pred   The predicted values. shape = [batch_size, d0, .. dN].
  sample_weight   Optional sample_weight acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the metric for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each metric element of y_pred is scaled by the corresponding value of sample_weight. (Note on dN-1: all metric functions reduce by 1 dimension, usually the last axis (-1)).

 Returns   Update op.


tf.keras.metrics.top_k_categorical_accuracy     View source on GitHub    Computes how often targets are in the top K predictions.  View aliases  Main aliases
tf.metrics.top_k_categorical_accuracy Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.top_k_categorical_accuracy
tf.keras.metrics.top_k_categorical_accuracy(
    y_true, y_pred, k=5
)
 Standalone usage:
y_true = [[0, 0, 1], [0, 1, 0]]
y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
m = tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)
assert m.shape == (2,)
m.numpy()
array([1., 1.], dtype=float32)

 Args
  y_true   The ground truth values.
  y_pred   The prediction values.
  k   (Optional) Number of top elements to look at for computing accuracy. Defaults to 5.

 Returns   Top K categorical accuracy value.


tf.keras.metrics.TrueNegatives     View source on GitHub    Calculates the number of true negatives. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.TrueNegatives Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.TrueNegatives
tf.keras.metrics.TrueNegatives(
    thresholds=None, name=None, dtype=None
)
 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.

 Args
  thresholds   (Optional) Defaults to 0.5. A float value or a python list/tuple of float threshold values in [0, 1]. A threshold is compared with prediction values to determine the truth value of predictions (i.e., above the threshold is true, below is false). One metric value is generated for each threshold value.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.TrueNegatives()
m.update_state([0, 1, 0, 0], [1, 1, 0, 0])
m.result().numpy()
2.0

m.reset_states()
m.update_state([0, 1, 0, 0], [1, 1, 0, 0], sample_weight=[0, 0, 1, 0])
m.result().numpy()
1.0
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.TrueNegatives()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates the metric statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


tf.keras.metrics.TruePositives     View source on GitHub    Calculates the number of true positives. Inherits From: Metric, Layer, Module  View aliases  Main aliases
tf.metrics.TruePositives Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.metrics.TruePositives
tf.keras.metrics.TruePositives(
    thresholds=None, name=None, dtype=None
)
 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.

 Args
  thresholds   (Optional) Defaults to 0.5. A float value or a python list/tuple of float threshold values in [0, 1]. A threshold is compared with prediction values to determine the truth value of predictions (i.e., above the threshold is true, below is false). One metric value is generated for each threshold value.
  name   (Optional) string name of the metric instance.
  dtype   (Optional) data type of the metric result.    Standalone usage:
m = tf.keras.metrics.TruePositives()
m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
m.result().numpy()
2.0

m.reset_states()
m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
m.result().numpy()
1.0
 Usage with compile() API: model.compile(optimizer='sgd',
              loss='mse',
              metrics=[tf.keras.metrics.TruePositives()])
 Methods reset_states View source
reset_states()
 Resets all of the metric state variables. This function is called between epochs/steps, when a metric is evaluated during training. result View source
result()
 Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the metric value using the state variables. update_state View source
update_state(
    y_true, y_pred, sample_weight=None
)
 Accumulates the metric statistics.

 Args
  y_true   The ground truth values.
  y_pred   The predicted values.
  sample_weight   Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.

 Returns   Update op.


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


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


tf.keras.mixed_precision.experimental.get_layer_policy Returns the dtype policy of a layer.
tf.keras.mixed_precision.experimental.get_layer_policy(
    layer
)
 Warning: This function is deprecated. Use tf.keras.layers.Layer.dtype_policy instead.

 Args
  layer   A tf.keras.layers.Layer.

 Returns   The tf.keras.mixed_precision.Policy of the layer.


tf.keras.mixed_precision.experimental.LossScaleOptimizer     View source on GitHub    An deprecated optimizer that applies loss scaling. Inherits From: LossScaleOptimizer, Optimizer  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.mixed_precision.experimental.LossScaleOptimizer
tf.keras.mixed_precision.experimental.LossScaleOptimizer(
    optimizer, loss_scale
)
 Warning: This class is deprecated and will be removed in TensorFlow 2.5. Please use the non-experimental class tf.keras.mixed_precision.LossScaleOptimizer instead. 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.
# In all of the the examples below, `opt1` and `opt2` are identical
opt1 = tf.keras.mixed_precision.experimental.LossScaleOptimizer(
    tf.keras.optimizers.SGD(), loss_scale='dynamic')
opt2 = tf.keras.mixed_precision.LossScaleOptimizer(
    tf.keras.optimizers.SGD())
assert opt1.get_config() == opt2.get_config()

opt1 = tf.keras.mixed_precision.experimental.LossScaleOptimizer(
    tf.keras.optimizers.SGD(), loss_scale=123)
# dynamic=False indicates to use fixed loss scaling. initial_scale=123
# refers to the initial loss scale, which is the single fixed loss scale
# when dynamic=False.
opt2 = tf.keras.mixed_precision.LossScaleOptimizer(
    tf.keras.optimizers.SGD(), dynamic=False, initial_scale=123)
assert opt1.get_config() == opt2.get_config()

loss_scale = tf.compat.v1.mixed_precision.experimental.DynamicLossScale(
    initial_loss_scale=2048, increment_period=500)
opt1 = tf.keras.mixed_precision.experimental.LossScaleOptimizer(
    tf.keras.optimizers.SGD(), loss_scale=loss_scale)
opt2 = tf.keras.mixed_precision.LossScaleOptimizer(
    tf.keras.optimizers.SGD(), initial_scale=2048,
    dynamic_growth_steps=500)
assert opt1.get_config() == opt2.get_config()
 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.
opt1 = tf.keras.mixed_precision.experimental.LossScaleOptimizer(
    tf.keras.optimizers.SGD(), loss_scale='dynamic')
# The experimental class subclasses the non-experimental class
isinstance(opt1, tf.keras.mixed_precision.LossScaleOptimizer)
True
opt2 = tf.keras.mixed_precision.LossScaleOptimizer(
    tf.keras.optimizers.SGD())
# The non-experimental class does NOT subclass the experimental class.
isinstance(opt2, tf.keras.mixed_precision.experimental.LossScaleOptimizer)
False

 Args
  optimizer   The Optimizer instance to wrap.
  loss_scale   The loss scale to scale the loss and gradients. This can either be an int/float to use a fixed loss scale, the string "dynamic" to use dynamic loss scaling, or an instance of a LossScale. The string "dynamic" equivalent to passing DynamicLossScale(), and passing an int/float is equivalent to passing a FixedLossScale with the given loss scale. If a DynamicLossScale is passed, DynamicLossScale.multiplier must be 2 (the default).

 Raises
  ValueError   in case of any invalid argument.

 Attributes
  dynamic   Bool indicating whether dynamic loss scaling is used.
  dynamic_counter   The number of steps since the loss scale was last increased or decreased. 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.

  dynamic_growth_steps   The number of steps it takes to increase the loss scale. This is None if LossScaleOptimizer.dynamic is False. Every dynamic_growth_steps consecutive steps with finite gradients, the loss scale is increased.

  initial_scale   The initial loss scale. If LossScaleOptimizer.dynamic is False, this is the same number as LossScaleOptimizer.loss_scale, as the loss scale never changes.

  inner_optimizer   The optimizer that this LossScaleOptimizer is wrapping.
  loss_scale   The current loss scale as a float32 scalar tensor.    Methods get_scaled_loss View source
get_scaled_loss(
    loss
)
 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.

 Args
  loss   The loss, which will be multiplied by the loss scale. Can either be a tensor or a callable returning a tensor.

 Returns   loss multiplied by LossScaleOptimizer.loss_scale.
 get_unscaled_gradients View source
get_unscaled_gradients(
    grads
)
 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.

 Args
  grads   A list of tensors, each which will be divided by the loss scale. Can have None values, which are ignored.

 Returns   A new list the same size as grads, where every non-None value in grads is divided by LossScaleOptimizer.loss_scale.


tf.keras.mixed_precision.experimental.Policy     View source on GitHub    A deprecated dtype policy for a Keras layer. Inherits From: Policy
tf.keras.mixed_precision.experimental.Policy(
    name, loss_scale='auto'
)
 Warning: This class is now deprecated and will be removed soon. Please use the non-experimental class tf.keras.mixed_precision.Policy instead. 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.

 Args
  name   A string. Can be one of the following values:  Any dtype name, such as 'float32' or 'float64'. Both the variable and compute dtypes will be that dtype. 'mixed_float16' or 'mixed_bfloat16': The compute dtype is float16 or bfloat16, while the variable dtype is float32. With 'mixed_float16', a dynamic loss scale is used. These policies are used for mixed precision training.

  loss_scale   A tf.compat.v1.mixed_precision.LossScale, an int (which uses a FixedLossScale), the string "dynamic" (which uses a DynamicLossScale), or None (which uses no loss scale). Defaults to "auto". In the "auto" case: 1) if name is "mixed_float16", then use loss_scale="dynamic". 2) otherwise, do not use a loss scale. Only tf.keras.Models, not layers, use the loss scale, and it is only used during Model.fit, Model.train_on_batch, and other similar methods.

 Attributes
  compute_dtype   The compute dtype of this policy. 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.

  loss_scale   Returns the loss scale of this Policy.
  name   Returns the name of this policy.
  variable_dtype   The variable dtype of this policy. 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.
   Methods from_config View source
@classmethod
from_config(
    config, custom_objects=None
)
 get_config View source
get_config()


tf.keras.mixed_precision.global_policy Returns the global dtype policy.  View aliases  Main aliases
tf.keras.mixed_precision.experimental.global_policy
tf.keras.mixed_precision.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).
tf.keras.mixed_precision.global_policy()
<Policy "float32">
tf.keras.layers.Dense(10).dtype_policy  # Defaults to the global policy
<Policy "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.

 Returns   The global Policy.


tf.keras.mixed_precision.LossScaleOptimizer An optimizer that applies loss scaling to prevent numeric underflow. Inherits From: Optimizer  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.keras.mixed_precision.LossScaleOptimizer
tf.keras.mixed_precision.LossScaleOptimizer(
    inner_optimizer, dynamic=True, initial_scale=None, dynamic_growth_steps=None
)
 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:
opt = tf.keras.optimizers.SGD(0.25)
opt = tf.keras.mixed_precision.LossScaleOptimizer(opt)
var = tf.Variable(1.)
loss_fn = lambda: var ** 2
# 'minimize' applies loss scaling and updates the loss sale.
opt.minimize(loss_fn, var_list=var)
var.numpy()
0.5
 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:
with tf.GradientTape() as tape:
  loss = loss_fn()
  scaled_loss = opt.get_scaled_loss(loss)
scaled_grad = tape.gradient(scaled_loss, var)
(grad,) = opt.get_unscaled_gradients([scaled_grad])
opt.apply_gradients([(grad, var)])  # Loss scale is updated here
var.numpy()
0.25
 Warning: If you forget to call get_scaled_loss or get_unscaled_gradients (or both) when using a tf.GradientTape, the model will likely converge to a worse quality. Please make sure you call each function exactly once. 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.

 Args
  inner_optimizer   The tf.keras.optimizers.Optimizer instance to wrap.
  dynamic   Bool indicating whether dynamic loss scaling is used. Defaults to True. If True, the loss scale will be dynamically updated over time using an algorithm that keeps the loss scale at approximately its optimal value. If False, a single fixed loss scale is used and initial_scale must be specified, which is used as the loss scale. Recommended to keep as True, as choosing a fixed loss scale can be tricky. Currently, there is a small performance overhead to dynamic loss scaling compared to fixed loss scaling.
  initial_scale   The initial loss scale. If dynamic is True, this defaults to 2 ** 15. If dynamic is False, this must be specified and acts as the sole loss scale, as the loss scale does not change over time. When dynamic loss scaling is used, is better for this to be a very high number, because a loss scale that is too high gets lowered far more quickly than a loss scale that is too low gets raised.
  dynamic_growth_steps   With dynamic loss scaling, every dynamic_growth_steps steps with finite gradients, the loss scale is doubled. Defaults to 2000. If a nonfinite gradient is encountered, the count is reset back to zero, gradients are skipped that step, and the loss scale is halved. The count can be queried with LossScaleOptimizer.dynamic_counter. This argument can only be specified if dynamic is True.    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 Hyperparameters can be accessed and set on the LossScaleOptimizer, which will be delegated to the wrapped optimizer.
opt = tf.keras.optimizers.Adam(beta_1=0.8, epsilon=1e-5)
opt = tf.keras.mixed_precision.LossScaleOptimizer(opt)
opt.beta_1  # Equivalent to `opt.inner_optimizer.beta_1`
0.8
opt.beta_1 = 0.7  # Equivalent to `opt.inner_optimizer.beta_1 = 0.7`
opt.beta_1
0.7
opt.inner_optimizer.beta_1
0.7
 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.
opt.inner_optimizer.epsilon
1e-5
opt.epsilon
Traceback (most recent call last):

AttributeError: 'LossScaleOptimizer' object has no attribute 'epsilon'
opt.epsilon = 1e-4  # This does NOT set epsilon on `opt.inner_optimizer`
opt.inner_optimizer.epsilon
1e-5
 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.

 Raises
  ValueError   in case of any invalid argument.

 Attributes
  dynamic   Bool indicating whether dynamic loss scaling is used.
  dynamic_counter   The number of steps since the loss scale was last increased or decreased. 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.

  dynamic_growth_steps   The number of steps it takes to increase the loss scale. This is None if LossScaleOptimizer.dynamic is False. Every dynamic_growth_steps consecutive steps with finite gradients, the loss scale is increased.

  initial_scale   The initial loss scale. If LossScaleOptimizer.dynamic is False, this is the same number as LossScaleOptimizer.loss_scale, as the loss scale never changes.

  inner_optimizer   The optimizer that this LossScaleOptimizer is wrapping.
  loss_scale   The current loss scale as a float32 scalar tensor.    Methods get_scaled_loss View source
get_scaled_loss(
    loss
)
 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.

 Args
  loss   The loss, which will be multiplied by the loss scale. Can either be a tensor or a callable returning a tensor.

 Returns   loss multiplied by LossScaleOptimizer.loss_scale.
 get_unscaled_gradients View source
get_unscaled_gradients(
    grads
)
 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.

 Args
  grads   A list of tensors, each which will be divided by the loss scale. Can have None values, which are ignored.

 Returns   A new list the same size as grads, where every non-None value in grads is divided by LossScaleOptimizer.loss_scale.