Index of a maximum element in TensorFlow tensor
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
To get the index of a maximum value in TensorFlow, the main tool is tf.argmax. The part that matters most is not the function name but the axis you choose, because that determines whether you are asking for the maximum per row, per column, or across the whole tensor.
Use tf.argmax for One-Dimensional Tensors
For a one-dimensional tensor, tf.argmax returns the index of the largest value.
This returns 1, because 7.2 is the largest value and it sits at index 1.
Axis Selection Is the Real Question
For multi-dimensional tensors, tf.argmax reduces along one axis and returns the index of the maximum inside each slice.
Those two calls mean different things:
- '
axis=0compares values down each column' - '
axis=1compares values across each row'
Choosing the wrong axis is the most common reason people get a correct-looking but wrong answer.
Find the Global Maximum in a Matrix
Sometimes you want the one location of the maximum across all elements. In that case, flatten the tensor first and then convert the flat index back into coordinates.
That gives both the flat offset and the row-column location of the overall maximum.
A Typical Classification Example
A very common use case is taking the predicted class from model output scores or logits.
If each row represents one example and each column one class, axis=1 returns the predicted class index for each example.
If you also want the winning score, combine argmax with reduce_max.
Tie Behavior Matters
If more than one element shares the maximum value, TensorFlow returns the first index along the chosen axis.
This returns 1, not 2, because the first maximum wins. That matters if downstream logic assumes ties are unique or random.
Use top_k When One Maximum Is Not Enough
If you need the top few indices rather than only one, use tf.math.top_k instead of repeated argmax calls.
That expresses the intent more clearly and avoids awkward repeated reduction logic.
Common Pitfalls
- Picking the wrong axis and then interpreting the result as if it meant something else.
- Forgetting that
argmaxreturns the first maximum when values are tied. - Treating the returned index tensor as if it were the maximum value itself.
- Forgetting to flatten first when the real goal is the global maximum over all elements.
- Using repeated
argmaxcalls when the requirement is really top-k ranking.
Summary
- '
tf.argmaxreturns index positions, not the maximum values themselves.' - The selected axis determines the meaning of the result.
- Flatten plus
tf.unravel_indexgives the global maximum location in multi-dimensional tensors. - '
axis=1is common for classification outputs where each row is one example.' - Use
tf.math.top_kwhen you need more than one best index.
Related reading
- Initial bias values for a neural network
- Initial bias values for a neural network
- Initialize keras placeholder as Input to a Custom Layer
- Initializing LSTM hidden state Tensorflow/Keras
- Inference using saved model in Tensorflow 2 how to control in/output?
- Inference using saved model in Tensorflow 2 how to control in/output?
- Inference with TensorRT .engine file on python
- Inferring templates from a collection of strings

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.