Alibi Detect 是一个源码可用的 Python 库,专注于异常值、对抗性和漂移检测。该包旨在覆盖适用于表格数据、文本、图像和时间序列的在线和离线检测器。漂移检测同时支持 TensorFlow 和 PyTorch 后端。
如需了解更多有关在生产环境中监测异常值和分布的重要性的背景信息,请查看来自 ICML 2020 研讨会 机器学习系统的部署与监测挑战 上的这个演讲,该演讲基于论文生产环境中模型的监测与可解释性并引用了 Alibi Detect。
如需全面了解漂移检测,请观看保护你的机器学习免受漂移影响:入门。该演讲涵盖了漂移是什么以及为什么值得检测、漂移的不同类型、如何以系统的方式检测漂移,并剖析了漂移检测器的内部构造。
该包 alibi-detect 可以从以下位置安装:
pip)conda/mamba)Prophet 时间序列异常值检测器: ```bash
pip install alibi-detect[prophet]
要从 conda-forge 安装,建议使用 mamba, 可以通过以下命令将其安装到 base conda 环境中:```bash conda install mamba -n base -c conda-forge
要安装 alibi-detect:```bash
mamba install -c conda-forge alibi-detect
我们将使用 VAE 异常检测器 来说明该 API。```python from alibi_detect.od import OutlierVAE from alibi_detect.saving import save_detector, load_detector
od = OutlierVAE(threshold=0.1, encoder_net=encoder_net, decoder_net=decoder_net, latent_dim=1024) od.fit(x_train)
preds = od.predict(x_test)
filepath = './my_detector/' save_detector(od, filepath) od = load_detector(filepath)
预测结果以字典形式返回,键为 `meta` 和 `data`。`meta` 包含检测器的元数据,而 `data` 本身是一个包含实际预测结果的字典。其中包含异常、对抗或漂移的分数与阈值,以及诸如实例是否为异常值之类的预测。不同方法的具体细节可能略有差异,因此我们建议读者熟悉[支持的算法类型](https://docs.seldon.io/projects/alibi-detect/en/stable/overview/algorithms.html)。
## 支持的算法
下表展示了每种算法的建议使用场景。*特征级别* 列表示检测是否可以在特征级别上执行,例如对图像的逐像素检测。有关更多信息,请查阅[算法参考列表](#reference-list),其中包含文档和原始论文的链接以及各检测器的示例。
### 异常检测
| 检测器 | 表格数据 | 图像 | 时间序列 | 文本 | 类别特征 | 在线 | 特征级别 |
|:---------------------|:-------:|:-----:|:-----------:|:----:|:--------------------:|:------:|:-------------:|
| Isolation Forest | ✔ | | | | ✔ | | |
| Mahalanobis Distance | ✔ | | | | ✔ | ✔ | |
| AE | ✔ | ✔ | | | | | ✔ |
| VAE | ✔ | ✔ | | | | | ✔ |
| AEGMM | ✔ | ✔ | | | | | |
| VAEGMM | ✔ | ✔ | | | | | |
| Likelihood Ratios | ✔ | ✔ | ✔ | | ✔ | | ✔ |
| Prophet | | | ✔ | | | | |
| Spectral Residual | | | ✔ | | | ✔ | ✔ |
| Seq2Seq | | | ✔ | | | | ✔ |
### 对抗性检测
| 检测器 | 表格数据 | 图像 | 时间序列 | 文本 | 类别特征 | 在线 | 特征级别 |
| :--- | :---: | :---: |:-----------:|:----:|:--------------------:|:------:|:-------------:|
| Adversarial AE | ✔ | ✔ | | | | | |
| Model distillation | ✔ | ✔ | ✔ | ✔ | ✔ | | |
### 漂移检测
| 检测器 | 表格数据 | 图像 | 时间序列 | 文本 | 类别特征 | 在线 | 特征级别 |
|:---------------------------------| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| Kolmogorov-Smirnov | ✔ | ✔ | | ✔ | ✔ | | ✔ |
| Cramér-von Mises | ✔ | ✔ | | | | ✔ | ✔ |
| Fisher's Exact Test | ✔ | | | | ✔ | ✔ | ✔ |
| Maximum Mean Discrepancy (MMD) | ✔ | ✔ | | ✔ | ✔ | ✔ | |
| Learned Kernel MMD | ✔ | ✔ | | ✔ | ✔ | | |
| Context-aware MMD | ✔ | ✔ | ✔ | ✔ | ✔ | | |
| Least-Squares Density Difference | ✔ | ✔ | | ✔ | ✔ | ✔ | |
| Chi-Squared | ✔ | | | | ✔ | | ✔ |
| Mixed-type tabular data | ✔ | | | | ✔ | | ✔ |
| Classifier | ✔ | ✔ | ✔ | ✔ | ✔ | | |
| Spot-the-diff | ✔ | ✔ | ✔ | ✔ | ✔ | | ✔ |
| Classifier Uncertainty | ✔ | ✔ | ✔ | ✔ | ✔ | | |
| Regressor Uncertainty | ✔ | ✔ | ✔ | ✔ | ✔ | | |
#### TensorFlow 和 PyTorch 支持
漂移检测器支持 TensorFlow、PyTorch 以及(在适用的情况下)[KeOps](https://www.kernel-operations.io/keops/index.html) 后端。
但是,Alibi Detect 默认不会安装这些。有关更多详细信息,请参阅[安装选项](#installation-and-usage)。```python
from alibi_detect.cd import MMDDrift
cd = MMDDrift(x_ref, backend='tensorflow', p_val=.05)
preds = cd.predict(x)
在 PyTorch 中的同一个检测器:```python cd = MMDDrift(x_ref, backend='pytorch', p_val=.05) preds = cd.predict(x)
或者在 KeOps 中:```python
cd = MMDDrift(x_ref, backend='keops', p_val=.05)
preds = cd.predict(x)
Alibi Detect 还提供了各种预处理步骤,例如随机初始化的编码器、预训练文本 嵌入,用于使用 transformers 库检测漂移,以及 从机器学习模型中提取隐藏层。这使得能够检测不同类型的漂移,例如 协变量偏移和预测分布偏移。这些预处理步骤同样在 TensorFlow 和 PyTorch 中得到支持。```python from alibi_detect.cd.tensorflow import HiddenOutput, preprocess_drift
model = # TensorFlow model; tf.keras.Model or tf.keras.Sequential preprocess_fn = partial(preprocess_drift, model=HiddenOutput(model, layer=-1), batch_size=128) cd = MMDDrift(x_ref, backend='tensorflow', p_val=.05, preprocess_fn=preprocess_fn) preds = cd.predict(x)
请参阅示例笔记本(例如 [CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_mmd_cifar10.html)、[电影评论](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_text_imdb.html))以了解更多详情。
### 参考列表
#### 离群点检测
- [Isolation Forest](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/iforest.html) ([FT Liu et al., 2008](https://cs.nju.edu.cn/zhouzh/zhouzh.files/publication/icdm08b.pdf))
- 示例:[网络入侵](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_if_kddcup.html)
- [Mahalanobis Distance](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/mahalanobis.html) ([Mahalanobis, 1936](https://insa.nic.in/writereaddata/UpLoadedFiles/PINSA/Vol02_1936_1_Art05.pdf))
- 示例:[网络入侵](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_mahalanobis_kddcup.html)
- [Auto-Encoder (AE)](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/ae.html)
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_ae_cifar10.html)
- [Variational Auto-Encoder (VAE)](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/vae.html) ([Kingma et al., 2013](https://arxiv.org/abs/1312.6114))
- 示例:[网络入侵](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_vae_kddcup.html)、[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_vae_cifar10.html)
- [Auto-Encoding Gaussian Mixture Model (AEGMM)](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/aegmm.html) ([Zong et al., 2018](https://openreview.net/forum?id=BJJLHbb0-))
- 示例:[网络入侵](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_aegmm_kddcup.html)
- [Variational Auto-Encoding Gaussian Mixture Model (VAEGMM)](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/vaegmm.html)
- 示例:[网络入侵](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_aegmm_kddcup.html)
- [Likelihood Ratios](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/llr.html) ([Ren et al., 2019](https://arxiv.org/abs/1906.02845))
- 示例:[基因组](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_llr_genome.html)、[Fashion-MNIST 对 MNIST](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_llr_mnist.html)
- [Prophet Time Series Outlier Detector](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/prophet.html) ([Taylor et al., 2018](https://peerj.com/preprints/3190/))
- 示例:[天气预报](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_prophet_weather.html)
- [Spectral Residual Time Series Outlier Detector](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/sr.html) ([Ren et al., 2019](https://arxiv.org/abs/1906.03821))
- 示例:[合成数据集](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_sr_synth.html)
- [Sequence-to-Sequence (Seq2Seq) Outlier Detector](https://docs.seldon.io/projects/alibi-detect/en/stable/od/methods/seq2seq.html) ([Sutskever et al., 2014](https://papers.nips.cc/paper/5346-sequence-to-sequence-learning-with-neural-networks.pdf); [Park et al., 2017](https://arxiv.org/pdf/1711.00614.pdf))
- 示例:[ECG](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_seq2seq_ecg.html)、[合成数据集](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/od_seq2seq_synth.html)
#### 对抗检测
- [Adversarial Auto-Encoder](https://docs.seldon.io/projects/alibi-detect/en/stable/ad/methods/adversarialae.html) ([Vacanti and Van Looveren, 2020](https://arxiv.org/abs/2002.09364))
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/ad_ae_cifar10.html)
- [Model distillation](https://docs.seldon.io/projects/alibi-detect/en/stable/ad/methods/modeldistillation.html)
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_distillation_cifar10.html)
#### 漂移检测
- [Kolmogorov-Smirnov](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/ksdrift.html)
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_ks_cifar10.html)、[分子图](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_mol.html)、[电影评论](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_text_imdb.html)
- [Cramér-von Mises](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/cvmdrift.html)
- 示例:[企鹅](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_supervised_penguins.html)
- [Fisher's Exact Test](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/fetdrift.html)
- 示例:[企鹅](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_supervised_penguins.html)
- [Least-Squares Density Difference](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/lsdddrift.html) ([Bu et al, 2016](https://alippi.faculty.polimi.it/articoli/A%20Pdf%20free%20Change%20Detection%20Test%20Based%20on%20Density%20Difference%20Estimation.pdf))
- [Maximum Mean Discrepancy](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/mmddrift.html) ([Gretton et al, 2012](http://jmlr.csail.mit.edu/papers/v13/gretton12a.html))
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_mmd_cifar10.html)、[分子图](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_mol.html)、[电影评论](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_text_imdb.html)、[亚马逊评论](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_text_amazon.html)
- [Learned Kernel MMD](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/learnedkerneldrift.html) ([Liu et al, 2020](https://arxiv.org/abs/2002.09116))
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_clf_cifar10.html)
- [Context-aware MMD](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/contextmmddrift.html) ([Cobb and Van Looveren, 2022](https://arxiv.org/abs/2203.08644))
- 示例:[ECG](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_context_ecg.html)、[新闻主题](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_context_20newsgroup.html)
- [Chi-Squared](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/chisquaredrift.html)
- 示例:[收入预测](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_chi2ks_adult.html)
- [Mixed-type tabular data](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/tabulardrift.html)
- 示例:[收入预测](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_chi2ks_adult.html)
- [Classifier](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/classifierdrift.html) ([Lopez-Paz and Oquab, 2017](https://openreview.net/forum?id=SJkXfE5xx))
- 示例:[CIFAR10](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_clf_cifar10.html)、[亚马逊评论](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_text_amazon.html)
- [Spot-the-diff](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/spotthediffdrift.html) (adaptation of [Jitkrittum et al, 2016](https://arxiv.org/abs/1605.06796))
- 示例 [MNIST 与葡萄酒质量](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/spot_the_diff_mnist_win.html)
- [Classifier and Regressor Uncertainty](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/modeluncdrift.html)
- 示例:[CIFAR10 与葡萄酒](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_model_unc_cifar10_wine.html)、[分子图](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_mol.html)
- [Online Maximum Mean Discrepancy](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/onlinemmddrift.html)
- 示例:[葡萄酒质量](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_online_wine.html)、[Camelyon 医学影像](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_online_camelyon.html)
- [Online Least-Squares Density Difference](https://docs.seldon.io/projects/alibi-detect/en/stable/cd/methods/onlinemmddrift.html) ([Bu et al, 2017](https://ieeexplore.ieee.org/abstract/document/7890493))
- 示例:[葡萄酒质量](https://docs.seldon.io/projects/alibi-detect/en/stable/examples/cd_online_wine.html)
## 数据集
该软件包还在 `alibi_detect.datasets` 中提供了相应功能,可轻松获取不同模态的多个数据集。对于每个数据集,返回数据与标签,或返回包含数据、标签和可选元数据的 *Bunch* 对象。示例:```python
from alibi_detect.datasets import fetch_ecg
(X_train, y_train), (X_test, y_test) = fetch_ecg(return_X_y=True)
基因组数据集:fetch_genome
(X_train, y_train), (X_val, y_val), (X_test, y_test) = fetch_genome(return_X_y=True)
ECG 5000: fetch_ecg
NAB: fetch_nab
alibi_detect.datasets.get_list_nab() 获取可用时间序列的列表。CIFAR-10-C: fetch_cifar10c
fetch_cifar10c 允许你选择任意严重程度或损坏类型。可用的损坏类型列表可以通过 alibi_detect.datasets.corruption_types_cifar10c() 获取。该数据集可用于鲁棒性和漂移方面的研究。原始数据可以在这里找到。示例: ```python
from alibi_detect.datasets import fetch_cifar10ccorruption = ['gaussian_noise', 'motion_blur', 'brightness', 'pixelate'] X, y = fetch_cifar10c(corruption=corruption, severity=5, return_X_y=True)
对抗性 CIFAR-10: fetch_attack
(X_train, y_train), (X_test, y_test) = fetch_attack('cifar10', 'resnet56', 'cw', return_X_y=True)
fetch_kdd
fetch_kdd 允许您选择网络入侵的一个子集作为目标,或仅挑选指定的特征。原始数据可在此处找到。在 alibi_detect.models 下可以找到在离群点、对抗性或漂移检测之外也可能有用的模型和/或构建模块。主要实现:
PixelCNN++: alibi_detect.models.pixelcnn.PixelCNN
变分自编码器:alibi_detect.models.autoencoder.VAE
序列到序列模型:alibi_detect.models.autoencoder.Seq2Seq
ResNet:alibi_detect.models.resnet
model = fetch_tf_model('cifar10', 'resnet32')
Alibi-detect 已集成到机器学习模型部署平台 Seldon Core 和模型服务框架 KFServing 中。
如果您在研究中使用 alibi-detect,请考虑引用它。
BibTeX 条目:``` @software{alibi-detect, title = {Alibi Detect: Algorithms for outlier, adversarial and drift detection}, author = {Van Looveren, Arnaud and Klaise, Janis and Vacanti, Giovanni and Cobb, Oliver and Scillitoe, Ashley and Samoilescu, Robert and Athorne, Alex}, url = {https://github.com/SeldonIO/alibi-detect}, version = {0.13.0}, date = {2025-12-11}, year = {2019} }