Python / Python Deep Learning and Neural Networks Interview Questions
How do you diagnose a neural network that is not training correctly from its loss curves?
Reading loss curves is one of the most important practical skills in deep learning. The shape of the training and validation loss over time reveals the failure mode and guides the fix.
| Loss curve shape | Diagnosis | Likely fix |
|---|---|---|
| Loss is NaN from the start | Exploding gradients or bad init | Gradient clipping, lower lr, check data for inf/NaN |
| Loss doesn't decrease at all | Vanishing gradient, lr too low, dead neurons | Check activations, raise lr, use He init + ReLU |
| Loss decreases then plateaus early | Learning rate too high or model too small | Reduce lr / lr schedule, increase capacity |
| Train loss low, val loss high (large gap) | Overfitting | More regularisation: dropout, weight decay, augmentation, early stopping |
| Both losses plateau at high value | Underfitting (high bias) | Increase model capacity, train longer, reduce regularisation |
| Loss oscillates wildly | Learning rate too high | Reduce lr, use lr schedule, check batch size |
import torch
import torch.nn as nn
# Checking for gradient issues
model = nn.Linear(10, 5)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
for step, (X, y) in enumerate(loader):
optimizer.zero_grad()
loss = criterion(model(X), y)
loss.backward()
# Check for NaN/Inf in loss
if not torch.isfinite(loss):
print(f'Step {step}: non-finite loss = {loss.item()}')
break
# Monitor gradient norms
total_norm = 0
for p in model.parameters():
if p.grad is not None:
total_norm += p.grad.data.norm(2).item() ** 2
total_norm = total_norm ** 0.5
if step % 100 == 0:
print(f'Step {step}: loss={loss.item():.4f} grad_norm={total_norm:.4f}')
optimizer.step()
# Check dead ReLU neurons
def count_dead_neurons(model, X):
activations = []
def hook(m, inp, out):
activations.append((out <= 0).float().mean().item())
handles = [l.register_forward_hook(hook)
for l in model.modules() if isinstance(l, nn.ReLU)]
with torch.no_grad(): model(X)
for h in handles: h.remove()
return activations # fraction of dead neurons per layer
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
