Vision Transformer, Part 6 — How It Learns: Logits → Loss → Backprop → Update
Summary
Vision Transformer, Part 6" elucidates the backward pass of a ViT's training, detailing how a single loss value updates its 1.2 million weights. The process begins by converting 10 logits to probabilities via softmax, then calculating cross-entropy loss against a one-hot label, resulting in a loss of 0.13 for a horse image with 0.88 probability. This loss, averaged over a batch of 128 images, initiates backpropagation. The gradient at each logit is `p − y` (e.g., `0.88 − 1 = −0.12` for the horse logit), which propagates backward as `δ`. Each weight's gradient is `δ · x`, with the chain rule determining its direction. The AdamW optimizer refines updates with momentum, per-parameter rates, and decoupled weight decay (0.05), using a 3e-4 learning rate and 1.0 gradient clip. This loop runs ~19,500 times, reducing loss from ~2.3 to ~0.8 and achieving 82.68% validation accuracy.
Key takeaway
For machine learning engineers optimizing Vision Transformer training, understanding the backward pass mechanics is crucial. You should meticulously configure your loss function (e.g., cross-entropy with label smoothing 0.1) and AdamW optimizer settings (lr 3e-4, weight decay 0.05, gradient clip 1.0). This precise control over how gradients propagate and weights update directly impacts model convergence, loss reduction from ~2.3 to ~0.8, and achieving target accuracy like 82.68%.
Key insights
The entire forward pass must be differentiable for a single loss to adjust all model weights via backpropagation.
Principles
- Every operation from pixels to loss must be differentiable.
- Gradient descent automatically reduces loss by stepping opposite the gradient.
- AdamW refines updates with momentum and per-parameter rates.
Method
The learning process involves logits → softmax → cross-entropy loss → backpropagation (gradient `p − y` then `δ · x`) → chain rule for direction → AdamW weight update (momentum, per-parameter rate, weight decay).
In practice
- Implement cross-entropy loss with label smoothing.
- Use AdamW optimizer with cosine-decayed learning rate.
- Monitor loss reduction and validation accuracy over epochs.
Topics
- Vision Transformer
- Backpropagation
- Cross-Entropy Loss
- AdamW Optimizer
- Gradient Descent
- Neural Network Training
Best for: AI Scientist, Machine Learning Engineer, AI Student
Related on AIssential
See Counsel's argued verdicts on the open AI decisions leaders are weighing →
Editorial summary, takeaway, and curation by AIssential. Original article published by Machine Learning on Medium.