-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
71 lines (58 loc) · 2.37 KB
/
Copy pathmodels.py
File metadata and controls
71 lines (58 loc) · 2.37 KB
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
import torch
from torch import nn
from transformers import (
RobertaConfig,
RobertaForSequenceClassification,
RobertaTokenizer,
)
# ANCE model
class ANCE(RobertaForSequenceClassification):
# class Pooler: # adapt to DPR
# def __init__(self, pooler_output):
# self.pooler_output = pooler_output
def __init__(self, config):
RobertaForSequenceClassification.__init__(self, config)
self.embeddingHead = nn.Linear(config.hidden_size, 768)
self.norm = nn.LayerNorm(768)
self.apply(self._init_weights)
self.use_mean = False
def _init_weights(self, module):
""" Initialize the weights """
if isinstance(module, (nn.Linear, nn.Embedding, nn.Conv1d)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=0.02)
def query_emb(self, input_ids, attention_mask):
outputs1 = self.roberta(input_ids=input_ids,
attention_mask=attention_mask)
outputs1 = outputs1.last_hidden_state
full_emb = self.masked_mean_or_first(outputs1, attention_mask)
query1 = self.norm(self.embeddingHead(full_emb))
return query1
def doc_emb(self, input_ids, attention_mask):
return self.query_emb(input_ids, attention_mask)
def masked_mean_or_first(self, emb_all, mask):
if self.use_mean:
return self.masked_mean(emb_all, mask)
else:
return emb_all[:, 0]
def masked_mean(self, t, mask):
s = torch.sum(t * mask.unsqueeze(-1).float(), axis=1)
d = mask.sum(axis=1, keepdim=True).float()
return s / d
def forward(self, input_ids, attention_mask, wrap_pooler=False):
return self.query_emb(input_ids, attention_mask)
def load_model(model_type, model_path):
if model_type == "ANCE_Query" or model_type == "ANCE_Passage":
config = RobertaConfig.from_pretrained(
model_path,
finetuning_task="MSMarco",
)
tokenizer = RobertaTokenizer.from_pretrained(
model_path,
do_lower_case=True
)
model = ANCE.from_pretrained(model_path, config=config)
else:
raise ValueError
return tokenizer, model