forked from google-research/bleurt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheli52.py
282 lines (225 loc) · 11.6 KB
/
eli52.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
print("importing")
from datasets import load_dataset
from datasets import load_metric
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import TrainingArguments, DefaultFlowCallback, PrinterCallback
from transformers import Trainer
import torch
from torch import nn
import numpy as np
import pickle
from sklearn.preprocessing import StandardScaler
import random
import json
sep_token = "[SEP]" # FORDOR maybe many special tokens
pretrained_model_name = "roberta-base" # 'bert-base-cased'
random.seed(42)
class my_Bert(nn.Module):
def __init__(self, bert):
super().__init__()
self.bert = bert
def forward(self,input_ids,attention_mask=None,labels=None,**kwargs):
res = self.bert.forward(input_ids,attention_mask,labels=labels,**kwargs)
print(f"FORDOR-input_ids {input_ids}")
print(f"FORDOR-inputss {tokenizer.decode(input_ids[0])}")
print(f"FORDOR-inputss {tokenizer.decode(input_ids[1])}")
print(f"FORDOR-labels {labels}")
print(f"FORDOR-res {res}")
return res
print("starting load")
# for i in range(len(dataset["train_eli5"])):
# print(f'train= {dataset["train_eli5"][i]["answers"]}')
# print(f'valid= {dataset["validation_eli5"][i]["answers"]}')
# print(f'test= {dataset["test_eli5"][i]["answers"]}')
class ELI5MetricDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
def tokenize_function(examples):
return tokenizer(examples["text"], padding="max_length", truncation=True)
def changeArr(input1):
# Copy input array into newArray
newArray = input1.copy()
# Sort newArray[] in ascending order
newArray.sort()
# Dictionary to store the rank of
# the array element
ranks = {}
rank = 1
for index in range(len(newArray)):
element = newArray[index];
# Update rank of element
if element not in ranks:
ranks[element] = rank
rank += 1
# Assign ranks to elements
for index in range(len(input1)):
element = input1[index]
input1[index] = float(ranks[input1[index]])
my_dataset = {}
if False:# try:
with open("my_dataset.pickle", "rb" ) as f:
my_dataset = pickle.load(f)
else: # except IOError:
print("could not load my_dataset - preprocessing")
raw_datasets = load_dataset("eli5")
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name)
def preprocess_data(split_name):
inputs = []
labels = []
cnt = 0
single_count = 0
candidates = []
references = []
scores = []
lengths = []
bads = []
test_mode = "test" in split_name.lower()
for example in raw_datasets[split_name]:
question = example["title"]+ " " + example["selftext"] #FORDOR add special sep token?
num_answers = len (example["answers"]["a_id"])
if num_answers == 1:
lengths.append(1)
single_count += 1
if random.randint(0, 1) == 0:
candidate = f'question: {question} answer: {question}'
reference = f'question: {question} answer: {example["answers"]["text"][0]}'
bads.append((candidate, reference, float(0)))
else:
answer_ind = random.randrange(len( raw_datasets[split_name]))
other_example = raw_datasets[split_name][answer_ind]
other_question = other_example["title"]+ other_example["selftext"]
if question != other_question:
bad_ind = random.randrange(len (other_example["answers"]["a_id"]))
candidate = f'question: {question} answer: {other_example["answers"]["text"][bad_ind]}'
reference = f'question: {question} answer: {example["answers"]["text"][0]}'
bads.append((candidate, reference, float(-2)))
continue
# print(f"FORDOR question with one answer")
for i in range (num_answers):
answer = example["answers"]["text"][i]
# question = question.replace('"','\\"')
# answer = answer.replace('"','\\"')
candidate = f'question: {question} answer: {answer}'
if not test_mode:
ref_ind = random.randrange(num_answers)
ref_ind = ((i + 1) % num_answers) if ref_ind == i else ref_ind
reference = f'question: {question} answer: {example["answers"]["text"][ref_ind]}'
score = float(example["answers"]["score"][i])
candidates.append(candidate)
references.append(reference)
scores.append(score)
else:
lengths.append(num_answers - 1)
for j in range(num_answers):
if j != i:
reference = f'question: {question} answer: {example["answers"]["text"][j]}'
score = float(example["answers"]["score"][i])
candidates.append(candidate)
references.append(reference)
scores.append(score)
# inputs.append(question + sep_token + answer)
# print (f'FORDOR float - {float(example["answers"]["score"][i])} {example["answers"]["score"][i]}')
# labels.append(float(example["answers"]["score"][i]))
cnt = cnt+1
# if cnt > 200000:
# break
# tokenized_datasets = raw_datasets.map(tokenize_function, batched=True)
#shuffle data
# c = list(zip(inputs, labels))
# random.shuffle(c)
# inputs, labels = zip(*c)
# inputs = list(inputs)
# labels = list(labels)
# encodings = tokenizer(inputs, padding="max_length", truncation=True)
# encodings2 = tokenizer(inputs, padding="max_length", truncation=False)
# for i in range(len(encodings)):
# if len(encodings[i]) != len( encodings2[i]):
# print (print(f"encoding and length {encodings[i]}, {len(encodings[i])} no truncation = {encodings2[i]}, {len(encodings2[i])}"))
#
# tensor_labels = torch.as_tensor(labels).reshape(-1,1)
# scaler = StandardScaler()
# scaler.fit(tensor_labels)
# scaled_labels = scaler.transform(tensor_labels).astype(np.float32)
# changeArr(scores)
for thruple in bads:
candidates.append(thruple[0])
references.append(thruple[1])
scores.append(thruple[2])
if not test_mode:
c = list(zip(candidates, references, scores))
random.shuffle(c)
candidates, references, scores = zip(*c)
candidates = list(candidates)
references = list(references)
scores = list(scores)
print (f"{split_name} singles: {single_count} / {len(scores)} = {single_count / len(scores)}")
# my_dataset[split_name] = ELI5MetricDataset(encodings, scaled_labels)
# print (f"FORDOR lens {len(encodings)}=={len(labels)}")
# assert len(encodings) == len(labels)
assert len(references) == len(candidates)
assert len(references) == len(scores)
return references, candidates, scores, lengths
def main():
references, candidates, scores, lengths = preprocess_data("train_eli5")
val_references, val_candidates, val_scores, val_lengths = preprocess_data("validation_eli5")
combined_scores = scores + val_scores
changeArr(combined_scores)
scores = combined_scores[:len(scores)]
val_scores = combined_scores[len(scores):]
with open(f'{"train_eli5"}.json', 'a') as the_file:
for i in range(len(references)):
reference = references[i]
candidate = candidates[i]
score = scores[i]
the_file.write(f'{{"candidate": {json.dumps(candidate)}, "reference": {json.dumps(reference)}, "score": {score} }}\n')
references, candidates, scores, lengths = val_references, val_candidates, val_scores, val_lengths
with open(f'{"validation_eli5"}.json', 'a') as the_file:
for i in range(len(references)):
reference = references[i]
candidate = candidates[i]
score = scores[i]
the_file.write(f'{{"candidate": {json.dumps(candidate)}, "reference": {json.dumps(reference)}, "score": {score} }}\n')
references, candidates, scores, lengths = preprocess_data("test_eli5")
with open("sentence_pairs.jsonl", 'a') as the_file:
for i in range(len(references)):
reference = references[i]
candidate = candidates[i]
the_file.write(f'{{"candidate": {json.dumps(candidate)}, "reference": {json.dumps(reference)}}}\n')
# pickle.dump( my_dataset, open( "my_dataset.pickle", "wb" ) )
# metric = load_metric("spearmanr")
# def compute_metrics(eval_pred):
# logits, labels = eval_pred
# print(f'logits- {max(logits)}, {min(logits)}')
# print(f'labels- {max(labels)}, {min(labels)}')
# return metric.compute(predictions=logits, references=labels)
# model = AutoModelForSequenceClassification.from_pretrained(pretrained_model_name, num_labels=1)
# # freezing bert parameters leaving only regression layer
# # for param in model.bert.parameters():
# # param.requires_grad = False
# # model = my_Bert(model)
# # print (f"FORDOR model = {str(model)}")
# # print (f'FORDOR debug {raw_datasets["train_eli5"][0]["answers"]} =:= {model(input_ids=my_dataset["train_eli5"][0]["input_ids"].unsqueeze(0), attention_mask=my_dataset["train_eli5"][0]["attention_mask"].unsqueeze(0), token_type_ids=my_dataset["train_eli5"][0]["token_type_ids"].unsqueeze(0))}')
# training_args = TrainingArguments("test_trainer", evaluation_strategy="steps", eval_steps=10000, save_steps=10000, per_device_train_batch_size=8, per_device_eval_batch_size=8)
# trainer = Trainer(model=model, args=training_args, train_dataset=my_dataset["train_eli5"], eval_dataset=my_dataset["validation_eli5"], compute_metrics=compute_metrics,
# callbacks = [
# DefaultFlowCallback(),
# PrinterCallback()
# ],
# )
# #, max_steps=3000
# trainer.train()
# # model.eval()
# # print (f'FORDOR2 debug {raw_datasets["train_eli5"][0]["answers"]} =:= {model(input_ids=my_dataset["train_eli5"][0]["input_ids"].unsqueeze(0).cuda(), attention_mask=my_dataset["train_eli5"][0]["attention_mask"].unsqueeze(0).cuda(), token_type_ids=my_dataset["train_eli5"][0]["token_type_ids"].unsqueeze(0).cuda())}')
# # print (f'FORDOR3 debug {raw_datasets["train_eli5"][0]["answers"]} =:= {model(input_ids=my_dataset["train_eli5"][1]["input_ids"].unsqueeze(0).cuda(), attention_mask=my_dataset["train_eli5"][1]["attention_mask"].unsqueeze(0).cuda(), token_type_ids=my_dataset["train_eli5"][1]["token_type_ids"].unsqueeze(0).cuda())}')
# # print (f'FORDOR4 debug {raw_datasets["train_eli5"][1]["answers"]} =:= {model(input_ids=my_dataset["train_eli5"][4]["input_ids"].unsqueeze(0).cuda(), attention_mask=my_dataset["train_eli5"][4]["attention_mask"].unsqueeze(0).cuda(), token_type_ids=my_dataset["train_eli5"][4]["token_type_ids"].unsqueeze(0).cuda())}')
# print ("evaluation starting")
# print (trainer.evaluate())
if __name__ == "__main__":
main()