Alibi Detect は、外れ値、敵対的、ドリフト検出に特化したソース利用可能な Python ライブラリです。このパッケージは、表形式データ、テキスト、画像、時系列向けのオンラインおよびオフライン検出器の両方をカバーすることを目的としています。ドリフト検出では、TensorFlow と PyTorch の両バックエンドがサポートされています。
本番環境での外れ値と分布のモニタリングの重要性に関する背景については、論文 Monitoring and explainability of models in production に基づき、Alibi Detect に言及している Challenges in Deploying and Monitoring Machine Learning Systems ICML 2020 ワークショップのこの講演をご覧ください。
ドリフト検出の徹底的な入門については、Protecting Your Machine Learning Against Drift: An Introduction をご覧ください。この講演では、ドリフトとは何か、なぜそれを検出することが有益なのか、ドリフトのさまざまな種類、原則に基づいて検出する方法、そしてドリフト検出器の構造について説明しています。
パッケージ 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 outlier detector を使用して 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 vs. 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), [Amazonレビュー](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), [Amazonレビュー](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
Sequence-to-sequence モデル: 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} }