[6ac965]: / src / iterpretability / explain.py

Download this file

172 lines (139 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from typing import Dict, List, Optional
import matplotlib.pyplot as plt
import numpy as np
import torch
from captum._utils.models.linear_model import SkLearnLinearRegression
from captum.attr import (
DeepLift,
FeatureAblation,
FeaturePermutation,
IntegratedGradients,
KernelShap,
Lime,
ShapleyValueSampling,
GradientShap,
)
from captum.attr._core.lime import get_exp_kernel_similarity_function
from torch import nn
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Explainer:
"""
Explainer instance, consisting of several explainability methods.
"""
def __init__(
self,
model: nn.Module,
feature_names: List,
explainer_list: List = [
"feature_ablation",
"feature_permutation",
"integrated_gradients",
"deeplift",
"shapley_value_sampling",
"lime",
],
n_steps: int = 500,
perturbations_per_eval: int = 10,
n_samples: int = 1000,
kernel_width: float = 1.0,
baseline: Optional[torch.Tensor] = None,
) -> None:
self.baseline = baseline
self.explainer_list = explainer_list
self.feature_names = feature_names
# Feature ablation
feature_ablation_model = FeatureAblation(model)
def feature_ablation_cbk(X_test: torch.Tensor) -> torch.Tensor:
out = feature_ablation_model.attribute(
X_test, n_steps=n_steps, perturbations_per_eval=perturbations_per_eval
)
return out
# Integrated gradients
integrated_gradients_model = IntegratedGradients(model)
def integrated_gradients_cbk(X_test: torch.Tensor) -> torch.Tensor:
return integrated_gradients_model.attribute(X_test, n_steps=n_steps)
# DeepLift
deeplift_model = DeepLift(model)
def deeplift_cbk(X_test: torch.Tensor) -> torch.Tensor:
return deeplift_model.attribute(X_test)
# Feature permutation
feature_permutation_model = FeaturePermutation(model)
def feature_permutation_cbk(X_test: torch.Tensor) -> torch.Tensor:
return feature_permutation_model.attribute(
X_test, n_steps=n_steps, perturbations_per_eval=perturbations_per_eval
)
# LIME
exp_eucl_distance = get_exp_kernel_similarity_function(
kernel_width=kernel_width
)
lime_model = Lime(
model,
interpretable_model=SkLearnLinearRegression(),
similarity_func=exp_eucl_distance,
)
def lime_cbk(X_test: torch.Tensor) -> torch.Tensor:
return lime_model.attribute(
X_test,
n_samples=n_samples,
perturbations_per_eval=perturbations_per_eval,
)
# Shapley value sampling
shapley_value_sampling_model = ShapleyValueSampling(model)
def shapley_value_sampling_cbk(X_test: torch.Tensor) -> torch.Tensor:
return shapley_value_sampling_model.attribute(
X_test,
n_samples=n_samples,
perturbations_per_eval=perturbations_per_eval,
)
# Kernel SHAP
kernel_shap_model = KernelShap(model)
def kernel_shap_cbk(X_test: torch.Tensor) -> torch.Tensor:
return kernel_shap_model.attribute(
X_test,
n_samples=n_samples,
perturbations_per_eval=perturbations_per_eval,
)
# Gradient SHAP
gradient_shap_model = GradientShap(model)
def gradient_shap_cbk(X_test: torch.Tensor) -> torch.Tensor:
return gradient_shap_model.attribute(X_test, baselines=self.baseline)
self.explainers = {
"feature_ablation": feature_ablation_cbk,
"integrated_gradients": integrated_gradients_cbk,
"deeplift": deeplift_cbk,
"feature_permutation": feature_permutation_cbk,
"lime": lime_cbk,
"shapley_value_sampling": shapley_value_sampling_cbk,
"kernel_shap": kernel_shap_cbk,
"gradient_shap": gradient_shap_cbk,
}
def _check_tensor(self, X: torch.Tensor) -> torch.Tensor:
if isinstance(X, torch.Tensor):
return X.to(DEVICE)
else:
return torch.from_numpy(np.asarray(X)).float().to(DEVICE)
def explain(self, X: torch.Tensor) -> Dict:
output = {}
if self.baseline is None:
self.baseline = torch.zeros(
X.shape
) # Zero tensor as baseline if no baseline specified
for name in self.explainer_list:
X_test = self._check_tensor(X)
self.baseline = self._check_tensor(self.baseline)
X_test.requires_grad_()
explainer = self.explainers[name]
output[name] = explainer(X_test).detach().cpu().numpy()
return output
def plot(self, X: torch.Tensor) -> None:
explanations = self.explain(X)
fig, axs = plt.subplots(int((len(explanations) + 1) / 2), 2)
idx = 0
for name in explanations:
x_pos = np.arange(len(self.feature_names))
ax = axs[int(idx / 2), idx % 2]
ax.bar(x_pos, np.mean(np.abs(explanations[name]), axis=0), align="center")
ax.set_xlabel("Features")
ax.set_title(f"{name}")
idx += 1
plt.tight_layout()