Convert logits or probabilities into predicted class indices.
Classification
TensorFlow · Function
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.
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.
tf.math.argmax(input, axis=None, output_type=tf.dtypes.int64, name=None)| Name | Type | Description |
|---|---|---|
input | Tensor | Input tensor whose values are compared. |
axis | int | Integer axis to reduce across; defaults to 0. |
output_type | tf.dtypes | Optional result dtype: tf.int32 or tf.int64; defaults to tf.int64. |
name | str | Optional operation name. |
A Tensor of type output_type.
axis selects the dimension over which candidate values are compared.output_type to choose whether returned indices use 32-bit or 64-bit integers.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.
Classification
Per-row selection
Tensor analysis
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.
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.
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.argmintf.math.reduce_maxtf.math.top_ktf.keras.ops.argmax