-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
168 lines (140 loc) · 7.7 KB
/
model.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
'''
Model classes
'''
import torch
from allennlp.modules.seq2vec_encoders import Seq2VecEncoder, PytorchSeq2VecWrapper
from allennlp.models import Model
from overrides import overrides
from allennlp.training.metrics import CategoricalAccuracy, BooleanAccuracy
from torch.nn.functional import normalize
import torch.nn.functional as F
import torch.nn as nn
import pdb
class Biencoder(Model):
def __init__(self, args,
mention_encoder: Seq2VecEncoder,
entity_encoder: Seq2VecEncoder,
vocab):
super().__init__(vocab)
self.args = args
self.mention_encoder = mention_encoder
self.accuracy = CategoricalAccuracy()
self.entity_encoder = entity_encoder
self.istrainflag = 1
def forward(self, gold_dui_canonical_and_def_concatenated,
context: torch.Tensor = None,
gold_duidx: torch.Tensor = None,
mention_uniq_id: torch.Tensor = None,
candidates_canonical_and_def_concatenated: torch.Tensor = None,
gold_location_in_candidates: torch.Tensor = None):
if context == None and gold_duidx == None and mention_uniq_id == None and \
candidates_canonical_and_def_concatenated == None and gold_location_in_candidates == None:
output = {}
output['encoded_entities'] = self.entity_encoder(
cano_and_def_concatnated_text=gold_dui_canonical_and_def_concatenated)
return output
batch_num = context['tokens']['token_ids'].size(0)
device = torch.get_device(context['tokens']['token_ids']) if torch.cuda.is_available() else torch.device('cpu')
contextualized_mention = self.mention_encoder(context)
encoded_entites = self.entity_encoder(cano_and_def_concatnated_text=gold_dui_canonical_and_def_concatenated)
if self.args.scoring_function_for_model == 'cossim':
contextualized_mention = normalize(contextualized_mention, dim=1)
encoded_entites = normalize(encoded_entites, dim=1)
encoded_entites = encoded_entites.squeeze(1)
dot_product = torch.matmul(contextualized_mention, encoded_entites.t()) # [bs, bs]
mask = torch.eye(batch_num).to(device)
loss = F.log_softmax(dot_product, dim=-1) * mask
loss = (-loss.sum(dim=1)).mean()
output = {'loss': loss}
if self.istrainflag:
golds = torch.eye(batch_num).to(device)
self.accuracy(dot_product, torch.argmax(golds, dim=1))
else:
output['gold_duidx'] = gold_duidx
output['encoded_mentions'] = contextualized_mention
return output
@overrides
def get_metrics(self, reset: bool = False):
return {"accuracy": self.accuracy.get_metric(reset)}
def return_entity_encoder(self):
return self.entity_encoder
class BiencoderSqueezedCandidateEvaluator(Model):
'''
For dev and test evaluation with Surface-based candidates.
'''
def __init__(self, args,
mention_encoder: Seq2VecEncoder,
entity_encoder: Seq2VecEncoder,
vocab):
super().__init__(vocab)
self.args = args
self.mention_encoder = mention_encoder
self.accuracy = CategoricalAccuracy()
self.BCEWloss = nn.BCEWithLogitsLoss()
self.mesloss = nn.MSELoss()
self.entity_encoder = entity_encoder
def forward(self, context, gold_dui_canonical_and_def_concatenated, gold_duidx, mention_uniq_id,
candidates_canonical_and_def_concatenated, gold_location_in_candidates):
batch_num = context['tokens']['token_ids'].size(0)
device = torch.get_device(context['tokens']['token_ids']) if torch.cuda.is_available() else torch.device('cpu')
contextualized_mention = self.mention_encoder(context)
encoded_entites = self._candidate_entities_emb_returner(batch_num, candidates_canonical_and_def_concatenated)
if self.args.scoring_function_for_model == 'cossim':
contextualized_mention_forcossim = normalize(contextualized_mention, dim=1)
encoded_entites_forcossim = normalize(encoded_entites, dim=2)
scores = torch.bmm(encoded_entites_forcossim, contextualized_mention_forcossim.view(batch_num, -1, 1)).squeeze()
elif self.args.scoring_function_for_model == 'indexflatip':
scores = torch.bmm(encoded_entites, contextualized_mention.view(batch_num, -1, 1)).squeeze()
else:
assert self.args.searchMethodWithFaiss == 'indexflatl2'
raise NotImplementedError
loss = self.BCEWloss(scores, gold_location_in_candidates.view(batch_num, -1).float())
output = {'loss': loss,
'contextualized_mention': contextualized_mention}
self.accuracy(scores, torch.argmax(gold_location_in_candidates.view(batch_num, -1), dim=1))
return output
@overrides
def get_metrics(self, reset: bool = False):
return {"accuracy": self.accuracy.get_metric(reset)}
def return_entity_encoder(self):
return self.entity_encoder
def _candidate_entities_emb_returner(self, batch_size, candidates_canonical_and_def_concatenated):
cand_nums = candidates_canonical_and_def_concatenated['tokens']['token_ids'].size(1)
candidates_canonical_and_def_concatenated['tokens']['token_ids'] = \
candidates_canonical_and_def_concatenated['tokens']['token_ids'].view(batch_size * cand_nums, -1)
candidates_canonical_and_def_concatenated['tokens']['mask'] = \
candidates_canonical_and_def_concatenated['tokens']['mask'].view(batch_size * cand_nums, -1)
candidates_canonical_and_def_concatenated['tokens']['type_ids'] = \
candidates_canonical_and_def_concatenated['tokens']['type_ids'].view(batch_size * cand_nums, -1)
candidate_embs = self.entity_encoder(candidates_canonical_and_def_concatenated)
candidate_embs = candidate_embs.view(batch_size, cand_nums, -1)
return candidate_embs
class BiencoderNNSearchEvaluator(Model):
'''
For dev and test evaluation with Approximante Nearest Neighbor Search based candidates.
'''
def __init__(self, args,
mention_encoder: Seq2VecEncoder,
vocab, faiss_searcher):
super().__init__(vocab)
self.args = args
self.mention_encoder = mention_encoder
self.accuracy = CategoricalAccuracy()
self.faiss_searcher = faiss_searcher
self.mention_idx2candidate_entity_idxs = {}
def forward(self, context, gold_dui_canonical_and_def_concatenated, gold_duidx, mention_uniq_id,
candidates_canonical_and_def_concatenated, gold_location_in_candidates):
contextualized_mention = self.mention_encoder(context)
distances, in_faiss_idxes = self.faiss_searcher.indexed_faiss.search(contextualized_mention.cpu().numpy(),
k=self.args.how_many_top_hits_preserved)
for mention_idx, in_faiss_candidates, gold_duidx_ in zip(mention_uniq_id.cpu().numpy(), in_faiss_idxes, gold_duidx.cpu().numpy()):
candidate_entity_idxes = [self.faiss_searcher.kb_idx2entity_idx[idx]
for idx in in_faiss_candidates]
self.mention_idx2candidate_entity_idxs.update({mention_idx:
{'candidate_entity_idx':candidate_entity_idxes,
'gold_entity_idx': gold_duidx_}})
output = {}
return output
@overrides
def get_metrics(self, reset: bool = False):
return {"accuracy": self.accuracy.get_metric(reset)}