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 等),讓你可以詢問 "which input features, neurons, or training examples contributed to this prediction?"。該函式庫可直接與任何 PyTorch 模型配合使用,包括使用 torchvision、torchtext 及其他領域特定擴充功能所建構的模型。
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] 可增加額外的開發或教學範例依賴項。)
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))
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: 與任何 PyTorch 模型相容,包括 torchvision, torchtext, 及自定義架構。
- 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 是任何需要 explain PyTorch 模型的人的必備函式庫,無論是為了研究、除錯或生產環境中面向用戶的解釋。
相關
- 專案
- 專案
- 專案
- 專案
- 專案