Training RNNs
Backprop through time, done carefully
RNN training is BPTT: unroll, backprop through T steps, gradient-clip, repeat. Practical craft lives in truncated BPTT, length bucketing, state carry, and learning-rate hygiene on long sequences.
What you will learn
- Describe full vs truncated BPTT and state carrying
- Apply gradient clipping and bucketing correctly
- Diagnose long-sequence training instabilities
What is this?
Training an RNN means unrolling its graph over the sequence and backpropagating through every step — BPTT — then updating the shared weights. Truncated BPTT unrolls only a window (e.g. 20 steps) while carrying the state across the boundary to keep long-range memory without a monster graph. Practical training adds gradient clipping, length bucketing/packing, and LR schedules tuned for deep effective depth.
Why do you need it?
- Full BPTT over 1,000 tokens is memory-prohibitive and numerically awful — truncation is standard.
- The effective model is as deep as your sequence length, so LR/grad hygiene matters more than in feedforward nets.
- Getting training right is usually 80% of RNN success — cells are secondary.
- Modern practice (attention) borrows the same discipline with different memory patterns.
Mental model
Rowing a boat while noting every rope pull ever
Rethinking BPTT truncation
Rowing a long trip, you only need the last strokes' feedback to steer, but you must remember upstream current (state carry). Full-BPTT would re-evaluate every pull since the marina; smart rowing carries the boat's state forward while adjusting only from recent strokes.
How it works
- Full BPTT: unroll all T steps, compute loss, backprop through all of them, update once.
- Truncated: unroll W steps, don't backprop farther, but KEEP h carried from the previous W-window.
- Packing sorts by length and groups, so batches don't waste padding compute.
- Gradient clip by global norm (e.g. 1.0) to tame the exponential growth.
- LR: start small, use schedule/adaptive optimizer, and re-checkable with range tests per cell type.
Truncated BPTT with state carry across windows.
In production
- Speech recognition towers train with truncated BPTT + state carry on Windows of acoustic frames.
- Time-series RNN products pack/bucket by forecast horizon to keep batch compute honest.
- RL-from-history agents train recurrent critics with mixed BPTT using carried states.
Best practices
- Carry the state across truncation boundaries; silent state-reset loses long memory.
- Bucket by length and mask padded steps in the loss.
- Debug with LSTM/GRU cells first, then try vanilla only for short sequences.
- Use gradient cropping and watch per-layer grad norms after each epoch.
Common mistakes
- No state carry — equivalent to a fresh RNN every window.
- Applying batch norm to a batched unroll where padding injects fake steps.
- Forgetting mask → gradients flow into padding positions.
- Small LR then blame the model: LR-range-test with the actual cell length first.
Common issues
Long sequences barely learn but short ones doTruncation window too small or state not carried
Lengthen window, ensure carry, and switch cell to LSTM/GRU.
Loss jumps every ~window lengthState gets reset at truncation boundaries (forgot carry)
Pass the computed h from the previous window into the next batch's initial state.
NaN right after gradient clipping is enabledClip applied to wrong object (per-parameter instead of global norm) or overflow before clip
Clip the full gradient vector norm; move clip before optimizer; scale to fp32 sooner.
Debugging tips
- Print grad-norm by time-window: if window 1 ≈ 0 and window 4 healthy, vanishing in action.
- Overfit a 3-sequence set to validate truncation/carry plumbing before larger runs.
- Compare train with truncation=100 vs 20; behavior change shows where the cut bites.
- Confirm packed/padded loss explicitly excludes pad ids via a mask matrix.
Interview checkpoint
Quick quiz
Truncated BPTT refers to:
Padded sequence positions should contribute to the loss.