Accuracy compares predictions directly against true labels and computes the simple fraction that match exactly, updated incrementally across batches via its update_state() method and read at any point with result(). It's an intuitive, easy-to-interpret metric, but unlike a loss function it's not differentiable, since it's based on discrete exact matches rather than a smooth numeric difference, which is exactly why it's tracked purely for monitoring rather than ever being used as the loss that training actually optimizes.
1Understanding metrics.Accuracy()
Accuracy compares predictions directly against true labels and computes the simple fraction that match exactly, updated incrementally across batches via its update_state() method and read at any point with result(). It's an intuitive, easy-to-interpret metric, but unlike a loss function it's not differentiable, since it's based on discrete exact matches rather than a smooth numeric difference, which is exactly why it's tracked purely for monitoring rather than ever being used as the loss that training actually optimizes.
tf.keras.metrics.Accuracy() expects already-computed discrete predictions to compare, not raw probabilities — for typical classification model outputs, the string shortcut 'accuracy' passed to compile()'s metrics argument automatically handles converting probabilities to predicted classes first, which is what you almost always want instead of instantiating Accuracy() directly.
import tensorflow as tf
acc = tf.keras.metrics.Accuracy()
acc.update_state([1, 0, 1, 1], [1, 0, 0, 1])
print(acc.result().numpy())2Practical Example
Here is a real-world application of metrics.Accuracy() showing how it is used in production TensorFlow code.
import tensorflow as tf
acc = tf.keras.metrics.Accuracy()
acc.update_state([1, 0], [1, 0])
acc.update_state([1, 1], [0, 1])
print(acc.result().numpy())3Best Practices
Follow these guidelines when working with metrics.Accuracy():
1. Use the string shortcut 'accuracy' in compile()'s metrics argument for typical classification models, letting Keras handle converting probabilities to predicted classes automatically
2. Remember accuracy alone can be misleading on an imbalanced dataset, where predicting only the majority class can still produce a deceptively high accuracy score
3. Track precision, recall, or AUC alongside accuracy for classification tasks where class imbalance is a concern
Tip: tf.keras.metrics.Accuracy() expects already-computed discrete predictions to compare, not raw probabilities — for typical classification model outputs, the string shortcut 'accuracy' passed to compile()'s metrics argument automatically handles converting probabilities to predicted classes first, which is what you almost always want instead of instantiating Accuracy() directly.
import tensorflow as tf
acc = tf.keras.metrics.Accuracy()
acc.update_state([1, 0, 1, 1], [1, 0, 0, 1])
print(acc.result().numpy())