When you're building or debugging neural networks in PyTorch, you'll frequently need to locate which element holds the highest value in a tensor. This comes up constantly: finding predicted classes from model outputs, locating peaks in data, or determining winning decisions in reinforcement learning setups.
PyTorch's torch.argmax() function handles exactly this task. It returns the index positions of maximum values rather than the values themselves, and it works across any number of dimensions when you know how to steer it.
At a Glance
- Torch.argmax() returns indices of maximum values, not the values themselves
- The dim parameter controls whether you search along rows (dim=1) or columns (dim=0)
- Keepdim preserves the reduced dimension in the output tensor
- When multiple elements share the maximum value, argmax() returns the first occurrence
- The function works identically on 1D, 2D, and higher-dimensional tensors
Basic Syntax and 1D Behavior
The function signature is straightforward:
Torch.argmax(tensor, dim=None, keepdim=False)
Here's the simplest case with a one-dimensional tensor:
Import torch
Data = torch.tensor([0., -1., 2., 8.])
Result = torch.argmax(data)
Print(result)
This prints "3" because the value 8.0 sits at index position 3 in the tensor. Without specifying dim, PyTorch flattens the input and returns the global maximum's index.
Working with Multi-Dimensional Tensors
For two-dimensional data, the dimension parameter becomes critical. Consider a 6×8 tensor created with random values:
Import torch
Matrix = torch.randn(6, 8)
Print(torch.argmax(matrix, dim=0))
Print(torch.argmax(matrix, dim=1))
Setting dim=0 compares column-wise, returning one index per column showing which row holds the maximum. Setting dim=1 compares row-wise, returning one index per row showing which column holds the maximum. Mixing these up silently gives you wrong indices, so double-check your dimension choice against what you're actually trying to find.
The keepdim parameter keeps the output tensor's shape consistent with the input:
Torch.argmax(matrix, dim=1, keepdim=True)
This produces a column vector instead of a row vector, which matters when you're feeding the result into operations expecting specific shapes.
Real-World Scenario
Say you're implementing an image classifier. Your model outputs ten scores (logits) for different categories. Converting those scores to a prediction looks like this:
Logits = trained_model(input_image)
Predicted_class = torch.argmax(logits, dim=1)
The predicted_class tensor now holds the index of whichever category received the highest score. In a batch of 32 images, you get 32 class predictions in one operation.
Why This Matters
The argmax() function is everywhere in machine learning code, yet its dimension behavior trips up newcomers regularly. Understanding that it returns positions rather than values prevents confusion when you chain operations together. The first-occurrence rule for tied values also matters more than it seems—when sorting activations or comparing model outputs, this behavior can subtly affect which prediction wins.
Bottom Line
Torch.argmax() is your go-to tool for finding index positions of maximum values in PyTorch tensors. Remember the core distinction: it gives you where the maximum lives, not what the maximum is. For the actual value, pair it with torch.max() when needed. Master the dim parameter, and you'll avoid the most common pitfall developers encounter with this function.
Key takeaways
- Always verify you're using the correct dim value before feeding results into downstream operations
- When multiple maxima exist, argmax() returns the earliest index; account for this in tie-breaking logic
- Combine torch.argmax() with torch.max() when you need both the index and the value
- Keepdim=True simplifies shape management when chaining tensor operations
Frequently asked questions
Does torch.argmax() return the value or the index?
It returns the index position of the maximum value, not the value itself. Use torch.max() if you need the actual value.
What happens when multiple elements have the same maximum value?
The function returns the index of the first occurrence among the tied values.
How do I find maximum values along a specific dimension?
Use the dim parameter. Dim=0 compares column-wise, dim=1 compares row-wise in 2D tensors.
What's the difference between argmax() with and without the dim parameter?
Without dim, it returns the global maximum's index in a flattened tensor. With dim specified, it returns one index per slice along that dimension.
Can I use argmax() on GPU tensors?
Yes, argmax() works on both CPU and CUDA tensors, and the syntax remains identical.