Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

metrics.Accuracy

keras.metrics.Accuracy measures how often predictions exactly equal their corresponding labels. It maintains running match and sample counts and returns their ratio.

How it works

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.

Syntax

keras.metrics.Accuracy(name="accuracy", dtype=None)

Parameters

NameTypeDescription
namestringMetric instance name; defaults to "accuracy".
dtypestring or data typeOptional data type used by the metric.

Returns

A scalar representing the frequency with which predictions equal labels.

Key points

  • Accuracy measures the fraction of predictions that are exactly equal to their corresponding labels.
  • It stores accumulated matches in total and the number of evaluated values in count.
  • The running state computes the frequency of y_pred matches against y_true.
  • result() returns the accumulated match frequency as a scalar metric value.

Common mistakes

  • This metric checks exact equality, not whether a predicted class is merely close to a label or within a tolerance.
  • The metric is stateful; reuse across independent evaluations requires resetting its accumulated state.

Example: Compute exact-match accuracy

import keras

metric = keras.metrics.Accuracy()
metric.update_state([1, 2, 3], [1, 0, 3])
print(float(metric.result()))  # 0.6666666865348816

Creates an exact-match accuracy metric, updates it with label and prediction arrays, and reads the resulting match frequency.

When to use it

Track element-wise correctness

Monitoring exact-value predictions during training or evaluation.

Aggregate batch metrics

Accumulating results across several batches before reading one value.

Compare already-decoded values

Evaluating integer or other directly comparable outputs where no thresholding or class decoding is needed.

Interview questions

QWhat does keras.metrics.Accuracy measure?

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.

QWhat are the constructor options?

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.

QHow does Accuracy aggregate multiple batches?

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.BinaryAccuracy
  • keras.metrics.CategoricalAccuracy
  • keras.metrics.SparseCategoricalAccuracy
  • keras.metrics.Metric

Source: Accuracy metrics - Keras. Checked 2026-10-15.