Alibi Detect是一个专注于异常值、对抗性和漂移检测的 Python 库。该软件包旨在涵盖表格数据、文本、图像和时间序列的在线和离线检测器。TensorFlow和PyTorch后端都支持漂移检测。
有关在生产环境中监控异常值和分布的重要性的更多背景信息,请查看ICML 2020部署和监控机器学习系统挑战研讨会中的演讲,该演讲基于生产中模型的监控和可解释性论文并引用 Alibi Detect。
有关偏差检测的完整介绍,请查看保护机器学习免受偏差:简介。该演讲涵盖了什么是漂移、为什么需要检测它、不同类型的漂移、如何以原则性的方式检测它,还描述了漂移检测器的解剖结构。
该软件包alibi-detect
可以从以下位置安装:
- PyPI 或 GitHub 源(带有
pip
) - 蟒蛇(带
conda
/mamba
)
-
pip install alibi-detect
-
或者,可以安装开发版本:
pip install git+https://github.com/SeldonIO/alibi-detect.git
-
要与 TensorFlow 后端一起安装:
pip install alibi-detect[tensorflow]
-
使用 PyTorch 后端安装:
pip install alibi-detect[torch]
-
使用 KeOps 后端安装:
pip install alibi-detect[keops]
-
要使用
Prophet
时间序列异常值检测器:pip install alibi-detect[prophet]
要从conda-forge安装,建议使用mamba,可以使用以下命令将其安装到基础conda 环境中:
conda install mamba -n base -c conda-forge
要安装 alibi-Detect:
mamba install -c conda-forge alibi-detect
我们将使用VAE 离群值检测器来说明 API。
from alibi_detect.od import OutlierVAE from alibi_detect.saving import save_detector, load_detector# initialize and fit detector od = OutlierVAE(threshold=0.1, encoder_net=encoder_net, decoder_net=decoder_net, latent_dim=1024) od.fit(x_train)
# make predictions preds = od.predict(x_test)
# save and load detectors filepath = './my_detector/' save_detector(od, filepath) od = load_detector(filepath)
预测结果在字典中返回,键为meta
和data
。meta
包含检测器的元数据,而data
其本身就是一个包含实际预测的字典。它包含离群值、对抗性或漂移分数和阈值以及实例是否为离群值的预测。不同方法的具体细节可能略有不同,因此我们鼓励读者熟悉支持的算法类型。
下表显示了每种算法的建议用例。特征级别列指示是否可以在特征级别(例如图像的每个像素)完成检测。检查算法参考列表以获取更多信息,其中包含文档和原始论文的链接以及每个检测器的示例。
探测器 | 表格 | 图像 | 时间序列 | 文本 | 分类特征 | 在线的 | 功能级别 |
---|---|---|---|---|---|---|---|
隔离森林 | ✔ | ✔ | |||||
马哈拉诺比斯距离 | ✔ | ✔ | ✔ | ||||
AE | ✔ | ✔ | ✔ | ||||
VAE | ✔ | ✔ | ✔ | ||||
AEGMM | ✔ | ✔ | |||||
VAEGMM | ✔ | ✔ | |||||
似然比 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
预言家 | ✔ | ||||||
光谱残差 | ✔ | ✔ | ✔ | ||||
序列到序列 | ✔ | ✔ |
探测器 | 表格 | 图像 | 时间序列 | 文本 | 分类特征 | 在线的 | 功能级别 |
---|---|---|---|---|---|---|---|
对抗性AE | ✔ | ✔ | |||||
模型蒸馏 | ✔ | ✔ | ✔ | ✔ | ✔ |
探测器 | 表格 | 图像 | 时间序列 | 文本 | 分类特征 | 在线的 | 功能级别 |
---|---|---|---|---|---|---|---|
柯尔莫哥洛夫-斯米尔诺夫 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
克拉梅尔·冯·米塞斯 | ✔ | ✔ | ✔ | ✔ | |||
费舍尔精确检验 | ✔ | ✔ | ✔ | ✔ | |||
最大平均差异 (MMD) | ✔ | ✔ | ✔ | ✔ | ✔ | ||
学习内核MMD | ✔ | ✔ | ✔ | ✔ | |||
情境感知 MMD | ✔ | ✔ | ✔ | ✔ | ✔ | ||
最小二乘密度差 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
卡方 | ✔ | ✔ | ✔ | ||||
混合类型表格数据 | ✔ | ✔ | ✔ | ||||
分类器 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
找出差异 | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |
分类器不确定性 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
回归不确定性 | ✔ | ✔ | ✔ | ✔ | ✔ |
漂移检测器支持 TensorFlow、PyTorch 和(如果适用)KeOps后端。但是,Alibi Detect 默认情况下不会安装这些。有关更多详细信息,请参阅安装选项。
from alibi_detect.cd import MMDDriftcd = MMDDrift(x_ref, backend='tensorflow', p_val=.05) preds = cd.predict(x)
PyTorch 中的相同检测器:
cd = MMDDrift(x_ref, backend='pytorch', p_val=.05) preds = cd.predict(x)
或者在 KeOps 中:
cd = MMDDrift(x_ref, backend='keops', p_val=.05) preds = cd.predict(x)
Alibi Detect 还附带各种预处理步骤,例如随机初始化编码器、预训练文本嵌入以检测使用Transformer库的漂移以及从机器学习模型中提取隐藏层。这允许检测不同类型的漂移,例如 协变量和预测的分布漂移。 TensorFlow 和 PyTorch 再次支持预处理步骤。
from alibi_detect.cd.tensorflow import HiddenOutput, preprocess_driftmodel = # 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、电影评论)以了解更多详细信息。
-
- 示例:网络入侵
-
- 示例:网络入侵
-
- 示例:CIFAR10
-
自动编码高斯混合模型(AEGMM)(Zong et al., 2018)
- 示例:网络入侵
-
- 示例:网络入侵
-
Prophet 时间序列离群值检测器(Taylor 等人,2018)
- 示例:天气预报
-
光谱残差时间序列离群值检测器(Ren et al., 2019)
- 示例:综合数据集
该包还包含alibi_detect.datasets
轻松获取不同模式的多个数据集的功能。对于每个数据集,返回数据和标签或包含数据、标签和可选元数据的Bunch对象。例子:
from alibi_detect.datasets import fetch_ecg(X_train, y_train), (X_test, y_test) = fetch_ecg(return_X_y=True)
(X_train, y_train), (X_test, y_test) = fetch_ecg(return_X_y=True)" tabindex="0" role="button">
-
基因组数据集:
fetch_genome
- 用于分布外检测的细菌基因组学数据集,作为分布外检测的似然比的一部分发布。来自原始的TL;DR:数据集包含来自 10 个分布内细菌类别的 250 个碱基对的基因组序列用于训练,60 个 OOD 细菌类别用于验证,以及另外 60 个不同的 OOD 细菌类别用于测试。训练、验证和测试集中分别有 1、7 和 700 万个序列。有关数据集的详细信息,请查看README。
from alibi_detect.datasets import fetch_genome
(X_train, y_train), (X_val, y_val), (X_test, y_test) = fetch_genome(return_X_y=True)
-
心电图 5000:
fetch_ecg
- 5000 个心电图,最初从Physionet获得。
-
国民银行:
fetch_nab
- Numenta Anomaly Benchmark中 DataFrame 中的任何单变量时间序列。可以使用 检索包含可用时间序列的列表
alibi_detect.datasets.get_list_nab()
。
- Numenta Anomaly Benchmark中 DataFrame 中的任何单变量时间序列。可以使用 检索包含可用时间序列的列表
-
CIFAR-10-C:
fetch_cifar10c
- CIFAR-10-C(Hendrycks & Dietterich,2019)包含 CIFAR-10 的测试集,但受到不同严重程度的各种类型的噪声、模糊、亮度等的破坏和干扰,导致分类逐渐下降模型的性能在 CIFAR-10 上训练。
fetch_cifar10c
允许您选择任何严重性级别或损坏类型。可以使用 检索包含可用损坏类型的列表alibi_detect.datasets.corruption_types_cifar10c()
。该数据集可用于鲁棒性和漂移的研究。原始数据可以在这里找到。例子:
from alibi_detect.datasets import fetch_cifar10c
corruption = ['gaussian_noise', 'motion_blur', 'brightness', 'pixelate'] X, y = fetch_cifar10c(corruption=corruption, severity=5, return_X_y=True)
- CIFAR-10-C(Hendrycks & Dietterich,2019)包含 CIFAR-10 的测试集,但受到不同严重程度的各种类型的噪声、模糊、亮度等的破坏和干扰,导致分类逐渐下降模型的性能在 CIFAR-10 上训练。
-
对抗性 CIFAR-10:
fetch_attack
- 在 CIFAR-10 上训练的 ResNet-56 分类器上加载对抗实例。可用的攻击:Carlini-Wagner ('cw') 和SLIDE ('slide')。例子:
from alibi_detect.datasets import fetch_attack
(X_train, y_train), (X_test, y_test) = fetch_attack('cifar10', 'resnet56', 'cw', return_X_y=True)
- KDD 杯 '99 :
fetch_kdd
- 具有不同类型计算机网络入侵的数据集。
fetch_kdd
允许您选择网络入侵的子集作为目标或仅选择指定的功能。原始数据可以在这里找到。
- 具有不同类型计算机网络入侵的数据集。
可以在异常值、对抗性或漂移检测之外找到有用的模型和/或构建块alibi_detect.models
。主要实现:
-
PixelCNN++:
alibi_detect.models.pixelcnn.PixelCNN
-
变分自动编码器:
alibi_detect.models.autoencoder.VAE
-
序列到序列模型:
alibi_detect.models.autoencoder.Seq2Seq
-
残差网络:
alibi_detect.models.resnet
- CIFAR-10 上预训练的 ResNet-20/32/44 模型可以在我们的Google Cloud Bucket上找到,并且可以按如下方式获取:
from alibi_detect.utils.fetching import fetch_tf_model
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.11.5},
date = {2024-01-22},
year = {2019}
}