-
Notifications
You must be signed in to change notification settings - Fork 0
/
modeling_base.py
101 lines (87 loc) · 2.89 KB
/
modeling_base.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
# Copyright (c) 2022, Yamagishi Laboratory, National Institute of Informatics
# Author: Canasai Kruengkrai ([email protected])
# All rights reserved.
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
from transformers import AutoConfig, AutoModel
from transformers.modeling_utils import PreTrainedModel
from transformers.file_utils import ModelOutput
class BaseModelOutput(ModelOutput):
loss: torch.FloatTensor = None
logits: torch.FloatTensor = None
penultimate_layer: torch.FloatTensor = None
class Classifier(nn.Module):
def __init__(
self,
hidden_size,
num_labels,
dropout=0.1,
):
super().__init__()
self.dense = nn.Linear(hidden_size, hidden_size)
self.dropout = nn.Dropout(dropout)
self.out_proj = nn.Linear(hidden_size, num_labels)
def forward(self, x):
x = self.dropout(x)
x = self.dense(x)
z = torch.relu(x)
x = self.dropout(z)
x = self.out_proj(x)
return x, z
class BaseModel(PreTrainedModel):
def __init__(self, hparams, num_labels):
config = AutoConfig.from_pretrained(
hparams.pretrained_model_name, num_labels=num_labels
)
super().__init__(config)
setattr(
self,
self.config.model_type,
AutoModel.from_pretrained(
hparams.pretrained_model_name, config=self.config
),
)
self.classifier = Classifier(
self.config.hidden_size,
num_labels,
dropout=hparams.classifier_dropout_prob,
)
self.hparams = hparams
def forward(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
if input_ids is not None:
assert input_ids.dim() == 2 # batch x len
encoder_outputs = getattr(self, self.config.model_type)(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=True,
)
features = encoder_outputs.last_hidden_state[:, 0] # equiv. to [CLS]
logits, penultimate_layer = self.classifier(features)
loss = None
if labels is not None:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1))
return BaseModelOutput(
loss=loss,
logits=logits,
penultimate_layer=penultimate_layer,
)