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?"*라는 질문에 답할 수 있게 해줍니다. 이 라이브러리는 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]를 사용하여 추가적인 개발 또는 튜토리얼 종속성을 추가할 수 있습니다.)
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: torchvision, torchtext 및 커스텀 아키텍처를 포함한 모든 PyTorch 모델과 작동합니다.
- Bench-marking: 연구자들은 내장된 수트를 사용하여 새로운 알고리즘을 비교할 수 있습니다.
Where to learn more
Official docs: https://captum.ai/
Tutorials (install with
pip install -e .[tutorials])FAQ:
docs/faq.mdTalks & papers linked in the README (NeurIPS 2019, KDD 2020, etc.)
Bottom line: Captum은 연구, 디버깅 또는 프로덕션급 사용자 대상 설명이 필요한 경우 PyTorch 모델을 explain하기 위한 필수 라이리브러리입니다.
관련
- 프로젝트
- 프로젝트
- 프로젝트
- 프로젝트
- 프로젝트