GANomalyDetector<T>
Models & Types · AiDotNet.AnomalyDetection.NeuralNetwork
Implements GANomaly for anomaly detection using GAN-based reconstruction.
Inheritance: ModelBase<T, Matrix<T>, Vector<T>> → AnomalyDetectorBase<T> → GANomalyDetector<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
GANomaly learns to encode, decode, and re-encode data. Anomalies are detected when the encoding of the original differs from the encoding of the reconstruction, indicating the model cannot properly represent the data.
How It Works
The algorithm works by:
- Encoder maps input to latent space z
- Decoder reconstructs from z
- Second encoder re-encodes the reconstruction
- A discriminator D (h = LeakyReLU(x W1 + b1), p = sigmoid(h W2 + b2)) tells real rows from
reconstructions; its hidden layer h is the feature map f(.) of the adversarial loss
- The generator minimizes w_adv ||f(x) - f(x’)||^2 + w_con |x - x’| + w_enc ||z - z’||^2 (element means,
w_adv = 1, w_con = 50, w_enc = 1) and the discriminator minimizes the binary cross-entropy of real rows as 1 and reconstructions as 0, both with Adam (beta1 = 0.5, beta2 = 0.999) as in the reference code
- Anomaly score is the mean squared difference between the encoding and the re-encoding
When to use:
- Image anomaly detection
- When reconstruction error alone is insufficient
- Semi-supervised anomaly detection with only normal examples
Industry Standard Defaults:
- Latent dimensions: 32
- Hidden dimensions: 64
- Epochs: 100
- Learning rate: 0.0002 (Adam, beta1 = 0.5)
- Batch size: 64
- Loss weights: adversarial 1, contextual 50, encoder 1
- Contamination: 0.1 (10%)
Reference: Akcay, S., Atapour-Abarghouei, A., and Breckon, T. P. (2018). “GANomaly: Semi-Supervised Anomaly Detection via Adversarial Training.” ACCV.
Constructors
| Constructor | Summary |
|---|---|
GANomalyDetector(int latentDim, int hiddenDim, int epochs, double learningRate, double contamination, int randomSeed) |
Creates a new GANomaly anomaly detector with the default loss weights (1 adversarial, 50 contextual, 1 encoder); keeps the shipped six-parameter signature binary compatible. |
GANomalyDetector(int latentDim, int hiddenDim, int epochs, double learningRate, double contamination, int randomSeed, double adversarialWeight, double contextualWeight, double encoderWeight) |
Creates a new GANomaly anomaly detector. |
Properties
| Property | Summary |
|---|---|
int HiddenDim { get; } |
Gets the hidden dimensions. |
int LatentDim { get; } |
Gets the latent dimensions. |
Methods
| Method | Summary |
|---|---|
Vector<T> ScoreAnomalies(Matrix<T> X) |
|
void Fit(Matrix<T> X) |