-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_processing.py
159 lines (138 loc) · 7.65 KB
/
data_processing.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
import os
import util
from tqdm import tqdm
def prepare_eval_data(dataset_dict, tokenizer):
tokenized_examples = tokenizer(dataset_dict['question'],
dataset_dict['context'],
truncation="only_second",
stride=128,
max_length=384,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding='max_length')
# Since one example might give us several features if it has a long context, we need a map from a feature to
# its corresponding example. This key gives us just that.
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
# For evaluation, we will need to convert our predictions to substrings of the context, so we keep the
# corresponding example_id and we will store the offset mappings.
tokenized_examples["id"] = []
for i in tqdm(range(len(tokenized_examples["input_ids"]))):
# Grab the sequence corresponding to that example (to know what is the context and what is the question).
sequence_ids = tokenized_examples.sequence_ids(i)
# One example can give several spans, this is the index of the example containing this span of text.
sample_index = sample_mapping[i]
tokenized_examples["id"].append(dataset_dict["id"][sample_index])
# Set to None the offset_mapping that are not part of the context so it's easy to determine if a token
# position is part of the context or not.
tokenized_examples["offset_mapping"][i] = [
(o if sequence_ids[k] == 1 else None)
for k, o in enumerate(tokenized_examples["offset_mapping"][i])
]
return tokenized_examples
def prepare_train_data(dataset_dict, tokenizer, domain_id):
tokenized_examples = tokenizer(dataset_dict['question'],
dataset_dict['context'],
truncation="only_second",
stride=128,
max_length=384,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding='max_length')
sample_mapping = tokenized_examples["overflow_to_sample_mapping"]
offset_mapping = tokenized_examples["offset_mapping"]
# Let's label those examples!
tokenized_examples["start_positions"] = []
tokenized_examples["end_positions"] = []
tokenized_examples['id'] = []
tokenized_examples['domain_id'] = []
inaccurate = 0
for i, offsets in enumerate(tqdm(offset_mapping)):
# We will label impossible answers with the index of the CLS token.
input_ids = tokenized_examples["input_ids"][i]
cls_index = input_ids.index(tokenizer.cls_token_id)
# Grab the sequence corresponding to that example (to know what is the context and what is the question).
sequence_ids = tokenized_examples.sequence_ids(i)
# One example can give several spans, this is the index of the example containing this span of text.
sample_index = sample_mapping[i]
answer = dataset_dict['answer'][sample_index]
# Start/end character index of the answer in the text.
start_char = answer['answer_start'][0]
end_char = start_char + len(answer['text'][0])
tokenized_examples['id'].append(dataset_dict['id'][sample_index])
# also append domain that this example is from
tokenized_examples['domain_id'].append(domain_id)
# Start token index of the current span in the text.
token_start_index = 0
while sequence_ids[token_start_index] != 1:
token_start_index += 1
# End token index of the current span in the text.
token_end_index = len(input_ids) - 1
while sequence_ids[token_end_index] != 1:
token_end_index -= 1
# Detect if the answer is out of the span (in which case this feature is labeled with the CLS index).
if not (offsets[token_start_index][0] <= start_char and offsets[token_end_index][1] >= end_char):
tokenized_examples["start_positions"].append(cls_index)
tokenized_examples["end_positions"].append(cls_index)
else:
# Otherwise move the token_start_index and token_end_index to the two ends of the answer.
# Note: we could go after the last offset if the answer is the last word (edge case).
while token_start_index < len(offsets) and offsets[token_start_index][0] <= start_char:
token_start_index += 1
tokenized_examples["start_positions"].append(token_start_index - 1)
while offsets[token_end_index][1] >= end_char:
token_end_index -= 1
tokenized_examples["end_positions"].append(token_end_index + 1)
# assertion to check if this checks out
context = dataset_dict['context'][sample_index]
offset_st = offsets[tokenized_examples['start_positions'][-1]][0]
offset_en = offsets[tokenized_examples['end_positions'][-1]][1]
if context[offset_st : offset_en] != answer['text'][0]:
inaccurate += 1
total = len(tokenized_examples['id'])
print(f"Preprocessing not completely accurate for {inaccurate}/{total} instances")
return tokenized_examples
def read_and_process(args, tokenizer, dataset_dict, dir_name, dataset_name, split, domain_id):
#TODO: cache this if possible
cache_path = f'{dir_name}/{dataset_name}_{domain_id}_encodings.pt'
if os.path.exists(cache_path) and not args.recompute_features:
print(f"Loading cache from {cache_path}")
tokenized_examples = util.load_pickle(cache_path)
print(f"Cache loaded successfully.")
else:
print(f'No cache found for {cache_path}')
if split=='train':
tokenized_examples = prepare_train_data(dataset_dict, tokenizer, domain_id)
else:
tokenized_examples = prepare_eval_data(dataset_dict, tokenizer)
util.save_pickle(tokenized_examples, cache_path)
return tokenized_examples
def create_cache(args, datasets, data_dir, tokenizer, split_name, domain_id):
datasets = datasets.split(',')
dataset_dict = None
dataset_name=''
for dataset in datasets:
dataset_name += f'_{dataset}'
dataset_dict_curr = util.read_squad(f'{data_dir}/{dataset}')
dataset_dict = util.merge(dataset_dict, dataset_dict_curr)
cache_path = f'{data_dir}/{dataset_name}_{domain_id}_encodings.pt'
# if args.recompute_features or not os.path.exists(cache_path):
# turning off caching
if True:
print(f"Creating cache at {cache_path}")
if split_name=='train':
tokenized_examples = prepare_train_data(dataset_dict, tokenizer, domain_id)
else:
tokenized_examples = prepare_eval_data(dataset_dict, tokenizer)
util.save_pickle(tokenized_examples, cache_path)
print("Cache created successfully")
return
def get_dataset(args, datasets, data_dir, tokenizer, split_name, domain_id=-1):
datasets = datasets.split(',')
dataset_dict = None
dataset_name=''
for dataset in datasets:
dataset_name += f'_{dataset}'
dataset_dict_curr = util.read_squad(f'{data_dir}/{dataset}')
dataset_dict = util.merge(dataset_dict, dataset_dict_curr)
data_encodings = read_and_process(args, tokenizer, dataset_dict, data_dir, dataset_name, split_name, domain_id)
return util.QADataset(data_encodings, train=(split_name=='train')), dataset_dict