Difference between keras.metrics.Accuracy and accuracy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When compiling a Keras model, you specify metrics to track during training and evaluation. Two common options are the string "accuracy" and the class keras.metrics.Accuracy(). They look like they do the same thing, but they behave differently. The string shorthand triggers Keras's smart metric selection, while the class gives you an explicit, specific accuracy computation. Confusing the two can lead to misleading evaluation numbers.
This article explains exactly what each one does, when Keras uses each variant, and how to choose the right one for your model.
What the String "accuracy" Does
When you pass the string "accuracy" to model.compile(), Keras does not simply use one fixed accuracy function. Instead, it inspects the loss function and output shape of your model to decide which accuracy metric to apply.
In this example, because the loss is categorical_crossentropy and the output shape is (batch, 10), Keras automatically selects CategoricalAccuracy as the underlying metric. Here is the selection logic:
categorical_crossentropyloss: Keras usesCategoricalAccuracy, which compares the index of the highest predicted probability against the one-hot encoded label.sparse_categorical_crossentropyloss: Keras usesSparseCategoricalAccuracy, which compares the predicted class index against an integer label.binary_crossentropyloss: Keras usesBinaryAccuracy, which applies a threshold (default 0.5) to the predictions and compares against binary labels.
This auto-selection is convenient because it handles the label format for you.
What keras.metrics.Accuracy() Does
keras.metrics.Accuracy() is a specific metric class that computes strict equality between predictions and labels. It does not apply argmax to the predictions or check probability thresholds. It literally compares the predicted value to the true value element by element.
This works well when your predictions are already discrete class labels (integers), not probability distributions. If you pass softmax probabilities to keras.metrics.Accuracy(), it will compare the probability vectors directly to one-hot labels, which will almost never match, resulting in a reported accuracy near zero.
The Difference in Practice
Here is a concrete example showing how the two diverge.
The string version reports a reasonable accuracy (depending on the data and training), while the class version reports a number near zero because it compares raw probability vectors to one-hot vectors.
Choosing the Right Metric Class Explicitly
If you want to be explicit rather than relying on string auto-selection, use the specific accuracy class that matches your label format.
Using the explicit class is clearer than the string shorthand and eliminates any ambiguity about which computation is being performed.
Custom Training Loops
In custom training loops (without model.compile), you must use metric objects directly and manage their state yourself.
In this context, the string shorthand does not apply. You are responsible for picking the correct metric class.
Common Pitfalls
- Using
keras.metrics.Accuracy()with softmax outputs. This is the most common mistake. The metric expects discrete predictions, not probability distributions. UseCategoricalAccuracyorSparseCategoricalAccuracyinstead. - Assuming
"accuracy"always means the same thing. The string changes meaning depending on your loss function. If you switch fromcategorical_crossentropytosparse_categorical_crossentropybut keep"accuracy", the underlying metric changes silently. This can be confusing during debugging. - Forgetting to reset state in custom loops. Metric objects accumulate state across batches. If you forget
reset_state()at the start of each epoch, your accuracy numbers will average across all epochs rather than reflecting the current one. - Mixing label formats. If your labels are one-hot but you use
SparseCategoricalAccuracy, or your labels are integers but you useCategoricalAccuracy, the accuracy numbers will be incorrect. Always match the metric to the label encoding.
Summary
The string "accuracy" in model.compile() is a shorthand that auto-selects the appropriate accuracy variant based on your loss function and output shape. keras.metrics.Accuracy() is a specific class that does strict equality comparison and is only appropriate when your predictions are already discrete values. For most classification tasks, either use the string shorthand or be explicit with CategoricalAccuracy, SparseCategoricalAccuracy, or BinaryAccuracy to avoid silent mismatches between your metric and your data format.

