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

TensorFlow · Function

tf.argmax

Returns the index of the largest value along a selected tensor axis. It is useful for converting model scores into winning class or item indices.

How it works

tf.math.argmax reduces a tensor along the chosen axis and returns the integer position of its largest value. The result keeps the other dimensions, and its integer dtype is controlled by output_type.

Syntax

tf.math.argmax(input, axis=None, output_type=tf.dtypes.int64, name=None)

Parameters

NameTypeDescription
inputTensorInput tensor whose values are compared.
axisintInteger axis to reduce across; defaults to 0.
output_typetf.dtypesOptional result dtype: tf.int32 or tf.int64; defaults to tf.int64.
namestrOptional operation name.

Returns

A Tensor of type output_type.

Key points

  • The operation identifies maximum-value positions rather than returning the maximum values themselves.
  • axis selects the dimension over which candidate values are compared.
  • Use output_type to choose whether returned indices use 32-bit or 64-bit integers.
  • The result is a tensor whose dtype follows the requested output type.
  • For a two-dimensional tensor, reducing over axis 1 produces one argmax index per row.

Common mistakes

  • When multiple values tie for the maximum, the first (smallest) index is returned.

Example: Choose the top class per row

import tensorflow as tf

scores = tf.constant([[0.2, 0.8, 0.5], [0.9, 0.1, 0.4]])
predicted_classes = tf.math.argmax(scores, axis=-1, output_type=tf.int32)
print(predicted_classes.numpy())  # [1 0]

Selects the highest-scoring class independently for each row.

When to use it

Convert logits or probabilities into predicted class indices.

Classification

Find the best-scoring item independently in each row of a batch.

Per-row selection

Locate maximum positions along a chosen dimension.

Tensor analysis

Interview questions

QWhat does tf.math.argmax return?

It returns the index of the largest value after reducing across axis. For a matrix, axis=0 chooses an index for each column, while axis=1 chooses one for each row.

Follow-up: How would you obtain class labels rather than indices? Use the returned indices to gather or index into a label tensor.

QHow can you control the result dtype?

output_type determines the integer dtype of the returned tensor and defaults to tf.int64. The documentation lists tf.int32 and tf.int64 as the optional output dtypes.

Follow-up: Why choose tf.int32? It can match APIs or tensors expecting 32-bit indices and may avoid an unnecessary dtype conversion.

QHow does the axis affect shape and meaning?

For a vector, reduce across axis 0. For higher-rank tensors, select the dimension whose maximum positions are needed; the reduced dimension is removed from the result shape.

Follow-up: How can you select the best item across all elements? Flatten the tensor first, then apply argmax without preserving separate axes.

  • tf.math.argmin
  • tf.math.reduce_max
  • tf.math.top_k
  • tf.keras.ops.argmax

Source: tf.math.argmax | TensorFlow v2.16.1. Checked 2026-10-15.