C.W.K.
Stream
Lesson 06 of 07 · published

Debugging Strategies

~9 min · advanced

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

The failure is almost always one of five

When training goes wrong it rarely fails loudly with a useful message — it fails quietly, with a loss that won't move or a gap between train and validation that won't close. The skill is reading the symptom back to its cause. Most of what you'll hit fits in one small table:

ProblemSymptomSolution
Shape mismatchValueError about incompatible shapesPrint shapes at each layer; check model.summary()
NaN lossLoss becomes NaN during trainingLower LR, check data for NaN/inf, add gradient clipping
OverfittingTrain acc high, val acc lowMore data, add dropout, L2 regularization, augmentation
UnderfittingBoth train and val acc lowBigger model, more epochs, lower regularization
Slow trainingEpochs take too longCheck GPU utilization, prefetch data, mixed precision

NaN loss: clip before you despair

NaN loss is the one that looks scariest and is usually the easiest. It means a gradient blew up — often a learning rate too high, or a single bad sample with an inf in it. Beyond lowering the LR and scrubbing the data, gradient clipping is a cheap guardrail: clipnorm rescales the whole gradient vector if its norm exceeds a threshold, so one exploding step can't poison the weights.

Code

Gradient clipping with clipnorm to stop NaN loss·python
# Gradient clipping to prevent NaN
optimizer = keras.optimizers.Adam(
    learning_rate=1e-3,
    clipnorm=1.0,  # Clip gradient norm
)

External links

Exercise

Take a working CIFAR-10 model. Introduce three bugs (one at a time): (a) label off-by-one, (b) loss/activation mismatch, (c) learning rate 100×. Note the symptom of each — failure mode library entry.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.