-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.py
318 lines (253 loc) · 11 KB
/
interpret.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import numpy as np
import torch
import torch.nn as nn
from model import JWAttentionClassifier, MLP
from distillbert import DistilBertForSequenceClassification
from captum.attr import visualization
from captum.attr import (
GradientShap,
Lime,
DeepLift,
DeepLiftShap,
IntegratedGradients,
LayerIntegratedGradients,
TokenReferenceBase,
LayerConductance,
NeuronConductance,
NoiseTunnel,
)
from captum._utils.models.linear_model import SkLearnRidge
class Interpreter:
# >> Adapted from court-of-xai codebase
def __init__(self, name, model, mask_features_by_token=False, attribute_args={}):
self.attribute_args = attribute_args
self.mask_features_by_token = mask_features_by_token
self.predictor = model
self.name = name
def interpret(self, instance, lengths, labels, **kwargs):
# Determine and set additional kwargs for the attribute method
# print("[I]",instance.shape)
captum_inputs = self.predictor.instances_to_captum_inputs(
instance, lengths, labels=labels
)
# print("[I]",captum_inputs[0].shape)
# 1. Prepare arguments
args = dict(
**kwargs, # Call-based extra arguments
**self.attribute_kwargs(
captum_inputs,
mask_features_by_token=self.mask_features_by_token,
), # General extra arguments
**self.attribute_args # To be added in subclass constructor
)
# print("[I]", args['inputs'].shape)
# 2. Attribute
# print("[I]", args['inputs'])
attributions = self.attribute(**args)
# print(attributions.shape)
# 3. Average/sum over embedding dimensions to obtain scores per-token
if self.mask_features_by_token:
attributions = attributions.mean(dim=-1).abs()
else:
attributions = attributions.sum(dim=-1).abs()
return attributions
def attribute_kwargs(self, captum_inputs, mask_features_by_token=False):
"""
Args:
captum_inputs (Tuple): result of model.instances_to_captum_inputs.
mask_features_by_token (bool, optional): For Captum methods that require a feature mask,
define each token as a feature if True. If False,
define each scalar in the embedding dimension as a
feature (e.g., default behavior in LIME).
Defaults to False.
Returns:
Dict: key-word arguments to be given to the attribute method of the
relevant Captum Attribution sub-class.
"""
inputs, target, additional = captum_inputs
vocab = self.predictor.vocab
# Manually check for distilbert.
if isinstance(self.predictor, JWAttentionClassifier) or isinstance(
self.predictor, MLP
):
embedding = self.predictor.embedding
else: # DistillBert?
embedding = (
self.predictor.embeddings
) # Need to assure the embedding is always fetchable
# Will only work on single-sentence input data
pad_idx = vocab.get_padding_index()
pad_idxs = torch.full(
inputs.shape[:2], fill_value=pad_idx, device=inputs.device
)
baselines = embedding(pad_idxs)
# print(baselines.shape, inputs.shape)
attr_kwargs = {
"inputs": inputs,
"target": target,
"baselines": baselines,
"additional_forward_args": None, # additional # set to additional
}
if isinstance(self.predictor, DistilBertForSequenceClassification):
attr_kwargs["additional_forward_args"] = additional
# For methods that require a feature mask, define each token as one feature
if mask_features_by_token:
# see: https://captum.ai/api/lime.html for the definition of a feature mask
feature_mask_tuple = tuple()
# We only have single-input datasets for now
# for i in range(len(inputs)):
# input_tensor = inputs[i]
bs, seq_len, emb_dim = inputs.shape
feature_mask = torch.tensor(list(range(bs * seq_len))).reshape(
[bs, seq_len, 1]
)
feature_mask = feature_mask.to(inputs.device)
feature_mask = feature_mask.expand(-1, -1, emb_dim)
# feature_mask_tuple += (feature_mask,) # (bs, seq_len, emb_dim)
attr_kwargs["feature_mask"] = feature_mask
return attr_kwargs
# DeepLift
class DeepLiftInterpreter(Interpreter, DeepLift):
def __init__(self, model):
Interpreter.__init__(self, "DeepLift", model)
self.submodel = model.captum_sub_model()
DeepLift.__init__(self, self.submodel)
# DeepLiftShap
# Errors out on <baselines> (tensor repeated?) TBD debug
class DeepLiftShapInterpreter(Interpreter, DeepLiftShap):
def __init__(self, model):
Interpreter.__init__(self, "DeepLiftShap", model)
self.submodel = model.captum_sub_model()
DeepLift.__init__(self, self.submodel)
# GradientShap
class GradientShapInterpreter(Interpreter, GradientShap):
def __init__(self, model):
Interpreter.__init__(self, "GradShap", model)
self.submodel = model.captum_sub_model()
GradientShap.__init__(self, self.submodel)
# IntegratedGradients
class IntegratedGradientsInterpreter(Interpreter, IntegratedGradients):
def __init__(self, model):
Interpreter.__init__(self, "intgrad", model)
self.submodel = model.captum_sub_model()
IntegratedGradients.__init__(self, self.submodel)
# Lime
class LIMEInterpreter(Interpreter, Lime):
def __init__(self, model, mask_features_by_token=True, attribute_args={}):
Interpreter.__init__(
self, "lime", model, mask_features_by_token, attribute_args
)
self.lin_model = SkLearnRidge()
self.submodel = model.captum_sub_model()
Lime.__init__(self, self.submodel, self.lin_model)
INTERPRETERS = {
"deeplift": DeepLiftInterpreter,
"grad-shap": GradientShapInterpreter,
"deeplift-shap": DeepLiftShapInterpreter,
"int-grad": IntegratedGradientsInterpreter,
"lime": LIMEInterpreter,
}
def get_interpreter(key):
return INTERPRETERS[key]
##########
# Legacy #
##########
def visualize_attributions(visualization_records):
cast_records = []
for record in visualization_records:
# Each record is assumed to be a tuple
print(record)
cast_records.append(visualization.VisualizationDataRecord(*record))
visualization.visualize_text(cast_records)
def interpret_instance_lime(model, numericalized_instance):
device = next(iter(model.parameters())).device
linear_model = SkLearnRidge()
lime = Lime(model)
numericalized_instance = numericalized_instance.unsqueeze(0) # Add fake batch dim
# Feature mask enumerates (word) features in each instance
bsz, seq_len = 1, len(numericalized_instance)
feature_mask = torch.tensor(list(range(bsz * seq_len))).reshape([bsz, seq_len, 1])
feature_mask = feature_mask.to(device)
feature_mask = feature_mask.expand(-1, -1, model.embedding_dim)
attributions = lime.attribute(
numericalized_instance, target=1, n_samples=1000, feature_mask=feature_mask
) # n samples arg taken from court of xai
print(attributions.shape)
print("Lime Attributions:", attributions)
return attributions
def interpret_instance_deeplift(model, numericalized_instance):
_model = model.captum_sub_model()
dl = DeepLift(_model)
# Should be done in model.prepare_inputs...
numericalized_instance = numericalized_instance.unsqueeze(0) # Add fake batch dim
lengths = torch.tensor(len(numericalized_instance)).unsqueeze(0)
logits, return_dict = model(numericalized_instance, lengths)
pred = logits.squeeze() # obtain prediction
scaled_pred = nn.Sigmoid()(pred).item() # scale to probability
# Reference indices are just a bunch of padding indices
# token_reference = TokenReferenceBase(reference_token_idx=0) # Padding index is the reference
# reference_indices = token_reference.generate_reference(len(numericalized_instance),
# device=next(iter(model.parameters())).device).unsqueeze(0)
with torch.no_grad():
embedded_instance = model.embedding(numericalized_instance)
# Pass embeddings to input
outs, delta = dl.attribute(embedded_instance, return_convergence_delta=True)
print(outs)
return outs, scaled_pred, delta
def interpret_instance_lig(model, numericalized_instance):
_model = model.captum_sub_model()
lig = LayerIntegratedGradients(_model, model.embedding) # LIG uses embedding data
numericalized_instance = numericalized_instance.unsqueeze(0) # Add fake batch dim
lengths = torch.tensor(len(numericalized_instance)).unsqueeze(0)
logits, return_dict = model(numericalized_instance, lengths)
pred = logits.squeeze() # obtain prediction
# print(pred)
scaled_pred = nn.Sigmoid()(pred).item() # scale to probability
# Reference indices are just a bunch of padding indices
token_reference = TokenReferenceBase(
reference_token_idx=0
) # Padding index is the reference
reference_indices = token_reference.generate_reference(
len(numericalized_instance), device=next(iter(model.parameters())).device
).unsqueeze(0)
with torch.no_grad():
embedded_instance = model.embedding(numericalized_instance)
attributions, delta = lig.attribute(
embedded_instance,
reference_indices,
n_steps=500,
return_convergence_delta=True,
)
print("IG Attributions:", attributions)
print("Convergence Delta:", delta)
return attributions, scaled_pred, delta
def legacy_interpret(model, meta):
sample_sentence = "this is a very nice movie".split()
sample_instance = torch.tensor(meta.vocab.numericalize(sample_sentence))
sample_instance = sample_instance.to(device)
# Try out various interpretability methods
# attributions = interpret_instance_lime(model, sample_instance)
# Layer integrated gradients
# attributions, prediction, delta = interpret_instance_lig(model, sample_instance)
# Deeplift
attributions, prediction, delta = interpret_instance_deeplift(
model, sample_instance
)
print(attributions.shape) # B, T, E
attributions = attributions.sum(dim=2).squeeze(0)
attributions = attributions / torch.norm(attributions)
attributions = attributions.cpu().detach().numpy()
visualize_attributions(
[
(
attributions,
prediction,
str(round(prediction)),
str(round(prediction)),
"Pos",
attributions.sum(),
sample_sentence,
delta,
)
]
)