Alibi Detect

repository·master·Indexed 25 days ago

https://github.com/seldonio/alibi-detect

A Python library for outlier, adversarial, and drift detection supporting tabular data, text, images, and time series. It provides online and offline detectors with TensorFlow, PyTorch, and KeOps backends. The library includes various algorithms such as Isolation Forest, VAE, and MMD, as well as utilities for dataset fetching, model building blocks, and integrations with Seldon Core and KFServing.

Tokens
92.4K
Snippets
223
Records
501
Agent score
78%

What's inside alibi-detect

  1. Overview of Alibi Detect

    master
    Alibi Detect is a Python library designed for detecting outliers, adversarial examples, and distribution drift. It supports both online and offline detection across various data modalities, including tabular data, text, images, and time series. The library provides backends for both TensorFlow and PyTorch specifically for drift detection tasks.
  2. Use building block models from alibi_detect.models

    master

    Beyond detection tasks, alibi_detect provides several model implementations in alibi_detect.models that can be used as building blocks. The main TensorFlow-based implementations include PixelCNN, VAE (Variational Autoencoder), Seq2Seq (Sequence-to-sequence), and resnet.

    from alibi_detect.models.tensorflow import PixelCNN
    from alibi_detect.models.tensorflow import VAE
    from alibi_detect.models.tensorflow import Seq2Seq
    from alibi_detect.models.tensorflow import resnet
  3. Fetch datasets using alibi_detect.datasets

    master

    The alibi_detect.datasets module provides utility functions to fetch various datasets for different modalities (Sequential, Images, Tabular). Depending on the function call, you can receive either the data and labels directly or a Bunch object containing data, labels, and optional metadata. Use the return_X_y=True parameter to get a tuple of (X, y) or (X_train, y_train), (X_test, y_test).

    from alibi_detect.datasets import fetch_ecg
    
    (X_train, y_train), (X_test, y_test) = fetch_ecg(return_X_y=True)
  4. Explore the alibi-detect API Reference

    master

    The alibi-detect API is organized into several functional modules. You can find detailed documentation for each module in the API Reference. Key modules include:

    • alibi_detect.ad: Adversarial detection algorithms (e.g., adversarialae, model_distillation).
    • alibi_detect.cd: Concept Drift detection algorithms, including base classes, online detectors, and framework-specific implementations (PyTorch, TensorFlow, Scikit-learn).
    • alibi_detect.od: Outlier Detection algorithms (e.g., ae, isolationforest, vae, mahalanobis).
    • alibi_detect.models: Model architectures and trainers for PyTorch and TensorFlow.
    • alibi_detect.saving: Utilities for saving, loading, and validating detectors and their schemas.
    • alibi_detect.utils: A wide range of utility modules for data handling, distance metrics, framework-specific helpers, and visualization.
  5. Overview of Auto-Encoding Gaussian Mixture Model (AEGMM)

    master
    The Auto-Encoding Gaussian Mixture Model (AEGMM) is an unsupervised/semi-supervised outlier detector suitable for tabular and image data. It works by using an encoder to compress data and a decoder to reconstruct it. The reconstruction error (and other features) are combined with the encodings and fed into a Gaussian Mixture Model (GMM). Outliers are identified by high sample energy in the GMM.
  6. What is Model Distillation for drift detection?

    master

    Model distillation is a technique used to detect adversarial data, malicious data drift, or data corruption by comparing the output distributions (logits) of an original large model with a smaller, distilled model.

    In this context:

    • Harmful data points: Inputs where the original model is correct on uncorrupted data but incorrect on corrupted data.
    • Harmless data points: Inputs where the original model remains correct even on corrupted data.

    The detector computes an adversarial score $S(x)$ based on the loss function used for distillation between the original and distilled models. If $S(x)$ exceeds a threshold, the instance is flagged as adversarial.

  7. What is Spectral Residual outlier detection?

    master

    The Spectral Residual (SR) outlier detector is designed for unsupervised online anomaly detection in univariate time series data.

    It works by:

    1. Computing the Fourier Transform of the input data.
    2. Calculating the spectral residual of the log amplitude of the transformed signal.
    3. Applying the Inverse Fourier Transform to create a saliency map in the time domain.
    4. Computing an anomaly score based on the relative difference between the saliency map values and their moving averages.

    If the score exceeds a specified threshold, the timestep is flagged as an outlier.

  8. How the Variational Auto-Encoder (VAE) outlier detector works

    master

    The Variational Auto-Encoder (VAE) is an unsupervised or semi-supervised outlier detector suitable for tabular and image data. It is trained on a batch of unlabeled, normal (inlier) data.

    Core Mechanism: The detector attempts to reconstruct the input data. If an input cannot be reconstructed well, it results in a high reconstruction error, and the data is flagged as an outlier.

    Scoring Methods: Reconstruction error is measured using one of the following:

    1. Mean Squared Error (MSE): The MSE between the input and the reconstructed instance.
    2. Probability: The probability that both the input and the reconstructed instance are generated by the same process.

    Currently, the score_type parameter only supports the default 'mse'.

  9. Understand Detector Configuration Schemas (Unresolved vs Resolved)

    master

    Alibi Detect uses two types of configuration schemas for its detectors:

    1. Unresolved Schemas (e.g., KSDriftConfig, MMDDriftConfig): These represent the initial configuration. Except for the name and meta fields, the fields in these schemas match the detector's args and kwargs directly.
    2. Resolved Schemas (e.g., KSDriftConfigResolved, MMDDriftConfigResolved): These represent the configuration after it has been processed and resolved for use by the detector.

    Common detector config types include:

    • FETDriftOnlineConfig / FETDriftOnlineConfigResolved
    • KSDriftConfig / KSDriftConfigResolved
    • LSDDDriftConfig / LSDDDriftConfigResolved
    • LSDDDriftOnlineConfig / LSDDDriftOnlineConfigResolved
    • LearnedKernelDriftConfig / LearnedKernelDriftConfigResolved
    • MMDDriftConfig / MMDDriftConfigResolved
    • MMDDriftOnlineConfig / MMDDriftOnlineConfigResolved
    • RegressorUncertaintyDriftConfig / RegressorUncertaintyDriftConfigResolved
    • SpotTheDiffDriftConfig / SpotTheDiffDriftConfigResolved
  10. Select an algorithm for Outlier Detection

    master

    Alibi Detect provides several algorithms for outlier detection across different data types. Choose based on your data format:

    • Tabular: Isolation Forest, Mahalanobis Distance, AE, VAE, AEGMM, VAEGMM, Likelihood Ratios.
    • Image: AE, VAE, AEGMM, VAEGMM, Likelihood Ratios.
    • Time Series: Likelihood Ratios, Prophet, Spectral Residual, Seq2Seq.
    • Text: (None specifically listed in the table, but check reference list).
    • Categorical Features: Isolation Forest, Mahalanobis Distance, Likelihood Ratios.

    Some detectors support Online detection (e.g., Mahalanobis Distance, Spectral Residual) or Feature Level detection (e.g., AE, VAE, Likelihood Ratios, Spectral Residual, Seq2Seq).

  11. Likelihood Ratios (LLR) for Outlier Detection Overview

    master

    The Likelihood Ratio (LLR) outlier detector uses the log-likelihood ratio between two generative models as an outlier score. One model is trained on the original data, while a second model (model_background) is trained on a perturbed version of the dataset. This approach aims to capture background statistics while erasing semantic features through perturbations (e.g., using a Bernoulli distribution to substitute features).

    For images, the package provides a PixelCNN++ implementation available in alibi_detect.models.tensorflow.pixelcnn.