# Autoencoder anomaly detection

> Train an autoencoder on normal data only; inputs it reconstructs badly are anomalies.

Source: https://www.mlfoundry.in/learn/autoencoder-anomaly-detection  
Section: ML Algorithms › Unsupervised · Anomaly Detection  
Level: core · 16 min read · updated 2026-10-08

**Autoencoder anomaly detection:** trains a neural network to copy its input to its output through a narrow **bottleneck**, using only normal data. The network can only afford to remember the structure normal data shares, so normal inputs come back almost unchanged while unusual ones come back distorted. The **reconstruction error** $\lVert x-\hat x\rVert^2$ is the anomaly score, and a threshold on it gives the alarm.

- **Task:** Unsupervised (or semi-supervised) anomaly / novelty detection
- **Input → output:** $x\in\mathbb{R}^d$ → score $s(x)=\lVert x-\hat x\rVert^2$, and a flag $s(x)>\tau$
- **Learns:** an encoder $x\mapsto z\in\mathbb{R}^k$ and a decoder $z\mapsto\hat x$, with $k<d$: in effect a $k$-dimensional surface (manifold) that normal data lies near
- **Training:** gradient descent (backprop) on the mean squared reconstruction error of normal examples; no anomaly labels needed
- **Prediction:** one forward pass per point, then compare its error to the threshold
- **Hyperparameters:** bottleneck size $k$, depth/width, activation, regularization (weight decay, dropout, noise), epochs, the threshold $\tau$
- **Assumes:** the training set is (almost) all normal, and normal data occupies a lower-dimensional structure that anomalies fall off

## The problem

Anomalies are rare and varied. A card-fraud team might have a few hundred confirmed frauds among millions of transactions, and next month's fraud will look different from last month's. A classifier trained on labelled anomalies only learns the kinds it has already seen. What we do have in abundance is **normal** data. So instead of describing anomalies, we describe normality, as tightly as we can, and flag whatever does not fit.

Density models (Gaussians, elliptic envelopes) do this well in a few dimensions, but they struggle with images, spectra or hundreds of correlated sensor channels. There, normal data usually lies near a curved, low-dimensional surface inside the huge input space: neighbouring pixels move together, and sensor readings are tied together by physics. An autoencoder learns that surface directly.

## The idea

Force the data through a narrow pipe. The **encoder** squeezes each $d$-dimensional input into a code $z$ with only $k<d$ numbers; the **decoder** rebuilds the input from that code. Training minimizes the rebuilding error on normal data, so the code is pushed to keep exactly the directions in which normal points differ from each other, and to drop everything else.

An anomaly differs from normal data *in the dropped directions*: that is what makes it unusual. The encoder has no slot for that difference, so the decoder rebuilds the nearest normal-looking point instead. The gap between the input and that rebuilt point is the score. Geometrically, $\hat x$ is a point on the learned manifold and $\lVert x-\hat x\rVert$ is (roughly) how far $x$ sits from it.

**Vocabulary**

- **Encoder:** $f_\theta: \mathbb{R}^d\to\mathbb{R}^k$, maps an input to its code
- **Code / latent / bottleneck:** $z=f_\theta(x)$, the $k$ numbers that must carry everything the decoder gets to see
- **Decoder:** $g_\phi:\mathbb{R}^k\to\mathbb{R}^d$, maps a code back to input space; its image is the learned manifold
- **Reconstruction:** $\hat x=g_\phi(f_\theta(x))$
- **Reconstruction error:** $\lVert x-\hat x\rVert^2$; averaged over the training set it is the loss, and for one point it is the anomaly score
- **Undercomplete:** $k<d$: the bottleneck itself prevents copying. *Overcomplete* autoencoders ($k\ge d$) need other regularization or they learn the identity
- **Threshold τ:** the error above which a point is called an anomaly; set from the error distribution of normal data

## The math

We use a single hidden (code) layer, which is all the worked example needs; deep autoencoders just stack more layers on each side and backprop through them the same way.

$$
z=g\big(W_e\,x+b_e\big),\qquad \hat x=W_d\,z+b_d
$$

- $x\in\mathbb{R}^d$: input (standardized features)
- $W_e\in\mathbb{R}^{k\times d},\ b_e\in\mathbb{R}^k$: encoder weights and bias
- $g$: activation, applied elementwise (identity for a linear autoencoder; tanh or ReLU otherwise)
- $z\in\mathbb{R}^k$: the code, with $k<d$
- $W_d\in\mathbb{R}^{d\times k},\ b_d\in\mathbb{R}^d$: decoder weights and bias
- $\hat x$: reconstruction of x

Training minimizes the mean squared reconstruction error over the $n$ normal training points:

$$
J(\theta)=\frac1n\sum_{i=1}^n e_i,\qquad e_i=\lVert \hat x_i-x_i\rVert^2=\sum_{j=1}^d(\hat x_{ij}-x_{ij})^2
$$

- $\theta$: all parameters $(W_e,b_e,W_d,b_d)$
- $e_i$: squared reconstruction error of point $i$; the same quantity is the anomaly score

Some libraries divide by $d$ as well ("MSE per feature"). That rescales every score by the same constant, so it changes the threshold's units but never which points are flagged.

## Backprop for the tiny network

For one point, write the residual $r=\hat x-x$ and the encoder's pre-activation $a=W_ex+b_e$. The chain rule, applied from the loss backwards, gives every gradient in four lines:

$$
\frac{\partial e}{\partial \hat x}=2r,\qquad \frac{\partial e}{\partial W_d}=2r\,z^\top,\qquad \frac{\partial e}{\partial b_d}=2r
$$

- $r=\hat x-x$: residual: where the reconstruction misses
- $2r\,z^\top$: outer product, a $d\times k$ matrix like $W_d$

$$
\delta=\frac{\partial e}{\partial a}=\big(W_d^\top\,2r\big)\odot g'(a),\qquad \frac{\partial e}{\partial W_e}=\delta\,x^\top,\qquad \frac{\partial e}{\partial b_e}=\delta
$$

- $\delta$: error signal at the code layer: the residual sent back through the decoder weights
- $\odot$: elementwise product
- $g'(a)$: activation slope; equals 1 for a linear autoencoder

The batch gradient of $J$ is the average of these per-point gradients, and each step updates $\theta\leftarrow\theta-\eta\,\nabla_\theta J$ with learning rate $\eta$. Notice that the decoder is told *where* the output missed ($r$), and the encoder is told how its code should have moved ($\delta$).

## Scoring and the threshold

$$
s(x)=\lVert x-\hat x\rVert^2,\qquad \text{flag } x \text{ as anomalous} \iff s(x)>\tau,\qquad \tau=Q_{q}\big(\{s(x_i)\}_{\text{normal}}\big)
$$

- $s(x)$: anomaly score: the reconstruction error of the trained network
- $\tau$: decision threshold
- $Q_q$: the $q$-th percentile (e.g. 95th or 99th) of the scores of normal points

The percentile is a direct dial on the **false-alarm rate**: at the 99th percentile about 1% of normal traffic will be flagged. Training errors are optimistic, because the network has fitted those exact points, so it is better to compute the percentile on a **held-out set of normal data**. If a validation set with some labelled anomalies exists, pick $\tau$ to optimize what you care about: recall at a fixed false-positive rate, F1, or expected cost (see precision, recall and ROC). Rules such as mean $+\,3$ standard deviations are common too, but error distributions are skewed, so percentiles are safer.

## A linear autoencoder is PCA

Take $g$ = identity. Then $\hat x=W_dW_e x+(W_db_e+b_d)$: the reconstruction is an affine map of rank at most $k$, so all reconstructions lie in a $k$-dimensional flat. Minimizing squared error over rank-$k$ maps is exactly the problem PCA solves, so (Baldi and Hornik, 1989) at the global minimum:

$$
\hat x=\bar x+U_kU_k^\top(x-\bar x),\qquad \min_\theta J=\frac{n-1}{n}\sum_{j>k}\lambda_j
$$

- $\bar x$: mean of the training data; the biases learn to pass the flat through it
- $U_k$: the top-$k$ eigenvectors of the covariance matrix (principal components)
- $\lambda_j$: eigenvalues of the covariance; the minimum loss is the variance in the dropped directions

The span of $W_d$ equals the principal subspace, but the weights themselves need not be orthonormal (any invertible $k\times k$ mixing $W_d\to W_dA$, $W_e\to A^{-1}W_e$ gives the same reconstruction). The anomaly score is then the squared distance to the principal subspace, which process engineers have used for decades as the **SPE** or **Q-statistic**. So a linear autoencoder is just a slow way to compute PCA; the reason to train a network is the nonlinear $g$, which lets the manifold *bend*.

## Worked example: a 2 → 1 → 2 autoencoder

Eight normal points lie near the line $x_2\approx x_1/2$, a 1-D manifold in 2-D. We train a linear autoencoder with a **one-number** bottleneck, then score four new points. Every number below was computed with NumPy and matches the interactive figure.

Training data (normal only), and the trained model's code, reconstruction and error for each point.

| Point | x | z | x̂ | error ‖x − x̂‖² |
| --- | --- | --- | --- | --- |
| A | (−3, −1.6) | −3.211 | (−3.046, −1.517) | 0.0090 |
| B | (−2, −0.8) | −2.022 | (−1.917, −0.952) | 0.0301 |
| C | (−1, −0.6) | −1.104 | (−1.046, −0.517) | 0.0090 |
| D | (0, 0.2) | 0.085 | (0.083, 0.048) | 0.0301 |
| E | (0.5, 0.1) | 0.454 | (0.433, 0.223) | 0.0196 |
| F | (1, 0.4) | 1.004 | (0.954, 0.483) | 0.0090 |
| G | (2, 1.2) | 2.193 | (2.083, 1.048) | 0.0301 |
| H | (3, 1.4) | 3.111 | (2.954, 1.483) | 0.0090 |

**Initialization** (fixed, deliberately poor): $W_e=(0.2,\,0.4)$, $b_e=0$, $W_d=(0.6,\,-0.2)^\top$, $b_d=(0,0)$. The decoder line $\{b_d+t\,W_d\}$ slopes *down* while the data slopes up, and the mean error is $J=3.247$.

One forward and backward pass on G = (2, 1.2) with the initial weights, then one gradient step on G alone (η = 0.05).

| Quantity | Formula | Value |
| --- | --- | --- |
| code | $z=W_e x+b_e=0.2\cdot2+0.4\cdot1.2$ | 0.88 |
| reconstruction | $\hat x=W_d z+b_d=(0.6,\,-0.2)\cdot0.88$ | (0.528, −0.176) |
| residual | $r=\hat x-x$ | (−1.472, −1.376) |
| error | $e=\lVert r\rVert^2$ | 4.0602 |
| decoder weight grad | $\partial e/\partial W_d=2r\,z$ | (−2.5907, −2.4218) |
| decoder bias grad | $\partial e/\partial b_d=2r$ | (−2.944, −2.752) |
| code error signal | $\delta=2W_d^\top r=2\,(0.6\cdot(-1.472)+(-0.2)\cdot(-1.376))$ | −1.216 |
| encoder weight grad | $\partial e/\partial W_e=\delta\,x$ | (−2.432, −1.4592) |
| encoder bias grad | $\partial e/\partial b_e=\delta$ | −1.216 |
| **after one step** | $\theta\leftarrow\theta-0.05\,\nabla_\theta e$ | $W_e=(0.3216,\,0.4730)$, $b_e=0.0608$, $W_d=(0.7295,\,-0.0789)$, $b_d=(0.1472,\,0.1376)$ |
| **new error on G** | $z=1.2716$, $\hat x=(1.0748,\,0.0373)$ | **2.2079** (was 4.0602) |

Every gradient is negative, so every weight grows: the step stretches the decoder towards G and tilts it upward ($W_d$'s second entry goes from $-0.2$ to $-0.079$). Real training averages the same gradients over all eight points per step. With full-batch gradient descent, $\eta=0.05$, 1000 epochs:

Training the 2→1→2 linear autoencoder by full-batch gradient descent. The fast phase aligns the line in about 10 epochs; the slow phase turns the oblique projection into an orthogonal one.

| Epoch | Mean error J | Decoder slope | Encoder–decoder angle |
| --- | --- | --- | --- |
| 0 | 3.2472 | −0.333 | 81.9° |
| 1 | 2.4651 | −0.167 | 64.5° |
| 3 | 0.7221 | 0.129 | 38.3° |
| 5 | 0.1172 | 0.330 | 24.5° |
| 10 | 0.0205 | 0.484 | 16.4° |
| 100 | 0.0192 | 0.501 | 12.9° |
| 1000 | 0.0182 | 0.500 | 2.0° |

Final weights: $W_e=(0.828,\,0.452)$, $b_e=-0.005$, $W_d=(0.949,\,0.475)^\top$, $b_d=(0.002,\,0.007)$. Check against PCA: the data mean is $(0.0625,\,0.0375)$, the covariance has eigenvalues $5.033$ and $0.0208$ (the first component keeps 99.6% of the variance), and the first eigenvector has slope $0.4998$. The autoencoder's line has slope $0.5000$ and passes through the mean, and its loss $0.01824$ is within 0.2% of PCA's optimum $\tfrac78\lambda_2=0.01822$. The per-point errors also match PCA's to within $0.0002$.

**Threshold.** The 95th percentile of the eight training errors is $\tau=0.0301$. (With only eight points that is essentially the maximum; with real data use thousands of held-out normal points.) Now score new points:

Held-out points scored by the trained autoencoder, τ = 0.0301.

| Point | x | z | x̂ | score | score / τ | verdict |
| --- | --- | --- | --- | --- | --- | --- |
| N | (1.5, 0.8) | 1.598 | (1.519, 0.766) | 0.0015 | 0.05 | normal |
| P | (0.5, 0.9) | 0.816 | (0.776, 0.394) | 0.332 | 11 | **anomaly** (subtle) |
| Q | (−1, 1.5) | −0.155 | (−0.146, −0.067) | 3.184 | 106 | **anomaly** |
| R | (2, −1) | 1.199 | (1.140, 0.576) | 3.224 | 107 | **anomaly** |

N lies on the manifold and reconstructs better than most training points. P, Q and R have ordinary-looking coordinates one at a time: their $x_1$ and $x_2$ values are all inside the training range. What gives them away is the *combination*, the component perpendicular to the line, which the one-number code cannot carry. The decoder snaps each one back onto the line, and the gap is the score.

> Figure: ① Training: the green decoder line swings onto the data in about 10 epochs, then the red error segments slowly turn perpendicular as the encoder direction rotates onto the decoder's. The network diagram shows the four weights, and the loss curve approaches the PCA optimum. ② Scoring: drag the test point T, move the threshold τ (log scale), and see which points fall outside the green tube where error ≤ τ. "Leak Q into training" retrains with an anomaly in the training set: the line shifts toward Q and the p95 threshold jumps from 0.030 to 1.55, so P is missed.

## The algorithm, step by step

**Autoencoder anomaly detection**

1. **Collect normal data** and remove known anomalies. Split it into train and a held-out normal set (plus a labelled validation set, if you have one).
2. **Scale** each feature using statistics from the training split only (standardize, or min-max for images).
3. **Choose an architecture** with a real bottleneck: $k$ well below $d$. Start small; MLP for tabular data, convolutional for images, LSTM/1-D conv/transformer for windows of a time series.
4. **Train** on normal data to minimize mean reconstruction error, with early stopping on the held-out normal loss.
5. **Set the threshold**: a high percentile of the held-out normal scores, or the value that best trades recall against false alarms on the labelled validation set.
6. **Score** each new input with one forward pass; flag $s(x)>\tau$. Inspect the per-feature residuals $(x_j-\hat x_j)^2$ to see *what* is abnormal.
7. **Monitor**: recompute the score distribution on recent normal traffic and retrain when normal behaviour drifts.

## In code

```python
import numpy as np

X = np.array([[-3,-1.6],[-2,-0.8],[-1,-0.6],[0,0.2],[0.5,0.1],[1,0.4],[2,1.2],[3,1.4]])
We, be = np.array([0.2, 0.4]), 0.0            # encoder 2 -> 1 (linear)
Wd, bd = np.array([0.6, -0.2]), np.zeros(2)   # decoder 1 -> 2
eta = 0.05

for epoch in range(1000):
  Z  = X @ We + be                  # codes, shape (n,)
  Xh = np.outer(Z, Wd) + bd         # reconstructions, shape (n, 2)
  R  = Xh - X                       # residuals
  D  = 2 * R @ Wd                   # delta at the code layer
  Wd -= eta * 2 * (R * Z[:, None]).mean(0)
  bd -= eta * 2 * R.mean(0)
  We -= eta * (D[:, None] * X).mean(0)
  be -= eta * D.mean()

score = lambda x: (((x @ We + be)[..., None] * Wd + bd - x) ** 2).sum(-1)
tau = np.percentile(score(X), 95)                       # 0.0301
test = np.array([[1.5, 0.8], [0.5, 0.9], [-1, 1.5], [2, -1]])
print(score(test).round(4), score(test) > tau)          # [0.0015 0.332 3.1841 3.2241] [F T T T]
```

From scratch in NumPy: the worked example above (reproduces every number in the tables).

```python
import numpy as np, torch, torch.nn as nn
from sklearn.preprocessing import StandardScaler

# X_normal: (n, d) normal rows only. X_new: rows to score.
X_tr, X_val = X_normal[:int(.8 * len(X_normal))], X_normal[int(.8 * len(X_normal)):]
scaler = StandardScaler().fit(X_tr)                     # fit on training normals only
t = lambda a: torch.tensor(scaler.transform(a), dtype=torch.float32)
Xtr, Xval = t(X_tr), t(X_val)

d, k = Xtr.shape[1], 4
model = nn.Sequential(
  nn.Linear(d, 32), nn.ReLU(), nn.Linear(32, k),        # encoder -> bottleneck
  nn.Linear(k, 32), nn.ReLU(), nn.Linear(32, d),        # decoder
)
opt = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
loader = torch.utils.data.DataLoader(Xtr, batch_size=256, shuffle=True)

def scores(x):
  model.eval()
  with torch.no_grad():
      return ((model(x) - x) ** 2).mean(dim=1).numpy()  # per-row MSE = anomaly score

best, patience = np.inf, 0
for epoch in range(200):
  model.train()
  for xb in loader:
      loss = ((model(xb) - xb) ** 2).mean()
      opt.zero_grad(); loss.backward(); opt.step()
  val = scores(Xval).mean()                             # early stopping on normal data
  if val < best - 1e-5: best, patience, state = val, 0, model.state_dict()
  elif (patience := patience + 1) >= 10: break
model.load_state_dict(state)

tau = np.percentile(scores(Xval), 99)                   # ~1% false alarms on normals
s_new = scores(t(X_new))
is_anomaly = s_new > tau
```

PyTorch: a nonlinear autoencoder for tabular data, trained on normal rows, with a held-out threshold.

> **Keras is the same recipe:** `Sequential([Dense(32, 'relu'), Dense(4), Dense(32, 'relu'), Dense(d)])`, `compile(loss='mse')`, `fit(X_tr, X_tr, validation_data=(X_val, X_val), callbacks=[EarlyStopping(restore_best_weights=True)])`. The only unusual detail is that the input is also the target.

**Strengths**

- You have lots of normal data and few or no labelled anomalies.
- Data is high-dimensional and structured (images, spectra, multichannel sensors, sequences), where density estimates break down.
- You want an explanation: per-feature or per-pixel residuals show *where* the input disagrees with normal.
- Scoring must be fast: one forward pass, easy to deploy on GPUs or edge devices.

**Limitations**

- Tabular data with a few dozen features: Isolation Forest or PCA are usually as good, much cheaper and need less tuning.
- The training data may be contaminated with anomalies: the network learns to reconstruct them too.
- You need a calibrated probability: the score is an error in feature units, not a likelihood.
- Hyperparameters (bottleneck, depth, epochs) are hard to choose without some labelled anomalies to validate on.

- **Fraud and abuse:** Reconstruct transaction or session features; unusual combinations of amount, merchant, time and device reconstruct badly.
- **Industrial sensors:** Reconstruct windows of vibration, temperature and pressure signals; a rising error warns of a failing bearing or pump before it breaks (predictive maintenance).
- **Visual inspection:** A convolutional autoencoder trained on good parts; the per-pixel error map localizes scratches and dents (benchmarks such as MVTec AD).
- **Networks and IT:** Intrusion detection from flow statistics; anomalous log or metric patterns in monitoring.
- **Medical:** Flag unusual ECG beats or scans for review, trained on healthy examples.

> **Contaminated training data:** Anomalies in the training set get learned. In the figure's "leak Q" mode, adding one anomaly to the eight training points pulls the line toward Q (its own score drops from 3.18 to 2.50) and raises the 95th-percentile threshold from 0.030 to 1.55, so the subtle anomaly P (score 0.15) is no longer flagged. Clean the training set, use robust thresholds, or iterate: train, drop the highest-error training points, retrain.

> **Too much capacity reconstructs everything:** If the bottleneck is as wide as the input, or the network is deep enough to memorize, it learns something close to the identity map, and anomalies reconstruct as well as normal points. The fix is a real bottleneck, early stopping on normal validation loss, weight decay, or a **denoising** autoencoder (corrupt the input, reconstruct the clean version). Always check that a few known or synthetic anomalies actually score high.

> **Scaling decides what counts as error:** The squared error sums over features, so a feature measured in thousands drowns one measured in fractions: the network is effectively only asked to reconstruct the large-scale features, and anomalies in the small ones go unnoticed. Standardize (fit the scaler on training normals only), and consider per-feature weights if some channels matter more.

> **Beyond plain reconstruction:** Variational autoencoders score with the reconstruction probability or ELBO; some methods add a density model on the code $z$ (deep SVDD, DAGMM), because an anomaly can sometimes reconstruct well yet land in an empty region of code space. For images, a perceptual or SSIM loss often beats pixel MSE.

**Question:** An autoencoder trained on normal data is used for anomaly detection. What is the anomaly score?

- A. The size of the code ‖z‖
- B. The reconstruction error ‖x − x̂‖²
- C. The training loss after the last epoch
- D. The predicted class probability

**Answer:** B. The reconstruction error ‖x − x̂‖² — Normal inputs lie near the learned manifold and reconstruct well; anomalies are projected onto it and reconstruct badly, so their error is large.

**Question:** A linear autoencoder with k code units is trained to its global minimum of squared error. What has it learned?

- A. The k original features with the largest variance
- B. k clusters of the data
- C. The same k-dimensional subspace as PCA's top-k components, though not necessarily orthonormal weights
- D. A nonlinear manifold that PCA cannot find

**Answer:** C. The same k-dimensional subspace as PCA's top-k components, though not necessarily orthonormal weights — The reconstruction is a rank-$k$ affine map, and the best rank-$k$ least-squares reconstruction is the projection onto the top-$k$ principal subspace. In the worked example, the AE's slope is 0.5000 vs PCA's 0.4998.

**Question:** Your autoencoder reconstructs anomalies about as well as normal points. Which change is most likely to help?

- A. Widen the bottleneck so it can represent more
- B. Train much longer
- C. Narrow the bottleneck or add regularization (weight decay, denoising, early stopping)
- D. Stop scaling the features

**Answer:** C. Narrow the bottleneck or add regularization (weight decay, denoising, early stopping) — Too much capacity lets the network approximate the identity map, which reconstructs everything. Restricting capacity forces it to keep only the structure of normal data.

**Question:** In the worked example, why does P = (0.5, 0.9) get a score 11 times the threshold even though both of its coordinates are within the training range?

- A. Its x₂ value is the largest in the data
- B. It lies off the line x₂ ≈ x₁/2: the 1-number code cannot carry the perpendicular part, so x̂ snaps back onto the line
- C. The threshold was chosen too low
- D. The network was not trained long enough

**Answer:** B. It lies off the line x₂ ≈ x₁/2: the 1-number code cannot carry the perpendicular part, so x̂ snaps back onto the line — The code keeps only the position along the line. P's perpendicular offset (about 0.58) is lost, so its error is about $0.58^2\approx0.33$, versus $\tau=0.030$.

**Key points**

- Train $x\to z\to\hat x$ through a bottleneck on normal data only; score new points by $\lVert x-\hat x\rVert^2$.
- Backprop: $\partial e/\partial W_d=2r z^\top$, $\delta=(W_d^\top 2r)\odot g'(a)$, $\partial e/\partial W_e=\delta x^\top$.
- Set τ as a high percentile of scores on held-out normal data (it fixes the false-alarm rate), or tune it on labelled validation anomalies.
- A linear autoencoder recovers PCA's subspace and the PCA reconstruction error; nonlinear activations let the manifold curve.
- Keep the training data clean, the bottleneck real and the features scaled, or the detector will quietly learn to reconstruct anomalies.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
