meta-pytorch/captum

Model interpretability and understanding for PyTorch

What is Captum?

Captumは、** open-source library **であり、PyTorchにモデル解釈性ツールを追加します。Integrated Gradients, DeepLift, Gradient Shap, SmoothGrad, TCAV, TracIn などの帰属手法(attribution algorithms)を実装しており、「どの入力特徴量、ニューロン、または学習データがこの予測に寄与したか?」を問うことができます。このライブラリは、torchvision, torchtext, およびその他のドメイン特化型拡張機能で構築されたモデルを含む、あらゆる PyTorch モデルと直接動作します。


Who is it for?

  • Model developers:予測を駆動する要因を見ることによって、ネットワークをデバッグまたは改善したい開発者。
  • Researchers:新しい解釈性手法を構築しており、ベンチマークスイートが必要な研究者。
  • Production engineers:エンドユーザーに対して説明(例:なぜその推奨事項がなされたか)を生成する必要があるエンジニア。

Quick installation

# from PyPI (most common)
pip install captum

# or via conda
conda install -c pytorch captum   # or conda-forge channel

最新の開発版:

git clone https://github.com/pytorch/captum.git
cd captum
pip install -e .

-e .[dev] または -e .[tutorials] を使用して、追加の dev または tutorial の依存関係を追加できます。)


Minimal example (the README’s toy model)

import torch, torch.nn as nn
from captum.attr import IntegratedGradients, GradientShap, DeepLift, NoiseTunnel

class ToyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin1 = nn.Linear(3, 3)
        self.relu = nn.ReLU()
        self.lin2 = nn.Linear(3, 2)
        # deterministic weights for the demo
        self.lin1.weight = nn.Parameter(torch.arange(-4., 5.).view(3,3))
n        self.lin1.bias   = nn.Parameter(torch.zeros(1,3))
        self.lin2.weight = nn.Parameter(torch.arange(-3., 3.).view(2,3))
        self.lin2.bias   = nn.Parameter(torch.ones(1,2))
    def forward(self, x):
        return self.lin2(self.relu(self.lin1(x)))

model = ToyModel().eval()
input   = torch.rand(2, 3)
baseline = torch.zeros(2, 3)

# Integrated Gradients
ig = IntegratedGradients(model)
attr, delta = ig.attribute(input, baseline, target=0, return_convergence_delta=True)
print('IG attributions:', attr)
print('Delta:', delta)

# GradientShap (uses a baseline distribution)
gs = GradientShap(model)
baseline_dist = torch.randn(10, 3) * 0.001
attr, delta = gs.attribute(input, stdevs=0.09, n_samples=4,
                           baselines=baseline_dist, target=0,
                           return_convergence_delta=True)
print('GradShap attributions:', attr)

# Smoothing with NoiseTunnel (SmoothGrad)
nt = NoiseTunnel(IntegratedGradients(model))
attr, delta = nt.attribute(input, nt_type='smoothgrad', stdevs=0.02,
                           nt_samples=4, baselines=baseline, target=0,
                           return_convergence_delta=True)
print('SmoothGrad IG:', attr)

このコードは、特徴量ごとの帰属スコア(正の値 → 予測を支持、負の値 → 予測に反対)と、積分近似の正確さを測定する convergence delta


Main capabilities (as listed in the README)

  • Attribution methods: Integrated Gradients, DeepLift, Gradient Shap, SmoothGrad/VarGrad, TCAV, TracIn, etc.
  • Neuron- and layer-level analysis: NeuronConductance, LayerConductance により、どの内部ユニットが重要かを把握できます。
  • Counterfactual & adversarial utilities: 説明や堅牢性テストのための最小限の入力摂動。
  • Compatibility: torchvision, torchtext, およびカスタムアーキテクチャを含む、あらゆる PyTorch モデルと動作します。
  • Bench-marking: 研究者は、組み込みのスイートを使用して新しいアルゴリズムを比較できます。

Where to learn more

  • Official docs: https://captum.ai/
  • Tutorials (install with pip install -e .[tutorials])
  • FAQ: docs/faq.md
  • Talks & papers linked in the README (NeurIPS 2019, KDD 2020, etc.)

Bottom line: Captumは、研究、デバッグ、またはプロダクションレベルのユーザー向け説明が必要な場合、PyTorchモデルを explain するための最適なライブラリです。

関連

  • プロジェクト
  • プロジェクト
  • プロジェクト
  • プロジェクト
  • プロジェクト