-
Notifications
You must be signed in to change notification settings - Fork 76
/
data.py
326 lines (273 loc) · 11.1 KB
/
data.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Dataset loader and data utilities.
Author:
Shrey Desai and Yasumasa Onoe
"""
import collections
import itertools
import torch
from torch.utils.data import Dataset
from random import shuffle
from utils import cuda, load_dataset
PAD_TOKEN = '[PAD]'
UNK_TOKEN = '[UNK]'
class Vocabulary:
"""
This class creates two dictionaries mapping:
1) words --> indices,
2) indices --> words.
Args:
samples: A list of training examples stored in `QADataset.samples`.
vocab_size: Int. The number of top words to be used.
Attributes:
words: A list of top words (string) sorted by frequency. `PAD_TOKEN`
(at position 0) and `UNK_TOKEN` (at position 1) are prepended.
All words will be lowercased.
encoding: A dictionary mapping words (string) to indices (int).
decoding: A dictionary mapping indices (int) to words (string).
"""
def __init__(self, samples, vocab_size):
self.words = self._initialize(samples, vocab_size)
self.encoding = {word: index for (index, word) in enumerate(self.words)}
self.decoding = {index: word for (index, word) in enumerate(self.words)}
def _initialize(self, samples, vocab_size):
"""
Counts and sorts all tokens in the data, then it returns a vocab
list. `PAD_TOKEN and `UNK_TOKEN` are added at the beginning of the
list. All words are lowercased.
Args:
samples: A list of training examples stored in `QADataset.samples`.
vocab_size: Int. The number of top words to be used.
Returns:
A list of top words (string) sorted by frequency. `PAD_TOKEN`
(at position 0) and `UNK_TOKEN` (at position 1) are prepended.
"""
vocab = collections.defaultdict(int)
for (_, passage, question, _, _) in samples:
for token in itertools.chain(passage, question):
vocab[token.lower()] += 1
top_words = [
word for (word, _) in
sorted(vocab.items(), key=lambda x: x[1], reverse=True)
][:vocab_size]
words = [PAD_TOKEN, UNK_TOKEN] + top_words
return words
def __len__(self):
return len(self.words)
class Tokenizer:
"""
This class provides two methods converting:
1) List of words --> List of indices,
2) List of indices --> List of words.
Args:
vocabulary: An instantiated `Vocabulary` object.
Attributes:
vocabulary: A list of top words (string) sorted by frequency.
`PAD_TOKEN` (at position 0) and `UNK_TOKEN` (at position 1) are
prepended.
pad_token_id: Index of `PAD_TOKEN` (int).
unk_token_id: Index of `UNK_TOKEN` (int).
"""
def __init__(self, vocabulary):
self.vocabulary = vocabulary
self.pad_token_id = self.vocabulary.encoding[PAD_TOKEN]
self.unk_token_id = self.vocabulary.encoding[UNK_TOKEN]
def convert_tokens_to_ids(self, tokens):
"""
Converts words to corresponding indices.
Args:
tokens: A list of words (string).
Returns:
A list of indices (int).
"""
return [
self.vocabulary.encoding.get(token, self.unk_token_id)
for token in tokens
]
def convert_ids_to_tokens(self, token_ids):
"""
Converts indices to corresponding words.
Args:
token_ids: A list of indices (int).
Returns:
A list of words (string).
"""
return [
self.vocabulary.decoding.get(token_id, UNK_TOKEN)
for token_id in token_ids
]
class QADataset(Dataset):
"""
This class creates a data generator.
Args:
args: `argparse` object.
path: Path to a data file (.gz), e.g. "datasets/squad_dev.jsonl.gz".
Attributes:
args: `argparse` object.
meta: Dataset metadata (e.g. dataset name, split).
elems: A list of raw examples (jsonl).
samples: A list of preprocessed examples (tuple). Passages and
questions are shortened to max sequence length.
tokenizer: `Tokenizer` object.
batch_size: Int. The number of example in a mini batch.
"""
def __init__(self, args, path):
self.args = args
self.meta, self.elems = load_dataset(path)
self.samples = self._create_samples()
self.tokenizer = None
self.batch_size = args.batch_size if 'batch_size' in args else 1
self.pad_token_id = self.tokenizer.pad_token_id \
if self.tokenizer is not None else 0
def _create_samples(self):
"""
Formats raw examples to desired form. Any passages/questions longer
than max sequence length will be truncated.
Returns:
A list of words (string).
"""
samples = []
for elem in self.elems:
# Unpack the context paragraph. Shorten to max sequence length.
passage = [
token.lower() for (token, offset) in elem['context_tokens']
][:self.args.max_context_length]
# Each passage has several questions associated with it.
# Additionally, each question has multiple possible answer spans.
for qa in elem['qas']:
qid = qa['qid']
question = [
token.lower() for (token, offset) in qa['question_tokens']
][:self.args.max_question_length]
# Select the first answer span, which is formatted as
# (start_position, end_position), where the end_position
# is inclusive.
answers = qa['detected_answers']
answer_start, answer_end = answers[0]['token_spans'][0]
samples.append(
(qid, passage, question, answer_start, answer_end)
)
return samples
def _create_data_generator(self, shuffle_examples=False):
"""
Converts preprocessed text data to Torch tensors and returns a
generator.
Args:
shuffle_examples: If `True`, shuffle examples. Default: `False`
Returns:
A generator that iterates through all examples one by one.
(Tuple of tensors)
"""
if self.tokenizer is None:
raise RuntimeError('error: no tokenizer registered')
example_idxs = list(range(len(self.samples)))
if shuffle_examples:
shuffle(example_idxs)
passages = []
questions = []
start_positions = []
end_positions = []
for idx in example_idxs:
# Unpack QA sample and tokenize passage/question.
qid, passage, question, answer_start, answer_end = self.samples[idx]
# Convert words to tensor.
passage_ids = torch.tensor(
self.tokenizer.convert_tokens_to_ids(passage)
)
question_ids = torch.tensor(
self.tokenizer.convert_tokens_to_ids(question)
)
answer_start_ids = torch.tensor(answer_start)
answer_end_ids = torch.tensor(answer_end)
# Store each part in an independent list.
passages.append(passage_ids)
questions.append(question_ids)
start_positions.append(answer_start_ids)
end_positions.append(answer_end_ids)
return zip(passages, questions, start_positions, end_positions)
def _create_batches(self, generator, batch_size):
"""
This is a generator that gives one batch at a time. Tensors are
converted to "cuda" if necessary.
Args:
generator: A data generator created by `_create_data_generator`.
batch_size: Int. The number of example in a mini batch.
Yields:
A dictionary of tensors containing a single batch.
"""
current_batch = [None] * batch_size
no_more_data = False
# Loop through all examples.
while True:
bsz = batch_size
# Get examples from generator
for i in range(batch_size):
try:
current_batch[i] = list(next(generator))
except StopIteration: # Run out examples
no_more_data = True
bsz = i # The size of the last batch.
break
# Stop if there's no leftover examples
if no_more_data and bsz == 0:
break
passages = []
questions = []
start_positions = torch.zeros(bsz)
end_positions = torch.zeros(bsz)
max_passage_length = 0
max_question_length = 0
# Check max lengths for both passages and questions
for ii in range(bsz):
passages.append(current_batch[ii][0])
questions.append(current_batch[ii][1])
start_positions[ii] = current_batch[ii][2]
end_positions[ii] = current_batch[ii][3]
max_passage_length = max(
max_passage_length, len(current_batch[ii][0])
)
max_question_length = max(
max_question_length, len(current_batch[ii][1])
)
# Assume pad token index is 0. Need to change here if pad token
# index is other than 0.
padded_passages = torch.zeros(bsz, max_passage_length)
padded_questions = torch.zeros(bsz, max_question_length)
# Pad passages and questions
for iii, passage_question in enumerate(zip(passages, questions)):
passage, question = passage_question
padded_passages[iii][:len(passage)] = passage
padded_questions[iii][:len(question)] = question
# Create an input dictionary
batch_dict = {
'passages': cuda(self.args, padded_passages).long(),
'questions': cuda(self.args, padded_questions).long(),
'start_positions': cuda(self.args, start_positions).long(),
'end_positions': cuda(self.args, end_positions).long()
}
if no_more_data:
if bsz > 0:
# This is the last batch (smaller than `batch_size`)
yield batch_dict
break
yield batch_dict
def get_batch(self, shuffle_examples=False):
"""
Returns a data generator that supports mini-batch.
Args:
shuffle_examples: If `True`, shuffle examples. Default: `False`
Returns:
A data generator that iterates though all batches.
"""
return self._create_batches(
self._create_data_generator(shuffle_examples=shuffle_examples),
self.batch_size
)
def register_tokenizer(self, tokenizer):
"""
Stores `Tokenizer` object as an instance variable.
Args:
tokenizer: If `True`, shuffle examples. Default: `False`
"""
self.tokenizer = tokenizer
def __len__(self):
return len(self.samples)