Standalone evaluation
Evaluate discrete predictions against labels over one or more batches.
TensorFlow · Class
Calculates how often predictions equal labels. It accumulates matches and evaluated values across updates, then returns their ratio.
Accuracy is a stateful Keras metric that compares each prediction with its corresponding label. Each update accumulates the weighted number of matches in total and the number of evaluated values in count; result() returns total divided by count.
tf.keras.metrics.Accuracy(name='accuracy', dtype=None)| Name | Type | Description |
|---|---|---|
name | string, default 'accuracy' | Optional string name of the metric instance. |
dtype | TensorFlow dtype, default None | Optional data type of the metric result. |
A scalar metric result representing the accumulated frequency of predictions matching labels.
import tensorflow as tf
metric = tf.keras.metrics.Accuracy()
metric.update_state(y_true=[1, 2, 3, 4], y_pred=[0, 2, 3, 4])
print(metric.result().numpy()) # 0.75Creates an Accuracy metric, records one batch, and prints the accumulated fraction of matching labels.
Evaluate discrete predictions against labels over one or more batches.
Register the metric for automatic reporting during Keras training and testing.
It measures the frequency with which predictions equal labels. The metric maintains total and count, then result() returns total divided by count.
Follow-up: How would you ignore selected observations? Pass sample_weight with zero for those observations.
Instantiate the metric, call update_state(y_true, y_pred) for each batch, and call result() to read the accumulated value. It can also be supplied as a Metric instance in model.compile().
Follow-up: Why use update_state rather than calculating one batch manually? Stateful updates let the metric aggregate observations across multiple batches.
Its constructor accepts an optional name and dtype. The default name is accuracy, and dtype controls the data type of the metric result.
Follow-up: What weighting behavior applies when no sample_weight is supplied? Every value receives a weight of one.
tf.keras.Metrictf.keras.metrics.BinaryAccuracytf.keras.metrics.CategoricalAccuracytf.keras.metrics.SparseCategoricalAccuracy