Keras
Deep Learning
Machine Learning Metrics
Model Evaluation
Accuracy Calculation

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

keras.metrics.Accuracy() and "accuracy" look similar in model.compile, but they do not mean the same thing. One is a concrete metric class that checks exact equality between y_true and y_pred, while the string alias asks Keras to choose an accuracy metric that fits the model output and label format.

What keras.metrics.Accuracy() Actually Measures

keras.metrics.Accuracy() is a general exact-match metric. It compares the predicted values and true values element by element after compatible casting. That makes it appropriate when your predictions are already discrete labels, not raw probabilities.

For example:

python
1import tensorflow as tf
2
3y_true = tf.constant([1, 0, 1], dtype=tf.float32)
4y_pred = tf.constant([1, 0, 1], dtype=tf.float32)
5
6metric = tf.keras.metrics.Accuracy()
7metric.update_state(y_true, y_pred)
8print(float(metric.result().numpy()))  # 1.0

Now look at the same metric on model probabilities:

python
1y_true = tf.constant([1, 0, 1], dtype=tf.float32)
2y_pred = tf.constant([0.9, 0.2, 0.8], dtype=tf.float32)
3
4metric = tf.keras.metrics.Accuracy()
5metric.update_state(y_true, y_pred)
6print(float(metric.result().numpy()))  # 0.0

The score is zero because 0.9 is not exactly equal to 1.0, even though those probabilities would be considered correct after thresholding. That is why Accuracy() is often the wrong choice for classifier outputs straight from a sigmoid or softmax layer.

What the "accuracy" Alias Means

When you pass "accuracy" to model.compile, Keras interprets it as a request for a task-appropriate accuracy metric. Depending on your output shape, target format, and training configuration, Keras typically resolves that alias to something like:

  • 'BinaryAccuracy'
  • 'CategoricalAccuracy'
  • 'SparseCategoricalAccuracy'

For a binary classifier with sigmoid output, "accuracy" usually behaves like thresholded binary accuracy:

python
1model = tf.keras.Sequential(
2    [
3        tf.keras.layers.Input(shape=(10,)),
4        tf.keras.layers.Dense(1, activation="sigmoid"),
5    ]
6)
7
8model.compile(
9    optimizer="adam",
10    loss="binary_crossentropy",
11    metrics=["accuracy"],
12)

In that case, predictions such as 0.9 and 0.2 are interpreted as class probabilities, not exact labels. That is much closer to what most people expect when they say they want classification accuracy.

Why the Difference Matters

The distinction matters because the wrong metric can report nonsense while still looking technically valid. If you compile a softmax classifier with keras.metrics.Accuracy(), Keras will compare floating-point output vectors or logits directly against the labels. That usually produces very low or meaningless scores.

For a multi-class model, you typically want one of the categorical metrics instead:

python
1model = tf.keras.Sequential(
2    [
3        tf.keras.layers.Input(shape=(20,)),
4        tf.keras.layers.Dense(32, activation="relu"),
5        tf.keras.layers.Dense(3, activation="softmax"),
6    ]
7)
8
9model.compile(
10    optimizer="adam",
11    loss="sparse_categorical_crossentropy",
12    metrics=["accuracy"],
13)

Here the alias lets Keras choose a categorical accuracy metric that knows how to compare class predictions with sparse integer targets.

When Accuracy() Is Still Useful

keras.metrics.Accuracy() is not wrong. It is just narrower than the alias. It makes sense when you already converted predictions into the exact labels you want to compare. For example, if you run post-processing that produces integer class ids or exact token labels, an exact-match metric may be exactly what you need.

You can also use it in evaluation code outside the training loop when you explicitly control both tensors and want literal equality semantics.

Inspect Metrics Intentionally

If you are unsure what Keras is doing, inspect your compiled setup and test the metric on a small batch. That is safer than assuming the string alias and the class-based metric behave the same way.

A useful mental model is:

  • 'Accuracy() asks, “Are these values exactly the same?”'
  • '"accuracy" asks, “Please choose the normal accuracy metric for this task.”'

That is not the official implementation detail, but it is a reliable way to decide which one belongs in your code.

Common Pitfalls

The most common mistake is using keras.metrics.Accuracy() with sigmoid or softmax outputs. Exact floating-point equality is almost never what you want in that scenario.

Another pitfall is assuming the "accuracy" alias always resolves to the same concrete metric. It can differ between binary, categorical, and sparse categorical setups, so the alias is context-sensitive.

It is also easy to mix one-hot targets and sparse integer targets with the wrong loss and then blame the metric. The metric choice and the label encoding must agree with each other.

Finally, remember that high accuracy may still hide class imbalance problems. Even when the alias picks the right classification metric, you may need precision, recall, AUC, or MCC to understand model quality.

Summary

  • 'keras.metrics.Accuracy() performs exact equality comparison.'
  • '"accuracy" is a convenience alias that usually resolves to the right classification accuracy metric for the task.'
  • For raw sigmoid or softmax outputs, the alias is usually the safer default.
  • Use Accuracy() when predictions are already discrete labels and exact match is the intended behavior.
  • Always align the metric, loss function, and label encoding instead of choosing them independently.

Course illustration
Course illustration

All Rights Reserved.