Self-supervised One-class SVM

Machine Learning
Introduced by
Golan & El-Yaniv; Ruff et al.
Year
2018
Type
Self-supervised anomaly/novelty detection
Family
Machine learning

What does it do?

Self-supervised One-class SVM learns feature embeddings from normal data alone and then fits a One-class SVM boundary in that learned space to flag out-of-distribution samples.

Training uses only normal examples, so no labeled anomalies are needed to fit the model. A small labeled set of known anomalies may still be used to choose the decision threshold.

The intuition behind the method

Suppose you have only examples of normal network traffic and want to detect intrusions. A One-class SVM trained directly on raw features may miss subtle structure in the data. Self-supervised learning addresses this by defining pretext tasks, such as predicting rotation angles, reconstructing masked patches, or discriminating contrastive pairs, that can be solved using normal data alone. Once the encoder has learned from these tasks, the One-class SVM draws its boundary in a feature space that is usually more informative than the raw inputs, which can help separate normal samples from anomalous ones.

History

The One-class SVM is a kernel-based method for novelty detection that uses only normal training samples; it was introduced by Schölkopf and colleagues. Ruff et al. (2018) proposed Deep SVDD, a hypersphere-based deep one-class method that does not use a pretext task. Pairing self-supervised pretext tasks with one-class objectives developed as a separate line of work alongside these deep one-class methods.

When to use it

Choose Self-supervised One-class SVM when the training data is normal-only or predominantly normal, labeled anomalies are rare or unavailable, and the goal is to detect departures from a known normal distribution. Typical settings include fraud screening, industrial fault detection, medical image screening, and network intrusion detection. It is most useful when the raw inputs are high-dimensional or unstructured (images, time series, text), where a One-class SVM on raw features is unlikely to capture the relevant structure. Do not use it when a balanced labeled dataset with many anomaly examples is available, since binary or multiclass classification is then more appropriate. It is also a poor choice when the dataset is too small to train a meaningful encoder, or when stakeholders need interpretable feature coefficients. The required amount of training data depends on the data modality and the encoder, so it should be checked empirically on held-out normal data.

Limitations and considerations

  • A meaningful encoder requires enough normal training data; performance is expected to drop when the dataset is small.
  • Training the encoder and the One-class SVM jointly is non-trivial and may require careful optimization.
  • The choice of pretext task depends on the domain and can strongly affect how well anomalies are separated from normal samples.
  • The learned embedding is usually harder to interpret than a One-class SVM on the original features, because its dimensions have no direct meaning.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Training needs only normal examples, so the method fits one-class settings where anomalies have not been labeled.
  • Pretext tasks allow the encoder to learn features from raw normal data that a One-class SVM on the original inputs cannot access directly.
  • The encoder can be adapted to the data modality (images, tabular data, or sequences) by choosing a suitable architecture and pretext task.
  • The nu parameter bounds the fraction of training points treated as outliers and the fraction of support vectors, giving direct control over how tightly the boundary fits the training data.
  • Large volumes of unlabeled normal data can be used to improve the embedding without annotation cost.

Applications

  • Industrial defect detection on manufacturing imagery where only defect-free samples are available at training time.
  • Network intrusion detection using only normal traffic logs, flagging previously unseen attack patterns.
  • Medical anomaly screening to identify pathological samples among predominantly healthy patient records.
  • Fraud detection in financial transactions where confirmed fraud cases are too scarce to train a binary classifier.
  • Autonomous system monitoring to detect out-of-distribution sensor readings without labeled failure data.

How it works

How it works

1

A pretext task creates a supervision signal from unlabeled normal data, so no manual labels are needed. Common choices in the literature include geometric transformation prediction, masked feature reconstruction, and contrastive instance discrimination. The task pushes the model to learn structure that is shared by the normal samples.

2

A neural encoder (convolutional, transformer, or MLP) is trained to solve the pretext task using only normal training instances. The aim is an embedding in which normal samples are grouped together and structurally similar inputs lie close to one another. The result is a learned feature representation derived from the data's own structure rather than hand-designed features.

3

With the encoder weights fixed, or optimized jointly with the boundary, a One-class SVM is fitted in the embedding space using only normal training examples. The method finds a maximum-margin hyperplane that separates the normal embeddings from the origin, as expressed by the objective above. The nu parameter controls the trade-off: it upper-bounds the fraction of training points treated as outliers and lower-bounds the fraction of support vectors.

Formula
\min_{\mathbf{w},\rho,\xi} \frac{1}{2}\|\mathbf{w}\|^2 - \rho + \frac{1}{\nu n}\sum_i \xi_i
4

For a new input, the encoder maps it into the embedding space and the One-class SVM computes its decision function, f(x) = w^T phi(x) - rho. Negative values fall on the outlier side of the boundary; larger magnitudes indicate a greater deviation from the normal region.

Formula
f(\mathbf{x}) = \mathbf{w}^\top \phi(\mathbf{x}) - \rho
5

A decision threshold converts scores into flags. It is often set at a chosen quantile of the training anomaly scores, or tuned on a small labeled validation set of known anomalies when one is available. Samples scoring below the threshold are flagged as anomalous or novel. The threshold determines the trade-off between false positives and false negatives, so it should be calibrated for the intended use rather than left at a default.

Common pitfalls

  • Choosing a pretext task that is too easy or unrelated to the anomaly structure, so the embedding does not separate normal samples from anomalies.
  • Including anomalies in the pretext-task training data, which distorts the learned normal manifold.
  • Tuning the nu parameter on the same data used to train the encoder, which can make anomaly scores look better than they will be on new data.
  • Keeping the default decision boundary instead of calibrating the threshold to the acceptable false-positive rate for the application.

Frequently asked questions

What pretext tasks work best for tabular data?

For tabular data, candidate pretext tasks include feature masking and reconstruction, contrastive learning with feature corruptions (for example SCARF), and predicting permuted feature subsets. Which task works best depends on the dataset and should be checked empirically, since no general ranking is established here.

How does this differ from a standard Autoencoder anomaly detection?

Autoencoder anomaly detection relies on reconstruction error as the anomaly score, which can fail when anomalies are also easy to reconstruct. Self-supervised OC-SVM instead learns embeddings with a pretext objective and uses a margin-based boundary in that space, which may be less sensitive to this failure mode.

Can I use a pretrained model (e.g., ImageNet encoder) instead of training the pretext task from scratch?

Yes. Using a pretrained encoder and fitting OC-SVM in its embedding space is a common shortcut when labeled anomalies are absent and the domain is close to the pretraining data. Domain shift between the pretraining data and the target domain can degrade results.

How do I choose the nu hyperparameter for OC-SVM?

Nu is an upper bound on the fraction of training points treated as outliers and a lower bound on the fraction of support vectors. If the training set is expected to be clean, use a small nu; if some contamination is expected, increase it. Validate the choice on a held-out set, and on known anomalies if any are available.

Is a GPU required?

A GPU is strongly recommended for training the self-supervised encoder on image or sequence data. For moderate-sized tabular datasets, a CPU-based encoder followed by a scikit-learn One-class SVM is often sufficient.

Related methods

Sources

  1. Golan, I. & El-Yaniv, R. (2018). Deep One-Class Classification. Proceedings of the 35th International Conference on Machine Learning (ICML), PMLR 80, 1747–1756.

    Open publication reference
  2. Ruff, L., Vandermeulen, R., Goernitz, N., Deecke, L., Siddiqui, S. A., Binder, A., Muller, E. & Kloft, M. (2018). Deep One-Class Classification. Proceedings of the 35th International Conference on Machine Learning (ICML), PMLR 80, 4393–4402.

    Open publication reference