"I built a machine that reproduces the world perfectly. Then it reproduced the fault perfectly too, gave it a low error, and reported that everything was fine."
An Overtrained AI Agent
Why this section matters
In condition monitoring you almost never have labeled faults. Bearings fail rarely, pumps cavitate on their own schedule, and nobody will let you break a turbine to collect training examples. So the dominant strategy is to model normal and flag whatever the model cannot explain. Two model families do this: a reconstruction model learns to compress and rebuild healthy signals, and a forecasting model learns to predict the next samples from the recent past. In both cases the anomaly score is a residual: how badly the model missed. When a machine drifts into a state the model never saw during training, the residual spikes. This section explains what each family models, why residuals turn "I have only normal data" into a working detector, the failure mode where the model reconstructs the anomaly too well and hides it, and how to convert a noisy residual stream into a threshold you can actually alarm on.
This section builds directly on the classical residual and change-detection machinery of Chapter 12: a forecasting detector is a learned generalization of a prediction-residual monitor, and the CUSUM-style thresholding there applies unchanged. The sequence models we lean on, recurrent networks and temporal convolutions, come from Chapter 14, and the learned representations they operate on from Chapter 13. Throughout, the split between "normal" training data and evaluation data must respect the unit- and time-level leakage rules of Chapter 5, because a residual detector that has seen the test machine's future is measuring memory, not surprise.
Model normal, score the residual
Both families share one contract: fit a function on healthy data only, then score new data by how far reality departs from what the model expected. The difference is what the model is asked to produce. A reconstruction model \(g\) maps a window \(x\) to an approximation \(\hat{x} = g(x)\) of that same window, typically through a bottleneck (an autoencoder, a variational autoencoder, or a masked reconstruction transformer). A forecasting model \(f\) maps the recent past \(x_{t-w:t}\) to a prediction \(\hat{x}_{t:t+h}\) of the next \(h\) samples it has not yet seen. In each case the anomaly score at time \(t\) is a residual norm:
$$s_t^{\text{recon}} = \lVert x_t - g(x)_t \rVert, \qquad s_t^{\text{fcst}} = \lVert x_t - \hat{x}_t \rVert.$$The logic is the same one that makes self-supervised pretext tasks work: a model trained to rebuild or predict healthy signals internalizes the correlations, periodicities, and cross-channel structure of the normal regime. Feed it a fault (a new harmonic, a stuck sensor, a phase slip between two motors) and it has no basis to reproduce or anticipate that pattern, so the residual grows. This is why the approach is called semi-supervised or one-class: the only label used is "this segment is normal," which is exactly the label industrial data gives you for free during commissioning and steady-state running.
Reconstruction asks "is this consistent?"; forecasting asks "is this predictable?"
The distinction is not cosmetic. Reconstruction sees the whole window at once and flags structural inconsistency: a spectral shape or channel correlation the normal manifold does not contain. It is strong on point and contextual anomalies and indifferent to when they occur. Forecasting is causal, it only sees the past, so it flags temporal surprise: a value that violates the dynamics the model learned. It excels at abrupt transients and level shifts and runs naturally online, but it degrades on signals that are inherently hard to predict (near-white noise), where even healthy data yields large residuals. Choose reconstruction when normality is a shape; choose forecasting when normality is a dynamic. Many production detectors run both and fuse the scores.
The identity-shortcut trap
The central danger of reconstruction is that a model with enough capacity learns to copy its input rather than to model the normal manifold. An autoencoder whose bottleneck is too wide, or a masked model given too much context, becomes a near-identity map: it reconstructs healthy and faulty windows with equally low error, so the residual never fires. The anomaly is reproduced perfectly and thereby hidden. This is the reconstruction analogue of overfitting, and it is silent, because training loss looks excellent exactly when the detector is worthless.
The defenses are architectural and they are why the naive "just train a big autoencoder" baseline underperforms. A tight bottleneck forces lossy compression so out-of-distribution structure cannot survive it. Denoising objectives (corrupt the input, ask for the clean version) prevent the copy solution. Adversarial reconstruction, as in the widely cited USAD detector, trains a second decoder to amplify the reconstruction error of inputs that are not clearly normal, which sharpens the boundary. Memory-augmented autoencoders (MemAE) restrict the decoder to a learned dictionary of normal prototypes so it literally cannot address a fault. Forecasting sidesteps the trap by construction: a model that has not yet seen \(x_t\) cannot copy it, which is one reason forecasting residuals are often more robust on streaming machine data even when their raw accuracy is lower.
From a residual stream to an alarm
A raw residual is not yet a detector. Three steps stand between them. First, aggregate across channels: a pump has vibration, temperature, current, and pressure, and a fault may perturb one or several. A per-channel residual normalized by that channel's healthy spread, then combined (a max for single-channel faults, a Mahalanobis-style sum when the covariance matters), keeps a small drift in one signal from being drowned out by a naturally noisy neighbor. Second, smooth in time: point residuals are jittery, so an exponentially weighted or windowed average suppresses one-sample spikes and exposes sustained excursions, which are what real degradation looks like. Third, threshold. Fit the score distribution on a held-out normal segment and set the alarm at a high quantile or a robust \(z\)-score, or feed the smoothed residual into the CUSUM change detector of Chapter 12 to catch slow drifts that never produce a single large sample.
The threshold is where honesty gets tested, and it is easy to cheat. Choosing the threshold on the same data you report metrics on, or using the classic point-adjustment trick that credits an entire anomalous segment when any single point inside it fires, can inflate F1 dramatically without a better detector underneath. We defer that reckoning to Section 37.7, but flag it here so you build the residual-to-alarm path with an honest evaluation already in mind. Calibrating the residual into a proper false-alarm rate, rather than an arbitrary cutoff, is exactly what the conformal methods of Chapter 18 provide.
import numpy as np
def forecast_residual_detector(x, w=32, k=4.0):
"""One-step forecasting detector on a 1-D signal.
Forecaster = local AR(1) fit on the trailing window (a stand-in for an
LSTM/TCN). Score = |x_t - x_hat_t|; threshold from the NORMAL warm-up only."""
n = len(x)
resid = np.zeros(n)
for t in range(w, n):
past = x[t - w:t]
# cheap one-step predictor: last value + local slope
slope = (past[-1] - past[0]) / (w - 1)
x_hat = past[-1] + slope
resid[t] = abs(x[t] - x_hat)
warm = resid[w:w + 400] # residuals over a KNOWN-normal stretch
thr = warm.mean() + k * warm.std() # robust-ish z threshold
smooth = np.convolve(resid, np.ones(8) / 8, mode="same") # temporal smoothing
alarms = np.where(smooth > thr)[0]
return smooth, thr, alarms
rng = np.random.default_rng(0)
t = np.arange(3000)
sig = np.sin(2 * np.pi * t / 60) + 0.05 * rng.standard_normal(3000) # healthy periodic
sig[2200:2260] += np.linspace(0, 1.5, 60) # injected drift fault
smooth, thr, alarms = forecast_residual_detector(sig)
print(f"threshold={thr:.3f} first alarm at t={alarms[0] if len(alarms) else None}")
The snippet above is deliberately the whole detector in one function so the three stages, forecast, residual, threshold-from-normal, are visible. Notice that the threshold is fit only on the warm-up segment, never on the fault region: that is the leakage-safe habit from Chapter 5 in miniature. Replacing the AR one-liner with a trained sequence model raises accuracy but does not change a single downstream step.
A wastewater pump station and the residual that saw it first
A municipal utility instrumented its lift-station pumps with current, vibration, and flow sensors and trained a channel-wise LSTM forecaster on three months of normal running, one model per pump. Weeks later, the forecasting residual on a single pump began climbing on the current channel while vibration and flow still looked ordinary, so no fixed threshold on any raw sensor would have tripped. The smoothed residual crossed its normal-derived alarm level nine days before the pump seized on a wrapped rag and impeller wear. The operators did not know the fault type in advance, and they had no labeled examples of it; the detector only knew that the pump had stopped behaving the way it used to. That is the entire value proposition of residual-based detection: it converts "we have never seen this failure" from a blocker into the expected case, and it localizes the surprise to a channel, which pointed maintenance straight at the motor circuit.
Let a library own the training loop, windowing, and thresholding
Hand-writing a reconstruction or forecasting detector means the model, the sliding-window tensorization, the per-channel score normalization, the smoothing, and the threshold search, easily 150 or more lines with several places for leakage and off-by-one bugs. Libraries such as PyOD (for AutoEncoder, VAE, and other one-class detectors) and time-series toolkits like Merlion, tods, and Darts wrap the full path behind a fit/score interface: you pass normal windows and get a calibrated anomaly score back. That is roughly a 150-line reduction, and the library handles the windowing and the normal-only threshold calibration that are the usual sources of silently inflated numbers.
Choosing and combining the two families
In practice the choice follows the signal and the deployment. Use reconstruction when a machine has rich multichannel structure and faults show up as broken correlations or spectral shape changes, and when you can afford to score whole windows offline. Use forecasting when you need streaming, low-latency detection and when normality is a predictable dynamic (rotating machinery, controlled processes). Reconstruction tends to win on subtle contextual anomalies that only make sense as a pattern; forecasting tends to win on abrupt onsets and to be more robust against the identity-shortcut trap. Because their blind spots differ, fusing the two scores (a simple normalized sum or a max) frequently beats either alone, and it is standard in cyber-physical detectors, where the same reconstruction-and-forecasting-residual idea reappears against process invariants in Chapter 38. What both families share, and what Section 37.5 pushes further, is that they need no fault labels, only a confident notion of normal.
Exercise: make the shortcut fail, then fix it
Start from the code above. (1) Replace the AR forecaster with an undercomplete autoencoder (bottleneck of 2) trained on healthy windows, and score by reconstruction error; confirm it flags the injected drift. (2) Now widen the bottleneck to the full window size and retrain: show that the reconstruction error on the fault drops toward the normal level, demonstrating the identity-shortcut trap. (3) Restore detection by adding a denoising objective (corrupt inputs with Gaussian noise during training) at the wide bottleneck. Report, for all three, the residual gap between the fault region and the normal region. Which architectural change bought back the most separation, and why?
Self-check
- Both families produce an anomaly score without any fault labels. What single assumption about the training data makes that possible, and how does it fail if the assumption is violated?
- Why can a forecasting detector never suffer the identity-shortcut trap, while a reconstruction autoencoder can? What one architectural change most directly prevents the trap in the reconstruction case?
- You have a smoothed residual stream. Name the two things you must decide to turn it into an alarm, and explain why fitting either of them on the test segment inflates your reported score.
What's Next
In Section 37.5, we scale the "model normal, score the residual" idea up to foundation models: instead of training one autoencoder per machine, we ask whether a large pretrained time-series or sensor model can reconstruct healthy behavior zero-shot and expose anomalies as its residuals, and what that buys (and costs) when a new machine arrives with no training data of its own.