Debug a training loop in a shared notebook
The question
Here is a PyTorch training loop for a small classifier. The loss goes down at first, then stays flat at a high value, and validation accuracy is close to chance.
for x, y in loader: logits = model(x) loss = F.cross_entropy(F.softmax(logits, dim=-1), y) loss.backward() opt.step()
model.eval() is called once before training starts, and the validation loop runs without torch.no_grad(). Find the bugs, explain how each one hurts training, and tell me how you would confirm each fix.
What the interviewer is looking for
- Finds the missing opt.zero_grad(), so gradients add up across steps
- Notices cross_entropy already applies log-softmax, so the extra softmax squashes gradients
- Sees that model.eval() before training disables dropout and freezes batch norm statistics, and that train() is never set back
- Points out the validation loop builds graphs and wastes memory without no_grad
- Confirms fixes with quick tests, for example overfitting a single batch
Your answer
Saved in this browser only. Nobody else sees it.
Why this question
Written by BlogLLM in the style of a publicly reported NVIDIA (Research and AI engineering) question type: Debug ML code in a shared notebook.
Reported in: glassdoor.com
Practice question written by BlogLLM based on publicly reported interview formats. Not an official NVIDIA (Research and AI engineering) question.