-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself-explanations-movies.py
177 lines (144 loc) · 7.35 KB
/
self-explanations-movies.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
import os
import sys
import re
import pickle
import random
import torch
from huggingface_hub import login
from getpass import getpass
from tqdm.autonotebook import tqdm
from resources.preprocessing import PromptLoaderIMDB
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:
with open('data/movies/val_sample.jsonl', 'r') as file:
data_test = file.read().split('\n')
labels_test = ['negative', 'positive']
tokens_test = [pipe.tokenizer.convert_ids_to_tokens(pipe.tokenizer(l)['input_ids'][1:]) for l in labels_test]
#====================================================================================================#
# Prompt: #
#====================================================================================================#
loader = PromptLoaderIMDB(
prefix = "What is the sentiment of the following review?\n\n",
postfix = "\n\nAssign one of the following labels: \"negative\" or \"positive\". 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/movies/{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[:-1])):
#================================================================================================#
# Classification Task: #
#================================================================================================#
# create prompt:
p0, label, spans = loader.createPrompt(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.lower())],
},
'prediction': {
'text': chat[1][1][:8].lower(),
'index': input_ids.shape[1]
},
'perturbation': {},
'spans': {},
'AGrad': pipe.aGrad(),
'Grad': pipe.grad(),
'GradIn': pipe.gradIn(),
'IGrad': pipe.iGrad(),
}
#================================================================================================#
# Analytic Explanations: #
#================================================================================================#
# get prediction boundaries:
y_start = result['prediction']['index']
y = int(result['prediction']['text'] == 'positive')
# generate counterfactual label:
cf_label = labels_test[1-y]
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:
if len(spans) <= 1: p1 = "What is the most important phrase of the review influencing your assessment? Provide only the phrase as a string."
else: p1 = f"What are the {len(spans):d} most important phrases of the review influencing your assessment? Provide a list of strings with one phrase per line."
chat, _, _ = pipe.generate(p1, output_ids)
result['chat'].extend(chat[2:])
result['spans']['human'] = [findSpan(pipe.getPrompt(p0, bos=False), loader.decode(s['text']).replace('"', '').lower(), pipe.tokenizer) for s in spans]
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]
p2, _, _ = loader.createPrompt((cf, False, None))
chat, input_ids_cf, _ = pipe.generate(p2, output_hidden_states=True)
similarity, spans = similarityTest(input_ids, input_ids_cf)
result['counterfactual'] = {
'text' : cf,
'target_label' : cf_label,
'prediction' : chat[1][1][:8].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)