Techniqueadvanced
Autoencoder anomaly detection
Also called reconstruction error anomaly detection.
Trains an autoencoder on normal data and flags inputs whose reconstruction error is large.
An autoencoder is a model that maps an input through a narrow middle layer, the bottleneck, and back to the input space, and that is trained to give back its input. Trained only on normal data, it learns to rebuild inputs that look like the training data and does poorly on the others. The technique uses the reconstruction error, the distance between an input and its rebuilt version, as the anomaly score, and flags inputs whose error is above a threshold. It is a general recipe for the anomaly detection problem: the holes are the architecture, the loss, the training and the threshold. Its linear form is the PCA subspace method.
In plain words
Teach a copier to copy one kind of document, through a narrow channel that cannot carry every detail. To fit through the channel, the copier must learn what such documents usually look like and keep only what tells them apart. Give it a normal document and the copy is close. Give it something of a kind it never saw and the copy comes out wrong, because the copier fills in the gaps with what is usual. So a bad copy is a sign that the input is unusual. The size of the difference is the score.
When it applies
- There is much normal data and few or no labeled anomalies, so a model of the normal class is the only option.
- The normal data lie near a set of much lower dimension than the input space, such as a curved surface, so a bottleneck can describe them. Correlated sensor readings, flow features of network traffic and images of one kind are typical.
- The anomalies differ from the normal data in a way the bottleneck cannot express. If they lie on the same surface, as a rare but valid combination does, the error does not see them.
- A threshold can be set on held-out normal data, and a false alarm rate measured on it is an acceptable guide.
The template
AE-DETECT(normal training set X, held-out normal set V, test set Z)- 1
scale each feature with statistics of X ▷ hole SCALE: z-scores, min-max, or a log for counts - 2
choose an encoder f and a decoder g ▷ hole ARCHITECTURE: layer sizes, bottleneck width r - 3
train f and g to minimize Σ_{x ∈ X} LOSS(x, g(f(x))) ▷ hole LOSS: squared error; hole OPTIMIZER: SGD or Adam - 4
τ ← a quantile of {LOSS(v, g(f(v))) : v ∈ V} ▷ hole THRESHOLD: the 1 - α quantile for a target rate α - 5
for each z in Z do - 6
e ← LOSS(z, g(f(z))) ▷ the reconstruction error, the score of z - 7
if e > τ then flag z
Why it works, in the linear case. Let and with an and an matrix, the data centered, and LOSS the squared error. Baldi and Hornik (1989) showed, for a covariance with distinct eigenvalues, that the training loss has no local minimum other than the global one, where all other critical points are saddle points, and that at the minimum is the orthogonal projection onto the span of the top principal axes of the data. So the reconstruction error of is , the squared prediction error of the PCA subspace method. The linear autoencoder and PCA give the same score, and PCA computes it without training.
Beyond the linear case. With nonlinear layers the bottleneck can follow a curved surface, and the score is the distance to the learned surface along the decoder. There is no theorem that this surface is the right one, or that anomalies are far from it. The quality of the detector is a measurement on held-out data, which makes this part a heuristic.
Walk-through. Take the three links and eight time bins of the example on the PCA page, with mean , and a linear autoencoder with and the squared error. At the minimum, projects onto . The bin is rebuilt exactly and scores 0. The bin is rebuilt as , and its error is . The training bins score 0.08 or 0.06. With the largest error on normal bins, the second bin is flagged and the first is not, as on the PCA page.
Classic examples
- The PCA subspace method. The PCA subspace method of Lakhina et al. (2004) is the linear autoencoder with an analytic threshold, the Q-statistic, in place of a quantile.
- Replicator neural networks. Hawkins et al. (2002) trained a network with three hidden layers to reproduce its input on records of a data set, and used the mean squared reconstruction error of each record as its outlier factor.
- Network intrusion detection. Systems such as Kitsune (Mirsky et al. 2018) train small autoencoders online on features of network flows and alert on a high reconstruction error. They are fast and need no labels, and they share the false alarm problem of all anomaly detectors that the base rate fallacy explains.
- Variational autoencoders. A variational autoencoder gives a probability model, so the score can be the probability of the input under the model or the expected reconstruction error over the latent code, which takes the noise of each feature into account.
- Kernel PCA. The reconstruction error in the feature space of kernel PCA is a nonlinear score without a neural network, and with a fixed solution.
Variations
- Denoising and sparse autoencoders. Training to rebuild clean inputs from noisy ones, or with a penalty on the activity of the middle layer, makes the model less able to copy every input, which can widen the gap between normal and anomalous errors.
- Per-feature errors. The error of each feature, not only the sum, shows which features break the pattern, which helps the analyst who reads the alarm.
- Robust training. When the training data hold some anomalies, robust losses or the split of the data into a low-rank part and a sparse part, as in robust deep autoencoders, keep the anomalies out of the model.
- Deep one-class models. Deep SVDD trains a network so that normal points map close to a fixed center, and uses the distance to the center as the score, a deep form of the support vector data description.
- Sequence models. For time series, a recurrent or convolutional autoencoder rebuilds a window of values, and the error of each window is the score.
Pitfalls
- The model rebuilds the anomalies too. A large network with a wide bottleneck can learn to copy any input. Then anomalies also get a small error at line L6. Keep the bottleneck narrow at line L2, and check on held-out data with some anomalies that their errors are larger.
- Polluted training data. If the set at line L3 holds anomalies, the model learns to rebuild them. Clean the data or use robust training.
- Features of different scales. The squared error at line L3 is dominated by the features with the largest values. Scale the features at line L1, with statistics of the training data only.
- A threshold without held-out data. A threshold taken from the training errors at line L4 is too small, because the model fits the training data better than new data. Use a separate normal set .
- The score is not a probability. The error at line L6 has no calibrated meaning. A false alarm rate measured on holds only while new normal data look like , and the drift of normal behavior raises it.
- Adaptive adversaries. An input made to have a small reconstruction error passes the detector. The score is a detector of the unusual, not a defense against an adversary who knows the model.
Exercises
- Show that for a linear autoencoder with , the reconstruction error of is , with the principal axes. Answer. Write . Then , so , and by Parseval its squared norm is .
- Why does a linear autoencoder with find no anomaly? Answer. With a bottleneck as wide as the input, can be the identity, so every input is rebuilt exactly and every error is 0.
- A detector flags 1% of normal inputs on . On a stream of normal inputs and 10 attacks a day, all of which it flags, what fraction of the alarms is real? Answer. About false alarms and 10 true ones, so .
- In the walk-through, the bin has error 0.1067. Is it flagged with ? Is it anomalous? Answer. Yes, . Whether it is anomalous depends on what the operator wants to see. The threshold taken as the largest training error is tight, and a few such bins are false alarms; the Q-statistic of the PCA page, 0.376, does not flag it.
References
- Varun Chandola, Arindam Banerjee and Vipin Kumar, Anomaly detection, a survey, ACM Computing Surveys 41(3), article 15, 2009, the section on neural network based methods, which covers replicator neural networks and the reconstruction error as a score
- Simon Hawkins, Hongxing He, Graham Williams and Rohan Baxter, Outlier detection using replicator neural networks, DaWaK 2002, LNCS 2454, pages 170 to 180, a network trained to reproduce its input, with the reconstruction error of each record as its outlier factor
- Guansong Pang, Chunhua Shen, Longbing Cao and Anton van den Hengel, Deep learning for anomaly detection, a review, ACM Computing Surveys, 2021, the part on autoencoders and the reconstruction error, with the known weaknesses of the approach
- Pierre Baldi and Kurt Hornik, Neural networks and principal component analysis, learning from examples without local minima, Neural Networks 2(1), pages 53 to 58, 1989, the auto-associative case, where the unique minimum of the squared error is the projection onto the leading principal subspace and all other critical points are saddle points
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 0JEWJC Edit this page History Report an error Markdown