-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_utils.py
173 lines (150 loc) · 7.43 KB
/
data_utils.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
import os
import json
import re
import string
import numpy as np
from tqdm import tqdm
import torch
from torch.utils.data import Dataset, DataLoader, RandomSampler, SequentialSampler
class QAData:
def __init__(self, logger, args, data_path, is_training):
self.data_path = data_path
with open(self.data_path, "r") as f:
self.data = json.load(f)
self.index2id = {i:d["id"] for i, d in enumerate(self.data)}
self.id2index = {d["id"]:i for i, d in enumerate(self.data)}
self.is_training = is_training
self.load = not args.debug
self.logger = logger
self.args = args
if "test" in self.data_path:
self.data_type = "test"
elif "dev" in self.data_path:
self.data_type = "dev"
elif "train" in self.data_path:
self.data_type = "train"
else:
raise NotImplementedError()
self.metric = "EM"
self.max_input_length = 512
self.tokenizer = None
self.dataset = None
self.dataloader = None
self.cache = None
def __len__(self):
return len(self.data)
def decode(self, tokens):
return self.tokenizer.decode(tokens, skip_special_tokens=True, clean_up_tokenization_spaces=True)
def decode_batch(self, tokens):
return self.tokenizer.batch_decode(tokens, skip_special_tokens=True, clean_up_tokenization_spaces=True)
def flatten(self, answers):
new_answers, metadata = [], []
for answer in answers:
metadata.append((len(new_answers), len(new_answers)+len(answer)))
new_answers += answer
return new_answers, metadata
def load_dataset(self, tokenizer):
self.tokenizer = tokenizer
postfix = tokenizer.__class__.__name__.replace("zer", "zed")
preprocessed_path = os.path.join(
"/".join(self.data_path.split("/")[:-1]),
self.data_path.split("/")[-1].replace(".json", "-{}.json".format(postfix)))
if self.load and os.path.exists(preprocessed_path):
self.logger.info("Loading pre-tokenized data from {}".format(preprocessed_path))
with open(preprocessed_path, "r") as f:
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, \
metadata = json.load(f)
else:
print ("Start tokenizing...")
questions = [d["question"] if d["question"].endswith("?") else d["question"]+"?" for d in self.data]
answers = [d["answer"] for d in self.data]
answers, metadata = self.flatten(answers)
question_input = tokenizer.batch_encode_plus(questions,
pad_to_max_length=True,
max_length=self.args.max_input_length)
answer_input = tokenizer.batch_encode_plus(answers,
pad_to_max_length=True)
input_ids, attention_mask = question_input["input_ids"], question_input["attention_mask"]
decoder_input_ids, decoder_attention_mask = answer_input["input_ids"], answer_input["attention_mask"]
if self.load:
preprocessed_data = [input_ids, attention_mask,
decoder_input_ids, decoder_attention_mask,
metadata]
with open(preprocessed_path, "w") as f:
json.dump(preprocessed_data, f)
self.dataset = QADataset(input_ids, attention_mask,
decoder_input_ids, decoder_attention_mask,
in_metadata=None, out_metadata=metadata,
is_training=self.is_training)
self.logger.info("Loaded {} examples from {} data".format(len(self.dataset), self.data_type))
return self.dataset
def load_dataloader(self, do_return=False):
self.dataloader = MyDataLoader(self.args, self.dataset, self.is_training)
if do_return:
return self.dataloader
def evaluate(self, predictions):
assert len(predictions)==len(self), (len(predictions), len(self))
ems = []
for (prediction, dp) in zip(predictions, self.data):
ems.append(get_exact_match(prediction, dp["answer"]))
return ems
def save_predictions(self, predictions):
assert len(predictions)==len(self), (len(predictions), len(self))
prediction_dict = {dp["id"]:prediction for dp, prediction in zip(self.data, predictions)}
save_path = os.path.join(self.args.output_dir, "{}predictions.json".format(self.args.prefix))
with open(save_path, "w") as f:
json.dump(prediction_dict, f)
self.logger.info("Saved prediction in {}".format(save_path))
def get_exact_match(prediction, groundtruth):
if type(groundtruth)==list:
if len(groundtruth)==0:
return 0
return np.max([get_exact_match(prediction, gt) for gt in groundtruth])
return (normalize_answer(prediction) == normalize_answer(groundtruth))
def normalize_answer(s):
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(remove_articles(remove_punc(lower(s))))
class QADataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids, decoder_attention_mask,
in_metadata=None, out_metadata=None,
is_training=False):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) \
if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) \
if out_metadata is None else out_metadata
self.is_training = is_training
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]
def __len__(self):
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx]
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx]
class MyDataLoader(DataLoader):
def __init__(self, args, dataset, is_training):
if is_training:
sampler=RandomSampler(dataset)
batch_size = args.train_batch_size
else:
sampler=SequentialSampler(dataset)
batch_size = args.predict_batch_size
super(MyDataLoader, self).__init__(dataset, sampler=sampler, batch_size=batch_size)