-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistilBertSentiment.py
71 lines (50 loc) · 2.45 KB
/
DistilBertSentiment.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
import torch.nn as nn
from transformers import DistilBertModel, DistilBertTokenizer
import utils
import torch
from utils import *
from ExperimentBase import ExperimentBase
from tqdm import tqdm
class DistilBertSentiment(ExperimentBase):
def __init__(self, conf, num_classes, hidden_size = 768, dropout_rate=0.3):
self.conf = conf
self.conf.actual_name = "distilbert-base-uncased"
self.conf.model_name = self.conf.actual_name
super(DistilBertSentiment, self).__init__(conf)
self.base_model = DistilBertModel.from_pretrained(self.conf.actual_name)
self.tokenizer = DistilBertTokenizer.from_pretrained(self.conf.actual_name)
self.num_classes = num_classes
'''ClassificationHead'''
'''output dims from last layer of distillbert are known from hugginface'''
self.hidden = nn.Linear(768, hidden_size)
self.classifier = nn.Linear(hidden_size, num_classes)
self.dropout = nn.Dropout(dropout_rate)
self.activation = nn.Tanh()
self.softmax = nn.LogSoftmax(dim=1)
class DistilBertSentimentAvg(DistilBertSentiment):
def __init__(self, conf, num_classes, hidden_size = 768, dropout_rate=0.3):
super(DistilBertSentimentAvg, self).__init__(conf, num_classes, hidden_size=hidden_size, dropout_rate=dropout_rate )
self.conf.model_name += "_avg"
'''avg all hidden states for classification'''
def forward(self, input_ids, attention_mask=None):
outputs = self.base_model(input_ids, attention_mask=attention_mask)
x = torch.mean(outputs.last_hidden_state, dim=1)
x = self.dropout(x)
x = self.activation(self.hidden(x))
x = self.classifier(x)
return x
class DistilBertSentimentCLS(DistilBertSentiment):
def __init__(self, conf, num_classes, hidden_size = 768, dropout_rate=0.3):
super(DistilBertSentimentCLS, self).__init__(conf, num_classes, hidden_size=hidden_size, dropout_rate=dropout_rate )
self.conf.model_name += "_cls"
'''pick the first token for classification'''
def forward(self, input_ids, attention_mask=None):
outputs = self.base_model(input_ids, attention_mask=attention_mask)
x = self.dropout(outputs.last_hidden_state[:,0,:])
x = self.activation(self.hidden(x))
x = self.classifier(x)
return x
if __name__ == '__main__':
conf = utils.read_conf()
model = DistilBertSentiment(conf, 2)
print(model.count_parameters())