Martijn van Beers
commited on
Commit
•
38e692a
1
Parent(s):
a411e9c
Add slider to select layer
Browse filesFor Integrated Gradients, this selects exactly that layer, but for
hila's method, this is the starting point. it ignores all lower layers,
and uses the selected layers and all the layers above.
app.py
CHANGED
@@ -76,14 +76,12 @@ def generate_relevance(model, input_ids, attention_mask, index=None, start_layer
|
|
76 |
.to(torch.float)
|
77 |
.requires_grad_(True)
|
78 |
).to(device)
|
79 |
-
print("ONE_HOT", one_hot.size(), one_hot)
|
80 |
one_hot = torch.sum(one_hot * output)
|
81 |
model.zero_grad()
|
82 |
# create the gradients for the class we're interested in
|
83 |
one_hot.backward(retain_graph=True)
|
84 |
|
85 |
num_tokens = model.roberta.encoder.layer[0].attention.self.get_attn().shape[-1]
|
86 |
-
print(input_ids.size(-1), num_tokens)
|
87 |
R = torch.eye(num_tokens).expand(output.size(0), -1, -1).clone().to(device)
|
88 |
|
89 |
for i, blk in enumerate(model.roberta.encoder.layer):
|
@@ -158,7 +156,6 @@ def show_explanation(model, input_ids, attention_mask, index=None, start_layer=8
|
|
158 |
output, expl = generate_relevance(
|
159 |
model, input_ids, attention_mask, index=index, start_layer=start_layer
|
160 |
)
|
161 |
-
#print(output.shape, expl.shape)
|
162 |
# normalize scores
|
163 |
scaler = PyTMinMaxScalerVectorized()
|
164 |
|
@@ -180,7 +177,6 @@ def show_explanation(model, input_ids, attention_mask, index=None, start_layer=8
|
|
180 |
1 : 0 - ((attention_mask[record] == 0).sum().item() + 1)
|
181 |
]
|
182 |
# vis_data_records.append(list(zip(tokens, nrm.tolist())))
|
183 |
-
#print([(tokens[i], nrm[i].item()) for i in range(len(tokens))])
|
184 |
vis_data_records.append(
|
185 |
visualization.VisualizationDataRecord(
|
186 |
nrm,
|
@@ -194,13 +190,10 @@ def show_explanation(model, input_ids, attention_mask, index=None, start_layer=8
|
|
194 |
)
|
195 |
)
|
196 |
return visualize_text(vis_data_records)
|
197 |
-
# return vis_data_records
|
198 |
|
199 |
def custom_forward(inputs, attention_mask=None, pos=0):
|
200 |
-
# print("inputs", inputs.shape)
|
201 |
result = model2(inputs, attention_mask=attention_mask, return_dict=True)
|
202 |
preds = result.logits
|
203 |
-
# print("preds", preds.shape)
|
204 |
return preds
|
205 |
|
206 |
def summarize_attributions(attributions):
|
@@ -228,8 +221,6 @@ def run_attribution_model(input_ids, attention_mask, ref_token_id=tokenizer.unk_
|
|
228 |
finally:
|
229 |
pass
|
230 |
vis_data_records = []
|
231 |
-
print("IN", input_ids.size())
|
232 |
-
print("ATTR", attributions.shape)
|
233 |
for record in range(input_ids.size(0)):
|
234 |
classification = output[record].argmax(dim=-1).item()
|
235 |
class_name = classifications[classification]
|
@@ -237,7 +228,6 @@ def run_attribution_model(input_ids, attention_mask, ref_token_id=tokenizer.unk_
|
|
237 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[record].flatten())[
|
238 |
1 : 0 - ((attention_mask[record] == 0).sum().item() + 1)
|
239 |
]
|
240 |
-
print("TOK", len(tokens), attr.shape)
|
241 |
vis_data_records.append(
|
242 |
visualization.VisualizationDataRecord(
|
243 |
attr,
|
@@ -252,16 +242,21 @@ def run_attribution_model(input_ids, attention_mask, ref_token_id=tokenizer.unk_
|
|
252 |
)
|
253 |
return visualize_text(vis_data_records)
|
254 |
|
255 |
-
def sentence_sentiment(input_text):
|
256 |
text_batch = [input_text]
|
257 |
encoding = tokenizer(text_batch, return_tensors="pt")
|
258 |
input_ids = encoding["input_ids"].to(device)
|
259 |
attention_mask = encoding["attention_mask"].to(device)
|
260 |
-
layer =
|
|
|
|
|
|
|
|
|
|
|
261 |
output = run_attribution_model(input_ids, attention_mask, layer=layer)
|
262 |
return output
|
263 |
|
264 |
-
def sentiment_explanation_hila(input_text):
|
265 |
text_batch = [input_text]
|
266 |
encoding = tokenizer(text_batch, return_tensors="pt")
|
267 |
input_ids = encoding["input_ids"].to(device)
|
@@ -270,16 +265,17 @@ def sentiment_explanation_hila(input_text):
|
|
270 |
# true class is positive - 1
|
271 |
true_class = 1
|
272 |
|
273 |
-
return show_explanation(model, input_ids, attention_mask)
|
274 |
|
|
|
275 |
hila = gradio.Interface(
|
276 |
fn=sentiment_explanation_hila,
|
277 |
-
inputs="text",
|
278 |
outputs="html",
|
279 |
)
|
280 |
lig = gradio.Interface(
|
281 |
fn=sentence_sentiment,
|
282 |
-
inputs="text",
|
283 |
outputs="html",
|
284 |
)
|
285 |
|
|
|
76 |
.to(torch.float)
|
77 |
.requires_grad_(True)
|
78 |
).to(device)
|
|
|
79 |
one_hot = torch.sum(one_hot * output)
|
80 |
model.zero_grad()
|
81 |
# create the gradients for the class we're interested in
|
82 |
one_hot.backward(retain_graph=True)
|
83 |
|
84 |
num_tokens = model.roberta.encoder.layer[0].attention.self.get_attn().shape[-1]
|
|
|
85 |
R = torch.eye(num_tokens).expand(output.size(0), -1, -1).clone().to(device)
|
86 |
|
87 |
for i, blk in enumerate(model.roberta.encoder.layer):
|
|
|
156 |
output, expl = generate_relevance(
|
157 |
model, input_ids, attention_mask, index=index, start_layer=start_layer
|
158 |
)
|
|
|
159 |
# normalize scores
|
160 |
scaler = PyTMinMaxScalerVectorized()
|
161 |
|
|
|
177 |
1 : 0 - ((attention_mask[record] == 0).sum().item() + 1)
|
178 |
]
|
179 |
# vis_data_records.append(list(zip(tokens, nrm.tolist())))
|
|
|
180 |
vis_data_records.append(
|
181 |
visualization.VisualizationDataRecord(
|
182 |
nrm,
|
|
|
190 |
)
|
191 |
)
|
192 |
return visualize_text(vis_data_records)
|
|
|
193 |
|
194 |
def custom_forward(inputs, attention_mask=None, pos=0):
|
|
|
195 |
result = model2(inputs, attention_mask=attention_mask, return_dict=True)
|
196 |
preds = result.logits
|
|
|
197 |
return preds
|
198 |
|
199 |
def summarize_attributions(attributions):
|
|
|
221 |
finally:
|
222 |
pass
|
223 |
vis_data_records = []
|
|
|
|
|
224 |
for record in range(input_ids.size(0)):
|
225 |
classification = output[record].argmax(dim=-1).item()
|
226 |
class_name = classifications[classification]
|
|
|
228 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[record].flatten())[
|
229 |
1 : 0 - ((attention_mask[record] == 0).sum().item() + 1)
|
230 |
]
|
|
|
231 |
vis_data_records.append(
|
232 |
visualization.VisualizationDataRecord(
|
233 |
attr,
|
|
|
242 |
)
|
243 |
return visualize_text(vis_data_records)
|
244 |
|
245 |
+
def sentence_sentiment(input_text, layer):
|
246 |
text_batch = [input_text]
|
247 |
encoding = tokenizer(text_batch, return_tensors="pt")
|
248 |
input_ids = encoding["input_ids"].to(device)
|
249 |
attention_mask = encoding["attention_mask"].to(device)
|
250 |
+
layer = int(layer)
|
251 |
+
if layer == 0:
|
252 |
+
layer = model2.roberta.embeddings
|
253 |
+
else:
|
254 |
+
layer = getattr(model2.roberta.encoder.layer, str(layer-1))
|
255 |
+
|
256 |
output = run_attribution_model(input_ids, attention_mask, layer=layer)
|
257 |
return output
|
258 |
|
259 |
+
def sentiment_explanation_hila(input_text, layer):
|
260 |
text_batch = [input_text]
|
261 |
encoding = tokenizer(text_batch, return_tensors="pt")
|
262 |
input_ids = encoding["input_ids"].to(device)
|
|
|
265 |
# true class is positive - 1
|
266 |
true_class = 1
|
267 |
|
268 |
+
return show_explanation(model, input_ids, attention_mask, start_layer=int(layer))
|
269 |
|
270 |
+
layer_slider = gradio.Slider(minimum=0, maximum=12, value=8, step=1, label="Select layer")
|
271 |
hila = gradio.Interface(
|
272 |
fn=sentiment_explanation_hila,
|
273 |
+
inputs=["text", layer_slider],
|
274 |
outputs="html",
|
275 |
)
|
276 |
lig = gradio.Interface(
|
277 |
fn=sentence_sentiment,
|
278 |
+
inputs=["text", layer_slider],
|
279 |
outputs="html",
|
280 |
)
|
281 |
|