pytorch
backpropagation
argmax
machine learning
neural networks

How does pytorch backprop through argmax?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

PyTorch does not meaningfully backpropagate through argmax because argmax is a discrete operation. During training, the normal pattern is to compute loss on logits or probabilities with differentiable functions and use argmax only for metrics or final predictions.

Why argmax Breaks Gradient Flow

Backpropagation needs gradients, and argmax does not provide a useful gradient. Small changes in the input usually leave the selected index unchanged, and then the output jumps abruptly when another class wins. That step-like behavior is not suitable for ordinary gradient-based learning.

python
1import torch
2
3x = torch.tensor([0.1, 0.9, 0.2], requires_grad=True)
4i = torch.argmax(x)
5print(i)

The result is an integer-like index tensor. It is useful for choosing a class label, but not for propagating learning signals back into the model parameters.

The Correct Training Pattern

For classification, keep the model output continuous and apply a differentiable loss such as cross-entropy.

python
1import torch
2import torch.nn.functional as F
3
4logits = torch.tensor([[2.1, 0.3, -1.0]], requires_grad=True)
5target = torch.tensor([0])
6
7loss = F.cross_entropy(logits, target)
8loss.backward()
9
10print("loss:", float(loss))
11print("grad:", logits.grad)

Then compute predicted classes separately for monitoring or inference.

python
pred = torch.argmax(logits.detach(), dim=1)
print("pred:", pred)

This keeps training differentiable while still giving you discrete class labels for accuracy or reporting.

torch.max Versus argmax

This topic often causes confusion because torch.max can return both values and indices. The max values can still participate in gradients when used appropriately, but the selected indices are still discrete outputs.

So the important distinction is:

  • max values can be part of differentiable computation,
  • max indices from argmax are not the path you train through.

What to Do If Training Really Needs Discrete Choices

Some models need something that feels like a hard choice during training, such as routing, token selection, or categorical sampling. In those cases, the solution is usually not raw argmax, but a differentiable approximation or estimator.

A common option is to work with softmax probabilities instead of hard indices.

python
1import torch
2
3logits = torch.tensor([[1.0, 0.2, -0.3]], requires_grad=True)
4probs = torch.softmax(logits, dim=1)
5weights = torch.tensor([[1.0, 0.5, 0.1]])
6
7objective = -(probs * weights).sum()
8objective.backward()
9print(logits.grad)

Another option is Gumbel-Softmax, which gives a differentiable approximation to categorical sampling.

python
1import torch
2import torch.nn.functional as F
3
4logits = torch.tensor([[1.2, 0.4, -0.7]], requires_grad=True)
5y = F.gumbel_softmax(logits, tau=1.0, hard=False)
6loss = -y[:, 0].mean()
7loss.backward()
8print(logits.grad)

These techniques are approximations, but they preserve a gradient path.

Separate Optimization From Metrics

A clean training loop keeps optimization and prediction logic separate.

python
1optimizer.zero_grad()
2logits = model(batch_x)
3loss = F.cross_entropy(logits, batch_y)
4loss.backward()
5optimizer.step()
6
7with torch.no_grad():
8    preds = logits.argmax(dim=1)
9    accuracy = (preds == batch_y).float().mean()

That separation prevents metric code from accidentally interfering with the computational graph.

Common Pitfalls

A common mistake is applying argmax before the loss and then wondering why the model does not learn. Once the output is reduced to hard class indices, the gradient path is effectively gone.

Another issue is using discrete predictions inside the forward path when the objective really needs continuous logits or probabilities.

Developers also sometimes assume estimator tricks such as straight-through methods are exact gradients. They are not; they are practical approximations.

Summary

  • 'argmax is not a differentiable training operation in the normal gradient-descent sense.'
  • Train on logits or probabilities with differentiable losses such as cross-entropy.
  • Use argmax for metrics and inference, not inside the loss path.
  • If training needs discrete-like behavior, use differentiable approximations such as softmax or Gumbel-Softmax.
  • Keep optimization logic and prediction logic separate in the training loop.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.