Track element-wise correctness
Monitoring exact-value predictions during training or evaluation.
Keras · Class
keras.metrics.Accuracy measures how often predictions exactly equal their corresponding labels. It maintains running match and sample counts and returns their ratio.
Accuracy compares each predicted value with its corresponding true label and reports the fraction of matches. It maintains running total and count state, so repeated update_state() calls accumulate observations until reset_state() is used.
keras.metrics.Accuracy(name="accuracy", dtype=None)| Name | Type | Description |
|---|---|---|
name | string | Metric instance name; defaults to "accuracy". |
dtype | string or data type | Optional data type used by the metric. |
A scalar representing the frequency with which predictions equal labels.
import keras
metric = keras.metrics.Accuracy()
metric.update_state([1, 2, 3], [1, 0, 3])
print(float(metric.result())) # 0.6666666865348816Creates an exact-match accuracy metric, updates it with label and prediction arrays, and reads the resulting match frequency.
Monitoring exact-value predictions during training or evaluation.
Accumulating results across several batches before reading one value.
Evaluating integer or other directly comparable outputs where no thresholding or class decoding is needed.
Accuracy returns the proportion of element-wise predictions that exactly equal the corresponding labels. Internally, it accumulates the number of matches and the number of observed elements, then divides the former by the latter.
Follow-up: How would you evaluate separate datasets with one metric instance? Reset the metric state between datasets with reset_state(), then call update_state() and result() for each evaluation.
Its constructor accepts an optional name, defaulting to "accuracy", and an optional dtype. The metric can then be updated with labels and predictions through the Metric API.
Follow-up: Why might a classification task use another accuracy metric? Accuracy is exact-value matching; binary, categorical, or sparse-categorical variants interpret predictions and labels according to their classification representation.
The metric keeps running total and count variables and derives the result as the match frequency. Calling update_state() adds another batch to those running values.
Follow-up: What happens if predictions and labels have incompatible shapes? Element-wise comparison cannot represent the intended pairs reliably, so the inputs should be prepared with matching shapes and semantics.
keras.metrics.BinaryAccuracykeras.metrics.CategoricalAccuracykeras.metrics.SparseCategoricalAccuracykeras.metrics.Metric