forked from gauthierdmn/question_answering
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.py
263 lines (211 loc) · 9.11 KB
/
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
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
# external libraries
import os
import re
import pickle
import string
import numpy as np
from collections import Counter
from spacy.lang.en import English
import torch
from torch.utils.data.sampler import SubsetRandomSampler
import torch.nn.functional as F
# internal utilities
import config
tokenizer = English()
device = torch.device("cuda" if config.cuda else "cpu")
def clean_text(text):
text = text.replace("]", " ] ")
text = text.replace("[", " [ ")
text = text.replace("\n", " ")
text = text.replace("''", '" ').replace("``", '" ')
return text
def word_tokenize(sent):
return [token.text for token in tokenizer(sent)]
def convert_idx(text, tokens):
current = 0
spans = []
for token in tokens:
current = text.find(token, current)
if current < 0:
print("Token {} cannot be found".format(token))
raise Exception()
spans.append((current, current + len(token)))
current += len(token)
return spans
def build_vocab(context_filename, question_filename, word_vocab_filename, word2idx_filename,
char_vocab_filename, char2idx_filename, is_train=True, max_words=-1):
# select the directory we want to create the vocabulary from
directory = config.train_dir if is_train else config.dev_dir
# load the context and question files
with open(os.path.join(directory, context_filename), "r", encoding="utf-8") as context,\
open(os.path.join(directory, question_filename), "r", encoding="utf-8") as question:
context_file = context.readlines()
question_file = question.readlines()
# clean and tokenize the texts
words = [w.strip("\n") for doc in context_file + question_file for w in word_tokenize(clean_text(doc))]
chars = [c for w in words for c in list(w)]
# create a dictionary with word and char frequencies
word_vocab = Counter(words)
char_vocab = Counter(chars)
# put them in a list ordered by frequency
word_vocab = ["--NULL--"] + ["--UNK--"] + sorted(word_vocab, key=word_vocab.get, reverse=True)
char_vocab = ["--NULL--"] + ["--UNK--"] + sorted(char_vocab, key=char_vocab.get, reverse=True)
# limit the word vocabulary to top max_words
word_vocab = word_vocab[:max_words]
# get the word and char to ID dictionary mapping
word2idx = dict([(x, y) for (y, x) in enumerate(word_vocab)])
char2idx = dict([(x, y) for (y, x) in enumerate(char_vocab)])
# save those files
with open(os.path.join(directory, word_vocab_filename), "wb") as wv, \
open(os.path.join(directory, word2idx_filename), "wb") as wd, \
open(os.path.join(directory, char_vocab_filename), "wb") as cv, \
open(os.path.join(directory, char2idx_filename), "wb") as cd:
pickle.dump(word_vocab, wv)
pickle.dump(word2idx, wd)
pickle.dump(char_vocab, cv)
pickle.dump(char2idx, cd)
print("Vocabulary created successfully.")
return word_vocab, word2idx, char_vocab, char2idx
def build_embeddings(vocab, embedding_path="", output_path="", vec_size=50):
embedding_dict = {}
# Load pretrained embeddings if an embedding path is provided
if embedding_path:
# Get the path associated to the embedding size we want
embedding_path = embedding_path.format(vec_size)
with open(embedding_path, "r", encoding="utf-8") as f:
for line in f:
values = line.split()
word = values[0]
vector = np.asarray(values[1:], dtype="float32")
if word in vocab:
embedding_dict[word] = vector
embedding_dict["--NULL--"] = np.asarray([0. for _ in range(vec_size)])
embedding_dict["--UNK--"] = np.asarray([0. for _ in range(vec_size)])
embedding_matrix = []
count = 0
for v in vocab:
if v in embedding_dict:
embedding_matrix.append(embedding_dict[v])
else:
count += 1
embedding_matrix.append(np.random.normal(0, 0.1, vec_size))
# Save the embedding matrix
with open(os.path.join(config.train_dir, output_path), "wb") as e:
pickle.dump(embedding_matrix, e)
def save_checkpoint(state, is_best, filename="/output/checkpoint.pkl"):
"""Save checkpoint if a new best is achieved"""
if is_best:
print("=> Saving a new best model.")
torch.save(state, filename) # save checkpoint
else:
print("=> Validation loss did not improve.")
def custom_sampler(data, valid_size=0.02):
# Define a split for train/valid
num_train = len(data)
indices = list(range(num_train))
split = int(np.floor((1 - valid_size) * num_train))
train_idx, valid_idx = indices[:split], indices[split:]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
return train_sampler, valid_sampler
def masked_softmax(logits, mask, dim=-1, log_softmax=False):
"""Take the softmax of `logits` over given dimension, and set
entries to 0 wherever `mask` is 0.
"""
mask = mask.type(torch.float32)
masked_logits = mask * logits + (1 - mask) * -1e30
softmax_fn = F.log_softmax if log_softmax else F.softmax
probs = softmax_fn(masked_logits, dim)
return probs
def to_string(context, idx2word, start_idx, end_idx):
if config.cuda:
return " ".join([idx2word[i] for i in context.cpu().numpy().tolist()[start_idx: end_idx + 1]])
else:
return " ".join([idx2word[i] for i in context.numpy().tolist()[start_idx: end_idx + 1]])
def discretize(p_start, p_end, max_len=15, no_answer=False):
if p_start.min() < 0 or p_start.max() > 1 \
or p_end.min() < 0 or p_end.max() > 1:
raise ValueError('Expected p_start and p_end to have values in [0, 1]')
# Compute pairwise probabilities
p_start = p_start.unsqueeze(dim=2)
p_end = p_end.unsqueeze(dim=1)
p_joint = torch.matmul(p_start, p_end) # (batch_size, c_len, c_len)
# Restrict to pairs (i, j) such that i <= j <= i + max_len - 1
c_len, device = p_start.size(1), p_start.device
is_legal_pair = torch.triu(torch.ones((c_len, c_len), device=device))
is_legal_pair -= torch.triu(torch.ones((c_len, c_len), device=device),
diagonal=max_len)
if no_answer:
# Index 0 is no-answer
p_no_answer = p_joint[:, 0, 0].clone()
is_legal_pair[0, :] = 0
is_legal_pair[:, 0] = 0
else:
p_no_answer = None
p_joint *= is_legal_pair
# Take pair (i, j) that maximizes p_joint
max_in_row, _ = torch.max(p_joint, dim=2)
max_in_col, _ = torch.max(p_joint, dim=1)
start_idxs = torch.argmax(max_in_row, dim=-1)
end_idxs = torch.argmax(max_in_col, dim=-1)
if no_answer:
# Predict no-answer whenever p_no_answer > max_prob
max_prob, _ = torch.max(max_in_col, dim=-1)
start_idxs[p_no_answer > max_prob] = 0
end_idxs[p_no_answer > max_prob] = 0
return start_idxs, end_idxs
def compute_batch_metrics(context, idx2word, pred1, pred2, batch_labels):
starts, ends = discretize(pred1.exp(), pred2.exp(), 15, False)
ems = 0
f1s = 0
for j in range(len(context)):
labels = [l.split(" ") for l in batch_labels[j].split("|")]
ground_truths = [to_string(context[j], idx2word, int(l[0]), int(l[1])) for l in labels]
prediction = to_string(context[j], idx2word, starts[j].item(), ends[j].item())
max_em = 0
max_f1 = 0
for gt in ground_truths:
f1 = compute_f1(gt, prediction)
if f1 > max_f1:
max_f1 = f1
em = compute_em(gt, prediction)
if em > max_em:
max_em = em
ems += max_em
f1s += max_f1
return ems, f1s
# All methods below this line are from the official SQuAD 2.0 eval script
# https://worksheets.codalab.org/rest/bundles/0x6b567e1cf2e041ec80d7098f031c5c9e/contents/blob/
def normalize_answer(s):
"""Convert to lowercase and remove punctuation, articles and extra whitespace."""
def remove_articles(text):
regex = re.compile(r'\b(a|an|the)\b', re.UNICODE)
return re.sub(regex, ' ', 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))))
def get_tokens(s):
if not s:
return []
return normalize_answer(s).split()
def compute_em(a_gold, a_pred):
return int(normalize_answer(a_gold) == normalize_answer(a_pred))
def compute_f1(a_gold, a_pred):
gold_toks = get_tokens(a_gold)
pred_toks = get_tokens(a_pred)
common = Counter(gold_toks) & Counter(pred_toks)
num_same = sum(common.values())
if len(gold_toks) == 0 or len(pred_toks) == 0:
# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
return int(gold_toks == pred_toks)
if num_same == 0:
return 0
precision = 1.0 * num_same / len(pred_toks)
recall = 1.0 * num_same / len(gold_toks)
f1 = (2 * precision * recall) / (precision + recall)
return f1