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

TensorFlow · Class

metrics.Accuracy

Calculates how often predictions equal labels. It accumulates matches and evaluated values across updates, then returns their ratio.

How it works

Accuracy is a stateful Keras metric that compares each prediction with its corresponding label. Each update accumulates the weighted number of matches in total and the number of evaluated values in count; result() returns total divided by count.

Syntax

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

Parameters

NameTypeDescription
namestring, default 'accuracy'Optional string name of the metric instance.
dtypeTensorFlow dtype, default NoneOptional data type of the metric result.

Returns

A scalar metric result representing the accumulated frequency of predictions matching labels.

Key points

  • The metric reports the proportion of elementwise prediction-label comparisons that match.
  • Its state consists of total matches and the count of evaluated values.
  • Each update records how frequently predicted values match true values.
  • The documented result is the accumulated matching frequency, rather than a loss value.
  • The final metric value is computed by dividing accumulated total by accumulated count.
  • A Keras model can receive an Accuracy instance through its compile metrics configuration.

Common mistakes

  • A zero sample weight excludes selected values from the calculation.
  • Without sample weights, every evaluated value contributes equally.

Example: Compute label accuracy

import tensorflow as tf

metric = tf.keras.metrics.Accuracy()
metric.update_state(y_true=[1, 2, 3, 4], y_pred=[0, 2, 3, 4])
print(metric.result().numpy())  # 0.75

Creates an Accuracy metric, records one batch, and prints the accumulated fraction of matching labels.

When to use it

Standalone evaluation

Evaluate discrete predictions against labels over one or more batches.

Model compilation

Register the metric for automatic reporting during Keras training and testing.

Interview questions

QHow does tf.keras.metrics.Accuracy compute its result?

It measures the frequency with which predictions equal labels. The metric maintains total and count, then result() returns total divided by count.

Follow-up: How would you ignore selected observations? Pass sample_weight with zero for those observations.

QHow is Accuracy used during eager execution or model training?

Instantiate the metric, call update_state(y_true, y_pred) for each batch, and call result() to read the accumulated value. It can also be supplied as a Metric instance in model.compile().

Follow-up: Why use update_state rather than calculating one batch manually? Stateful updates let the metric aggregate observations across multiple batches.

QWhat constructor options does Accuracy provide?

Its constructor accepts an optional name and dtype. The default name is accuracy, and dtype controls the data type of the metric result.

Follow-up: What weighting behavior applies when no sample_weight is supplied? Every value receives a weight of one.

  • tf.keras.Metric
  • tf.keras.metrics.BinaryAccuracy
  • tf.keras.metrics.CategoricalAccuracy
  • tf.keras.metrics.SparseCategoricalAccuracy

Source: tf.keras.metrics.Accuracy | TensorFlow v2.16.1. Checked 2026-10-15.