"My loss went to zero, my val curve looked gorgeous, and then a colleague pointed out that all my validation windows came from subjects I had already trained on."
A Chastened AI Agent
The architecture is the easy part
By now you can build an LSTM or a TCN, size its receptive field, attach the right head, and run it as a stream. None of that matters if the training run itself is broken. Sensor sequence models fail for reasons that have almost nothing to do with the layer stack: exploding gradients through time, a loss that rewards ignoring the rare event you actually care about, vision-borrowed augmentations that quietly destroy physical meaning, and validation splits that leak a subject's future into its own past. This section is the recipe book, architecture-agnostic and applying equally to the RNNs of Section 14.1, the TCNs of Section 14.2, and the transformers of Chapter 15.
This section assumes the encoders and heads of Sections 14.1-14.4, the batching and masking of variable-length windows, and the normalization pipeline of Chapter 13. It leans on one non-negotiable discipline: the subject-disjoint, no-peeking-at-the-future evaluation of Chapter 5. Every recipe below is downstream of getting the split right; a tuning decision measured on a leaky validation set optimizes for a number that will not survive deployment.
The loss is the specification, not a default
The single most consequential training choice for sensor models is the loss, because sensor labels are almost always imbalanced. A fall detector sees minutes of walking for every fraction of a second of falling; a bearing runs healthy for months before it fails; an arrhythmia is a handful of beats in a day-long ECG. Train plain cross-entropy on that and the optimizer finds the cheapest solution: always predict "normal", collect 99.7% accuracy, and never fire. The fix is to make the rare class expensive to miss. Class-weighted cross-entropy multiplies each class's loss by a weight inversely proportional to its frequency. Focal loss goes further, down-weighting easy, confidently-correct samples by a factor \((1-p_t)^\gamma\) so the gradient concentrates on the hard minority, which usually beats static class weights for detection tasks. For heavily skewed segmentation, soft Dice or Tversky losses optimize overlap directly and are far less seduced by majority background than per-pixel cross-entropy.
A rising loss is not always the wrong loss
Switching to focal loss or aggressive class weights routinely makes the training loss look worse, and a common misreading is to revert because "the model got worse". It did not: down-weighting the easy majority class raises the average loss per sample even as the model improves on the minority class the deployment metric cares about. Judge the change on the held-out, leakage-safe metric you will publish (recall at a fixed false-alarm rate, not the loss curve), and revert only if that metric gets worse.
Optimize the loss that matches the metric you report
If you will be judged on event-level recall at a fixed false-alarm rate, do not train cross-entropy and hope. Accuracy is not the metric; the confusion matrix under class imbalance is. Pick a loss whose gradient pushes on the errors your deployment actually punishes, and validate on the metric you will publish. The house rule of construct-matched evaluation, one metric co-computed with the loss it justifies, applies here: a model tuned on accuracy and reported on recall is two different experiments wearing one checkpoint.
Optimization for sequences: clip, warm up, decay
Recurrent networks propagate gradients through as many multiplications as there are timesteps, so a window of \(T=500\) samples is a 500-deep computation graph, and that depth makes the gradient norm volatile: one bad batch can produce a gradient a thousand times larger than the running average and blow the weights out in a single step. Gradient clipping is the cheap, load-bearing defense: clip the global gradient norm to a fixed ceiling (1.0 to 5.0 is a good starting range) before every optimizer step, and training that was diverging on batch 40 becomes stable. TCNs are less prone to this, since their gradients flow through a fixed convolutional depth, but clipping costs nothing and protects both. Note that clipping only caps a gradient that grew too large; it does nothing for one that decayed toward zero many steps back, the more common failure on long dependencies with a plain RNN, where the fix is architectural (gating, dilated convolutions) rather than a tighter clip threshold.
AdamW is the default optimizer here: it adapts per-parameter step sizes, which suits the heterogeneous gradient magnitudes of gated recurrent cells, and its decoupled weight decay is a cleaner regularizer than Adam's L2 coupling. Pair it with a schedule that warms up the learning rate over the first few hundred steps, then decays it with a cosine curve, because a large initial learning rate on a freshly initialized recurrent state produces exactly the gradient explosions clipping fights; easing in lets normalization statistics and gate biases settle first. The loop below also enables mixed precision via GradScaler: matrix multiplications run in float16 while master weights and the loss stay in float32, roughly halving activation memory and letting tensor cores run near double throughput. The risk is that small gradients underflow to zero in float16 before reaching the optimizer, silently starving the minority-class signal the loss above protects; the scaler compensates by scaling the loss up before backward and back down before clipping, the scale(...) and unscale_(...) calls below. Reach for it whenever GPU memory or throughput is the bottleneck; fall back to full precision only when debugging a numerically fragile loss.
import torch, math
from torch.optim import AdamW
def make_scheduler(opt, warmup, total):
def lr_lambda(step):
if step < warmup: # linear warmup
return step / max(1, warmup)
p = (step - warmup) / max(1, total - warmup)
return 0.5 * (1 + math.cos(math.pi * p)) # cosine decay to 0
return torch.optim.lr_scheduler.LambdaLR(opt, lr_lambda)
opt = AdamW(model.parameters(), lr=3e-3, weight_decay=1e-2)
sched = make_scheduler(opt, warmup=300, total=10_000)
scaler = torch.cuda.amp.GradScaler() # mixed precision
for x, y, mask in train_loader:
opt.zero_grad()
with torch.cuda.amp.autocast():
loss = masked_loss(model(x), y, mask) # ignore padded steps
scaler.scale(loss).backward()
scaler.unscale_(opt)
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
scaler.step(opt); scaler.update(); sched.step()
clip_grad_norm_ (tames the through-time gradient), the cosine schedule with warmup=300 (prevents the early-step blowup), and masked_loss (never trains on padding).Schedules, clipping, and early stopping for free
A hand-rolled trainer with warmup, cosine decay, clipping, mixed precision, checkpointing, and early stopping is a few hundred lines you will maintain forever. PyTorch Lightning collapses it: pass gradient_clip_val=1.0 to the Trainer, return the scheduler from configure_optimizers, and add EarlyStopping(monitor="val_recall", mode="max", patience=10) as a callback. Roughly 200 lines, and their off-by-one bugs, collapse to three arguments, with AMP and multi-GPU plumbing included.
Regularization and augmentation that respect physics
Sensor models overfit fast because labeled sensor data is scarce, so regularization earns its keep. Weight decay and dropout are the baseline, but recurrent dropout must use the same mask across timesteps (variational dropout) rather than resample each step, or it corrupts the recurrence itself. The higher-leverage lever is data augmentation, and here sensor work diverges from vision: an augmentation is only legal if the label survives it. Gaussian noise, small time warps, magnitude scaling, and random cropping all preserve "this is walking". But flipping an accelerometer axis inverts gravity and turns walking into physical nonsense; time-reversing an ECG destroys the causal P-QRS-T ordering; rotating a three-axis IMU is valid only if all three channels rotate by the same matrix, since the axes are physically coupled. The measurement models of Chapter 2 tell you which transforms are label-preserving; borrow a vision augmentation blindly and you train on data that cannot occur.
Industrial vibration: the recipe that saved the run
A bearing fault classifier for a fleet of factory pumps had a TCN that trained to 96% on held-out windows and detected almost nothing on a new pump. Three recipe changes fixed it, none architectural. First, switching from random-window to machine-disjoint validation dropped the honest score to 71% and exposed the real problem. Second, replacing cross-entropy with focal loss helped, because incipient faults were 2% of the data and the model had learned to predict "healthy" everywhere. Third, vision-style augmentation (including time-reversal) was replaced with jitter, magnitude scaling, and time-warps drawn from the actual RPM variation across pumps. The reworked recipe reached 88% machine-disjoint recall on unseen pumps.
Windowing, batching, and honest validation
Two mechanical choices decide whether your batches are even coherent. Window length must exceed the receptive field of Section 14.3 and must contain the temporal pattern the label describes; a two-second window cannot learn a gait cycle that takes three. Batching variable-length sequences requires padding to the longest member and a mask so the loss ignores the pad, exactly the masked loss the code above assumes; bucket similar-length sequences together to keep padding waste low. For sequences too long to fit in memory, truncated backpropagation through time splits the sequence into chunks, carries the recurrent state forward across chunk boundaries (detached from the graph), and backpropagates only within each chunk; set the truncation length to comfortably exceed the longest dependency the model needs, since gradients never flow past that boundary.
Of every choice in this chapter, the split decides the number that ships: get it right and a mediocre architecture still generalizes, get it wrong and a brilliant one just memorizes more convincingly. Random-window splits leak because consecutive windows from one subject or machine are nearly identical, so a validation window has a near-twin in training. Split by subject, by device, or by time period, never by window, and normalize using statistics computed on the training split alone. Early-stop on the leakage-safe validation metric and treat that curve as the truth. The formal protocols live in Chapter 65, and when labels are too scarce for any split to leave enough to train on, the self-supervised pretraining of Chapter 17 buys back label efficiency.
Where the recipe is moving
The 2023-2026 shift is away from training a bespoke model per task and toward fine-tuning a pretrained time-series backbone. Models such as MOMENT and TimesFM, and wearable-specific efforts like Google's Large Sensor Model (LSM), train on enormous unlabeled sensor corpora and adapt with a small labeled set, which changes the recipe: lower learning rates, layer-wise decay, often a frozen backbone with only the head trained. With a few hundred labeled windows rather than a few hundred thousand, the modern recipe is increasingly "fine-tune a foundation model" rather than "train from scratch", a tradeoff the book returns to across Part V.
Exercise: ablate the recipe, not the architecture
Take one fixed TCN and one imbalanced sensor task (for example fall detection from wrist IMU). Hold the architecture constant and ablate the recipe: (a) cross-entropy versus focal loss, (b) with and without gradient clipping, (c) random-window versus subject-disjoint split, (d) physics-respecting augmentation versus a vision-style set that includes axis flips and time-reversal. Report event-level recall at a fixed false-alarm rate for each, and rank the four choices by effect size: the split and the loss should move the honest number more than most architecture changes would.
Self-check
- Under 100:1 class imbalance, plain cross-entropy reaches 99% accuracy and fires on nothing. Name two loss changes that fix this and say which errors each one's gradient emphasizes.
- Why is gradient clipping more critical for a 500-step LSTM than for a shallow TCN, and what failure does it prevent in the first few training steps if you skip warmup?
- You augment IMU walking data by flipping the sign of the vertical accelerometer axis and your model gets worse. Why is this augmentation illegal, and what is a label-preserving alternative?
Lab 14
compare LSTM and TCN on a multivariate sensor task, including a streaming variant.
What's Next
In Chapter 15, we replace the recurrence and the fixed convolution with attention over time and channels, which lets a model relate any two timesteps directly, at a compute cost that forces its own set of training recipes: patchification, positional encoding for sensor identity, and the efficient-attention tricks that keep long sensor sequences affordable.
Bibliography
Optimization and regularization
Loshchilov, I., & Hutter, F. (2019). Decoupled Weight Decay Regularization (AdamW). ICLR.
Introduces AdamW, the default optimizer for sequence models here; decoupling weight decay from the adaptive step is the fix that makes regularization behave predictably.
The paper that diagnoses exploding and vanishing gradients through time and motivates gradient clipping, the single most load-bearing stability trick for RNN training.
Loshchilov, I., & Hutter, F. (2017). SGDR: Stochastic Gradient Descent with Warm Restarts. ICLR.
Origin of the cosine learning-rate schedule used in the training loop; warmup plus cosine decay is the standard recipe for stable convergence.
Losses for imbalance
Introduces focal loss; its down-weighting of easy examples is directly why it beats class-weighted cross-entropy on the heavy imbalance typical of sensor detection.
Augmentation and pretraining for sensor time series
Systematic catalogue of time-series augmentations (jitter, scaling, warping) and their effect on accuracy; the reference for choosing label-preserving transforms.
A pretrained time-series backbone that changes the recipe from train-from-scratch to fine-tune; illustrative of the frontier shift noted in this section.
Google's TimesFM; a concrete example of the pretrained backbone plus light fine-tuning recipe now competitive with bespoke models.
Google's wearable foundation model trained on large-scale unlabeled sensor data; grounds the claim that fine-tuning is displacing from-scratch training for scarce-label sensor tasks.