-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert_contexts.py
293 lines (243 loc) · 11.8 KB
/
bert_contexts.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
import argparse
import numpy as np
import pandas as pd
import torch
from collections import defaultdict, Counter
from itertools import product
from tqdm import tqdm
from transformers import BertTokenizer, BertModel, BertForMaskedLM, BertConfig
def _softmax_score(vec, idx):
return float(vec[idx].exp() / vec.exp().sum())
def mask_blend(sentence, blend, replacement='[MASK]'):
sentence = sentence.lower() # this is necessary because blends are lowercase
sentence = sentence.replace(blend, replacement)
return sentence
def augment_sentence(tokenized_sentence, twosided_flag=False):
tokenized_sentence = ['[CLS]'] + tokenized_sentence + ['[SEP]']
tokenized_sentence_tmp = []
for i, w in enumerate(tokenized_sentence):
if w=='[MASK]':
if twosided_flag:
tokenized_sentence_tmp += ['[MASK]', '[MASK]']
else:
tokenized_sentence_tmp.append(w)
else:
tokenized_sentence_tmp.append(w)
return tokenized_sentence_tmp
def find_mask(tokenized_sentence, twosided_flag=False):
target_indices = [i for i, x in enumerate(tokenized_sentence) if x=='[MASK]']
tokenized_sent_tmp = []
for i, x in enumerate(target_indices):
if twosided_flag: # then double i's
tokenized_sent_tmp.extend([x + i, x + i + 1])
else:
tokenized_sent_tmp.append(x)
return tokenized_sent_tmp
def embed_sentence(model, tokenizer, augmented_sentence):
# check sentence shape
if (augmented_sentence[0]!='[CLS]') or (augmented_sentence[-1]!='[SEP]'):
print("Please check your tokenization.")
indexed_tokens = tokenizer.convert_tokens_to_ids(augmented_sentence)
segments_ids = [0] * len(augmented_sentence)
tokens_tensor, segments_tensor = torch.tensor([indexed_tokens]), torch.tensor([segments_ids])
with torch.no_grad():
return model(tokens_tensor, segments_tensor)
def parse_single_sentence(model, tokenizer, sentence, blend, twosided_flag=True):
masked_sentence = mask_blend(sentence, blend)
tokenized_sentence = tokenizer.tokenize(masked_sentence)
augmented_sentence = augment_sentence(tokenized_sentence, twosided_flag=twosided_flag)
target_indices = find_mask(tokenized_sentence, twosided_flag=twosided_flag)
embedded_sentence = embed_sentence(model, tokenizer, augmented_sentence)
return embedded_sentence, target_indices
def get_word_embedding(embedded_sentence, target_index):
return embedded_sentence[0, target_index + 1, :].detach()
def get_piece_score(embedding, piece_index, raw=False):
if raw:
score = embedding[piece_index].item()
else:
score = _softmax_score(embedding, piece_index)
return score
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--anns', help='annotations file in tsv format')
parser.add_argument('--neg-samps', help='negative samples file in tsv format')
parser.add_argument('--sentences', help='contexts for each word in tsv format, leave empty for no context')
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--outf', help='output file in tsv format')
args = parser.parse_args()
# load from files
neg_samps = pd.read_csv(args.neg_samps, sep="\t", encoding='utf-8')
bases = {}
with open(args.anns) as in_f:
in_f.readline() # header
for l in in_f:
# Annotation, Form, Bases, Semantic Affixes, Triv Affixes, Additional Segment Count, PAXOBS, Blend Type
_, fullform, basess, _, _, _, _, _ = l.strip().split('\t')
bases[fullform] = basess.split(' ')
if args.sentences is not None:
sentences = pd.read_csv(args.sentences, sep="\t")
blend_sentences_only = sentences[sentences['Category']=='blend']
blend_sentences_only = blend_sentences_only.reset_index().iterrows()
else:
blend_sentences_only = []
for b in bases.keys():
blend_sentences_only.append((None, {'sentence':f'{b}', 'Word':b}))
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
config = BertConfig.from_pretrained("bert-base-uncased", output_hidden_states=True)
model = BertForMaskedLM.from_pretrained('bert-base-uncased', config=config)
model.eval()
# main loop
ranks = defaultdict(list)
lcands = {}
rcands = {}
bcands = {}
token_counts = {}
token_neg_counts = []
for _, row in tqdm(blend_sentences_only):
sentence = row['sentence']
blend = row['Word']
# run bert
embedded_sentence, target_indices = parse_single_sentence(model, tokenizer, sentence, blend)
# get candidate lists
true_bases = bases[blend]
if len(true_bases) != 2 and args.verbose:
print(f'{len(true_bases)} bases in {blend}')
t_l = true_bases[0]
t_r = true_bases[-1]
negsamp_df = neg_samps[neg_samps['FORM']==blend]
negsamp_prefs = negsamp_df[negsamp_df['PLACE']=='PRE']['NEGATIVE'].tolist()
negsamp_sufs = negsamp_df[negsamp_df['PLACE']=='SUF']['NEGATIVE'].tolist()
all_ls = [t_l] + negsamp_prefs
all_rs = [t_r] + negsamp_sufs
lcands[blend] = len(all_ls)
rcands[blend] = len(all_rs)
all_l_toks = [tokenizer.encode(b) for b in all_ls] # list of list of ints
all_r_toks = [tokenizer.encode(b) for b in all_rs] # list of list of ints
true_l_len = len(all_l_toks[0])
true_r_len = len(all_r_toks[0])
# token count stats
if t_l not in token_counts:
token_counts[t_l] = true_l_len
for toks in all_l_toks[1:]:
token_neg_counts.append(len(toks))
if t_r not in token_counts:
token_counts[t_r] = true_r_len
for toks in all_r_toks[1:]:
token_neg_counts.append(len(toks))
# scoring
l_scores = np.zeros(len(tokenizer))
r_scores = np.zeros(len(tokenizer))
trg_i = 0
while trg_i < len(target_indices):
# try summing raw scores
l_scores += get_word_embedding(embedded_sentence[0], target_indices[trg_i]).numpy()
trg_i += 1
r_scores += get_word_embedding(embedded_sentence[0], target_indices[trg_i]).numpy()
trg_i += 1
l_exp = np.exp(l_scores)
r_exp = np.exp(r_scores)
l_softmax = l_exp / (l_exp.sum())
r_softmax = r_exp / (r_exp.sum())
pair_token_ids = product(all_l_toks, all_r_toks)
pair_scores = []
pairs = []
for l, r in pair_token_ids:
pair_scores.append(-l_softmax[l[0]] - r_softmax[r[0]]) # so sort is good to bad
pairs.append((l, r)) # for tie breaking
bcands[blend] = len(pairs)
assert bcands[blend] == lcands[blend] * rcands[blend]
np_pscores = np.array(pair_scores)
true_score = pair_scores[0]
tied = np.where(np_pscores==true_score)[0]
sorted_scores = np.argsort(np_pscores)
true_bases_rank = np.where(sorted_scores==0)[0][0] + 1
toks_compared = 1
# check if tie and true bases have more than one piece
while len(tied) > 1 and (true_l_len > toks_compared or true_r_len > toks_compared):
# anchor rank to start tiebreak from
start_rank = true_bases_rank
new_cands = np.array(pairs)[tied].tolist()
assert len(new_cands) == len(tied)
leftmask = ' '.join([tokenizer.ids_to_tokens[all_l_toks[0][j]]
for j in range(min(toks_compared, len(all_l_toks[0])))])
rightmask = ' '.join([tokenizer.ids_to_tokens[all_r_toks[0][j]]
for j in range(min(toks_compared, len(all_r_toks[0])))])
# re-rank finished token pairs above unfinished ones and save true's sublist
# filtering missing tokens from left first, this is a heuristic.
if true_l_len <= toks_compared:
new_cands = [(i, c) for i, c in zip(tied, new_cands) if len(c[0]) <= toks_compared]
else:
leftmask += ' [MASK]'
new_cands = [(i, c) for i, c in zip(tied, new_cands) if len(c[0]) > toks_compared]
start_rank += (len(tied) - len(new_cands))
if true_r_len <= toks_compared:
new_cands = [(i, c) for i, c in new_cands if len(c[1]) <= toks_compared]
else:
rightmask += ' [MASK]'
old_len = len(new_cands)
new_cands = [(i, c) for i, c in new_cands if len(c[1]) > toks_compared]
start_rank += (old_len - len(new_cands))
assert new_cands[0][0] == 0
replacement = leftmask + ' ' + rightmask
# tie-break
trg_id = 0
l_scores = np.zeros(len(l_scores))
r_scores = np.zeros(len(r_scores))
while trg_id < len(target_indices):
if true_l_len > toks_compared:
l_scores += get_word_embedding(embedded_sentence[0], target_indices[trg_id]).numpy()
trg_id += 1
if true_r_len > toks_compared:
r_scores += get_word_embedding(embedded_sentence[0], target_indices[trg_id]).numpy()
trg_id += 1
l_exp = np.exp(l_scores)
r_exp = np.exp(r_scores)
l_softmax = l_exp / (l_exp.sum())
r_softmax = r_exp / (r_exp.sum())
cand_pair_scores = []
for i, (l, r) in new_cands:
score = 0.0
if true_l_len > toks_compared: score -= l_softmax[l[toks_compared]]
if true_r_len > toks_compared: score -= r_softmax[r[toks_compared]]
cand_pair_scores.append(score)
np_pscores = np.array(cand_pair_scores)
true_score = cand_pair_scores[0]
tied = np.where(np_pscores==true_score)[0]
sorted_scores = np.argsort(np_pscores)
new_true_rank = start_rank + np.where(sorted_scores==0)[0][0]
true_bases_rank = new_true_rank
toks_compared += 1
ranks[blend].append(true_bases_rank)
# aggregate
with open(args.outf, 'w') as out_f:
rrs = []
at1_hits = 0
out_f.write('blend\tleft cands\tright cands\tpair cands\trank\treciprocal\n')
for b in sorted(bases):
if b not in ranks:
negsamp_df = neg_samps[neg_samps['FORM']==b]
negsamp_prefs = negsamp_df[negsamp_df['PLACE']=='PRE']['NEGATIVE'].tolist()
negsamp_sufs = negsamp_df[negsamp_df['PLACE']=='SUF']['NEGATIVE'].tolist()
lcands[b] = (1+len(negsamp_prefs))
rcands[b] = (1 + len(negsamp_sufs))
bcands[b] = (1+len(negsamp_prefs)) * (1 + len(negsamp_sufs))
rank = bcands[b]
else:
rlist = ranks[b]
rank = np.average(rlist)
if rank == 1:
at1_hits += 1
rr = 1.0/rank
rrs.append(rr)
outstr = f'{b}\t{lcands[b]}\t{rcands[b]}\t{bcands[b]}\t{rank:.1f}\t{rr:.3f}\n'
out_f.write(outstr)
pat1 = at1_hits/len(ranks)
mrr = np.average(rrs)
out_f.write(f'\nH@1\t{at1_hits}\tP@1\t{pat1:.4f}\n\t\tMRR\t{mrr:.4f}\n')
print(at1_hits, mrr, pat1)
print(f'average gold token counts: {np.average(list(token_counts.values()))}')
print(f'average false candidate token counts: {np.average(token_neg_counts)}')
one_tok_bases = Counter(token_counts.values())[1]
print(f'gold bases with only one token: {one_tok_bases}, prop {one_tok_bases/len(token_counts)}')
if __name__ == '__main__':
main()