Keras
accuracy
binary_accuracy
machine learning
deep learning

Why the accuracy and binary_accuracy in keras have same result?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In a standard binary-classification Keras model, accuracy and binary_accuracy often produce the same number because they are effectively measuring the same decision rule. With one sigmoid output and binary targets, Keras maps the generic accuracy metric to binary-style accuracy behavior, so both metrics threshold predictions and compare them to the labels the same way.

The Common Binary Setup

The typical case looks like this:

  • the model ends with one sigmoid output
  • labels are 0 and 1
  • the default threshold is 0.5

Example:

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

In this setup, both metrics interpret values >= 0.5 as class 1 and values < 0.5 as class 0.

Why accuracy Works as an Alias Here

Keras does not treat the string "accuracy" as one hard-coded formula for every task. It resolves that generic metric name based on output shape and target structure.

For a simple binary task, the resolved behavior is effectively binary accuracy. That is why these often look identical in logs:

python
metrics=["accuracy"]

and:

python
metrics=[tf.keras.metrics.BinaryAccuracy()]

They are not two fundamentally different metrics in that binary configuration.

A Small Example

python
1import numpy as np
2import tensorflow as tf
3
4y_true = np.array([[1.0], [0.0], [1.0], [0.0]])
5y_pred = np.array([[0.9], [0.2], [0.7], [0.4]])
6
7metric = tf.keras.metrics.BinaryAccuracy()
8metric.update_state(y_true, y_pred)
9print(metric.result().numpy())

If the model above were compiled with "accuracy", Keras would typically report the same result for this kind of data.

When They Can Differ

They stop behaving like duplicates when the problem setup changes.

Examples include:

  • multi-class softmax outputs
  • sparse categorical targets
  • multi-label outputs with several sigmoid units
  • a custom BinaryAccuracy(threshold=...)

For example, changing the threshold changes the metric behavior explicitly:

python
metric = tf.keras.metrics.BinaryAccuracy(threshold=0.7)
metric.update_state(y_true, y_pred)
print(metric.result().numpy())

Now you are no longer using the same default decision boundary as the generic binary-style accuracy behavior.

Why This Matters in Practice

If you log both metrics in a standard binary model and they match exactly, that usually means you added a redundant metric, not that Keras is doing something wrong.

If you want extra signal, it is often more useful to log metrics such as these:

  • precision
  • recall
  • AUC
  • binary accuracy with a custom threshold

Those metrics tell you something genuinely different about the classifier.

That matters especially for imbalanced problems. Two identical accuracy values may look reassuring while still hiding very poor recall on the positive class. Replacing one redundant accuracy metric with a more informative metric usually makes the training report more useful.

Common Pitfalls

A common mistake is assuming accuracy is always the same formula regardless of task shape. In Keras, the generic metric name is interpreted in context.

Another mistake is being surprised when accuracy and binary_accuracy match in a one-output sigmoid model. That is expected behavior.

A third issue is relying only on accuracy in imbalanced datasets. Even when the metric is computed correctly, it may still hide poor minority-class performance.

Summary

  • In standard binary classification, accuracy and binary_accuracy often compute the same thing
  • This happens with one sigmoid output, binary labels, and the default threshold
  • Keras resolves the generic accuracy metric according to the task structure
  • The metrics can diverge when output shapes or thresholds differ
  • If you need additional insight, log metrics that measure something beyond thresholded correctness

Course illustration
Course illustration

All Rights Reserved.