-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodeling.py
74 lines (60 loc) · 2.92 KB
/
modeling.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
import math
import torch
import logging
from torch import nn
from transformers.modeling_bert import (BertModel, BertPreTrainedModel)
logger = logging.getLogger(__name__)
class Observer(BertPreTrainedModel):
def __init__(self, config):
super(Observer, self).__init__(config)
self.bert = BertModel(config)
self.dropout = nn.Dropout(p=0.2)
self.classifier = nn.Linear(768, 1)
self.init_weights()
def forward(self, input_ids, token_type_ids, valid_mask, position_ids, weight, is_training=True):
batch_size = input_ids.size()[0]
device = input_ids.device
pool_output = self.bert(input_ids,
attention_mask=valid_mask,
token_type_ids=token_type_ids,
position_ids=position_ids)[1]
score = self.classifier(self.dropout(pool_output)) # [B, 1]
output = score
if is_training:
similarities = torch.cat((score[0::2], score[1::2]), dim=1) # [bs//2, 2]
labels = torch.zeros((batch_size//2,), dtype=torch.int64).to(device) # [bs//2,]
loss_fct = nn.CrossEntropyLoss(reduce=False)
loss = loss_fct(similarities, labels) # [bs//2]
if weight is None:
weight = torch.ones((batch_size//2,), dtype=torch.int64).to(device)
loss_origin = loss.sum() / (batch_size//2)
loss_weight = torch.dot(weight, loss) / (batch_size//2)
output = loss_origin, loss_weight, output
return output
class Reranker(BertPreTrainedModel):
def __init__(self, config):
super(Reranker, self).__init__(config)
self.bert = BertModel(config)
self.dropout = nn.Dropout(p=0.2)
self.classifier = nn.Linear(768, 1)
self.init_weights()
def forward(self, input_ids, token_type_ids, valid_mask, position_ids, weight=None, is_training=True):
batch_size = input_ids.size()[0]
device = input_ids.device
pool_output = self.bert(input_ids,
attention_mask=valid_mask,
token_type_ids=token_type_ids,
position_ids=position_ids)[1]
score = self.classifier(self.dropout(pool_output)) # [B, 1]
output = score
if is_training:
similarities = torch.cat((score[0::2], score[1::2]), dim=1) # [bs//2, 2]
labels = torch.zeros((batch_size//2,), dtype=torch.int64).to(device) # [bs//2,]
loss_fct = nn.CrossEntropyLoss(reduce=False)
loss = loss_fct(similarities, labels) # [bs//2]
if weight is None:
weight = torch.ones((batch_size//2,), dtype=torch.int64).to(device)
loss_origin = loss.sum() / (batch_size//2)
loss_weight = torch.dot(weight, loss) / (batch_size//2)
output = loss_origin, loss_weight, output
return output