-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
47 lines (39 loc) · 2.41 KB
/
models.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
from transformers import *
import torch
class ElectraReINTELClassification(ElectraPreTrainedModel):
def __init__(self, config):
super(ElectraReINTELClassification, self).__init__(config=config)
self.electra = ElectraModel(config)
self.num_labels = config.num_labels
self.init_weights()
self.ln = torch.nn.Linear(config.hidden_size * 4, self.num_labels)
def forward(self, input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None,
start_positions=None, end_positions=None):
outputs = self.electra(input_ids, attention_mask=attention_mask, position_ids=position_ids, head_mask=head_mask)[1]
cls_output = torch.cat((outputs[-1][:, 0, ...], outputs[-2][:, 0, ...], outputs[-3][:, 0, ...], outputs[-4][:, 0, ...]), -1)
logits = self.ln(cls_output)
return logits
class PhoBERTReINTELClassification(BertPreTrainedModel):
def __init__(self, config):
super(PhoBERTReINTELClassification, self).__init__(config)
self.roberta = RobertaModel(config)
self.num_labels = config.num_labels
self.outputs = torch.nn.Linear(config.hidden_size * 4, self.num_labels)
def forward(self, input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None,
start_positions=None, end_positions=None):
outputs = self.roberta(input_ids, attention_mask=attention_mask)
cls_output = torch.cat((outputs[2][-1][:, 0, ...], outputs[2][-2][:, 0, ...], outputs[2][-3][:, 0, ...], outputs[2][-4][:, 0, ...]), -1)
logits = self.outputs(cls_output)
return logits
class BertReINTELClassification(BertPreTrainedModel):
def __init__(self, config):
super(BertReINTELClassification, self).__init__(config=config)
self.bert = BertModel(config)
self.num_labels = config.num_labels
self.ln = torch.nn.Linear(config.hidden_size * 4, self.num_labels)
def forward(self, input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None,
start_positions=None, end_positions=None):
outputs = self.bert(input_ids, attention_mask=attention_mask, position_ids=position_ids, head_mask=head_mask)[2]
cls_output = torch.cat((outputs[-1][:, 0, ...], outputs[-2][:, 0, ...], outputs[-3][:, 0, ...], outputs[-4][:, 0, ...]), -1)
logits = self.ln(cls_output)
return logits