Code Room
Code reviewMediumcr-g629
Subject Model evaluation pytorchLevel Mid–Senior~16 minCommon in Code quality & review interviewsIndustries Software development

Question

Review this PyTorch validation function.

Validation accuracy is noisy across runs and lower than expected for a model with dropout and batchnorm. What's wrong?

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewpython
import torch def validate(model, val_loader):    correct, total = 0, 0    for xb, yb in val_loader:        logits = model(xb)            # model has Dropout + BatchNorm layers        preds = logits.argmax(dim=1)        correct += (preds == yb).sum().item()        total += yb.size(0)    return correct / total
Run or narrate your approach, then ask the coach.