---
title: You are handed a training script whose validation accuracy stays at chance. Name the ten bugs you would check first.
url: https://gurkanozkan.ai/learn/questions/mle-finding-bugs-in-a-pytorch-training-loop-68/
markdown: https://gurkanozkan.ai/learn/questions/mle-finding-bugs-in-a-pytorch-training-loop-68.md
kind: question
role: ML engineering
stage: Technical
difficulty: Medium
json: https://gurkanozkan.ai/api/learn/questions/mle-finding-bugs-in-a-pytorch-training-loop-68.json
robots: noindex
---

# You are handed a training script whose validation accuracy stays at chance. Name the ten bugs you would check first.

ML engineering · Technical · Medium

## Answer

*Model answer, written in a candidate's voice*

### In short

- First diagnostic: overfit one batch/small subset; if it cannot, suspect the loop/data pipeline, not capacity.
- Missing optimizer.zero_grad(); gradients accumulate.
- model.eval() left on, or BN/dropout misconfigured.
- Labels shuffled independently of inputs.
- Wrong label dtype/shape (float vs long, wrong shape).
- Softmax applied before CrossEntropyLoss.
- LR too high or too low.
- Frozen parameters not passed to the optimiser / optimiser built before unfreezing.
- loss.backward() on a detached tensor / no grad path.
- Train/val normalisation mismatch, NHWC vs NCHW channel order, augmentation applied to labels.

### In full

I’d start by overfitting one batch: if the model cannot drive a tiny batch to near-zero loss, the bug is in the training loop or data pipeline, not capacity. Then I’d check missing optimizer.zero_grad(), model.eval() left on or BN/dropout misconfigured, labels shuffled independently of inputs, wrong label dtype/shape, softmax before CrossEntropyLoss, LR too high or too low, frozen parameters not passed to the optimiser, loss.backward() on a detached tensor, and train/val normalisation mismatch, NHWC vs NCHW channel order, or augmentation applied to labels. I’d verify the loss is finite and gradients are nonzero, then re-check the validation loader separately from the training loader. If it still sits at chance, I’d suspect the validation labels or metric are wrong rather than the model.

*Corrected by an independent AI reviewer.*

## What a strong answer covers

- Missing `optimizer.zero_grad()`; `model.eval()` left on, or BN/dropout misconfigured.
- Labels shuffled independently of inputs; wrong label dtype or shape; softmax before CrossEntropyLoss.
- LR too high or too low; frozen parameters not passed to the optimiser; `loss.backward()` on a detached tensor.
- Normalisation mismatch between train and val; channel order (NHWC vs NCHW); data augmentation applied to labels.
- The overfit-one-batch test as a first diagnostic.

## Common mistakes

- rewriting the model instead of systematically bisecting the pipeline.

## Topic guides

- [Debugging ML and LLM systems](https://gurkanozkan.ai/learn/topics/debugging.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
