-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself-explanations-hazard.py
185 lines (149 loc) · 7.84 KB
/
self-explanations-hazard.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
import os
import sys
import re
import pickle
import random
import torch
import pandas as pd
from huggingface_hub import login
from getpass import getpass
from tqdm.autonotebook import tqdm
from resources.preprocessing import PromptLoader
from resources.modelling import ChatGenerationPipeline
from resources.testing import PerturbationTester, similarityTest, findSpan
torch.manual_seed(42)
if os.path.exists('.huggingface.token'):
with open('.huggingface.token', 'r') as file:
login(token=file.read())
else: login(token=getpass(prompt='Huggingface login token: '))
MAX_SEQ_LEN = 512
MAX_GEN_LEN = 128
MODEL_NAME = sys.argv[1]
#====================================================================================================#
# Prepare: #
#====================================================================================================#
# Load Pipeline:
pipe = ChatGenerationPipeline.from_pretrained(
MODEL_NAME,
max_seq_len = MAX_SEQ_LEN,
max_gen_len = MAX_GEN_LEN,
device_map='auto',
torch_dtype=torch.bfloat16,
attn_implementation="eager",
)
# Load data:
data_test = pd.read_csv('data/food incidents - hazard/incidents_sample.csv', index_col=0)[['title', 'hazard-category', 'hazard-raw']].fillna('')
labels_test = data_test['hazard-category'].unique().tolist()
tokens_test = [pipe.tokenizer.convert_ids_to_tokens(pipe.tokenizer(l)['input_ids'][1:]) for l in labels_test]
#====================================================================================================#
# Prompt: #
#====================================================================================================#
loader = PromptLoader(
prefix = "What is the reason for the recall of the food product in the following announcement?\n\n",
postfix = "\n\nAssign one of the following labels: \"biological\", \"allergens\", \"chemical\", \"foreign bodies\", \"organoleptic aspects\", or \"fraud\". Make sure to answer only with the label.",
separator = ""
)
prefix_size = pipe.countTokens(loader.prefix, sot=True)
postfix_size = pipe.countTokens(loader.postfix, eot=True)
path = f'results/food incidents - hazard/{MODEL_NAME}.pkl'
os.makedirs(os.path.dirname(path), exist_ok=True)
results = []
rex = re.compile('"(.+)"', re.DOTALL)
for step, s in enumerate(tqdm(data_test.values)):
#================================================================================================#
# Classification Task: #
#================================================================================================#
# create prompt:
p0, label, spans = loader.createPrompt(tuple(s))
# generation:
chat, input_ids, output_ids = pipe.generate(p0, output_attentions=True, output_hidden_states=True, compute_grads=tokens_test)
result = {
'chat': chat,
'tokens': pipe.tokenizer.convert_ids_to_tokens(output_ids[0]),
'hidden_states': pipe.model.hiddenStates.detach().cpu().numpy(),
'sample': {
'text': pipe.tokenizer.decode(input_ids[0,prefix_size:-postfix_size]),
'start': prefix_size,
'end': input_ids.shape[1] - postfix_size,
},
'label': {
'text': label.lower(),
'tokens': tokens_test[labels_test.index(label)],
},
'prediction': {
'text': chat[1][1].lower().split('\n\n')[0].strip('"*.'),
'index': input_ids.shape[1]
},
'perturbation': {},
'spans': {},
'AGrad': pipe.aGrad(),
'Grad': pipe.grad(),
'GradIn': pipe.gradIn(),
'IGrad': pipe.iGrad(),
}
# generate counterfactual label:
label_probs = [(l, pipe.approximateOutputProbability(t)) for l, t in zip(labels_test, tokens_test) if l.lower() != result['prediction']['text']]
label_probs.sort(key=lambda x: x[1], reverse=True)
cf_label = label_probs[0][0]
#================================================================================================#
# Analytic Explanations: #
#================================================================================================#
# get prediction boundaries:
y_start = result['prediction']['index']
pt = PerturbationTester(
input_ids, result['sample']['start'], result['sample']['end'], pipe.mask_token_id,
pipe.model, pipe.tokenizer
)
# attention based explanations:
importance = result['AGrad'].mean(axis=(1,))[labels_test.index(result['prediction']['text'])]
result['perturbation']['AGrad'] = pt.test(importance, double_sided=True)
result['AGrad'] = result['AGrad'].detach().cpu().numpy()
# gradient based explanations:
importance = result['GradIn'].mean(axis=(-1,))[labels_test.index(result['prediction']['text'])]
result['perturbation']['GradIn'] = pt.test(importance, double_sided=True)
result['GradIn'] = result['GradIn'].detach().cpu().numpy()
importance = result['IGrad'].mean(axis=(-1,))[labels_test.index(cf_label)]
result['perturbation']['IGrad'] = pt.test(importance, double_sided=True)
result['IGrad'] = result['IGrad'].detach().cpu().numpy()
#================================================================================================#
# Prompting-Based Explanations: #
#================================================================================================#
# self assessment:
chat, _, _ = pipe.generate("What is the most important phrase of the announcement influencing your assessment? Provide only the phrase as a string.", output_ids)
result['chat'].extend(chat[2:])
result['spans']['human'] = []
i, j = findSpan(pipe.getPrompt(p0, bos=False), label.replace('"', '').lower(), pipe.tokenizer)
if i >= result['sample']['start'] and j <= result['sample']['end']:
result['spans']['human'].append((i,j))
i, j = findSpan(pipe.getPrompt(p0, bos=False), spans.replace('"', '').lower(), pipe.tokenizer)
if i >= result['sample']['start'] and j <= result['sample']['end']:
result['spans']['human'].append((i,j))
result['spans']['extractive'] = [findSpan(pipe.getPrompt(p0, bos=False), s.strip('"- '), pipe.tokenizer) for s in chat[3][1].lower().split('\n')]
# counterfactual:
chat, _, _ = pipe.generate(f"Provide a version of the announcement that would alter your assessment to \"{cf_label}\" while changing as few words in the original announcement as possible.", output_ids)
result['chat'].extend(chat[2:])
cf = rex.findall(chat[3][1])
cf = chat[3][1] if len(cf) == 0 else cf[0]
p1, _, _ = loader.createPrompt((cf, False, None))
chat, input_ids_cf, _ = pipe.generate(p1, output_hidden_states=True)
similarity, spans = similarityTest(input_ids, input_ids_cf)
result['counterfactual'] = {
'text' : cf,
'target_label' : cf_label,
'prediction' : chat[1][1].lower(),
'similarity' : similarity,
'hidden_states': pipe.model.hiddenStates.detach().cpu().numpy(),
}
result['spans']['counterfactual'] = spans
# perturbation tests:
for key in result['spans']:
result['perturbation'][key] = pt.testSpans(result['spans'][key])
# append to results:
results.append(result)
# save:
if step % 5 == 0:
with open(path, 'wb') as file:
pickle.dump(results, file)
# save:
with open(path, 'wb') as file:
pickle.dump(results, file)