-
Notifications
You must be signed in to change notification settings - Fork 1
/
classifiers.py
242 lines (182 loc) · 9.37 KB
/
classifiers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#%%
from pytorch_transformers import BertModel, BertPreTrainedModel
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
class BertForSequenceClassification(BertPreTrainedModel):
def __init__(self, config):
super(BertForSequenceClassification, self).__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, self.config.num_labels)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
position_ids=None, head_mask=None):
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
pooled_output = outputs[1]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
reshaped_logits = logits.view(-1, 5)
_, reshaped_labels = torch.max(labels.view(-1, 5), 1)
loss_fct = CrossEntropyLoss()
loss = loss_fct(reshaped_logits, reshaped_labels)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)
class Baseline(BertPreTrainedModel):
def __init__(self, config):
super(Baseline, self).__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, self.config.num_labels)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
position_ids=None, head_mask=None):
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
pooled_output = outputs[1]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
reshaped_logits = logits.view(-1, 5)
_, reshaped_labels = torch.max(labels.view(-1, 5), 1)
loss_fct = CrossEntropyLoss()
loss = loss_fct(reshaped_logits, reshaped_labels)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)
class NoHiddenLayerClassification(BertPreTrainedModel):
# NoHiddenLayerClassification
def __init__(self, config):
super(NoHiddenLayerClassification, self).__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.fc1 = nn.Linear(in_features=768*5,out_features=2)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
position_ids=None, head_mask=None):
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
# batch size * 768
pooled_output = outputs[1]
# need reshape
pooled_output = pooled_output.view(1,-1)
pooled_output = self.dropout(pooled_output)
logits = self.fc1(pooled_output)
outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
model_output = logits
real_label = torch.tensor([labels[0]],device="cuda")
loss_fct = CrossEntropyLoss()
loss = loss_fct(model_output, real_label)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)
class oneHiddenLayer_768_classifier(BertPreTrainedModel):
# oneHiddenLayer_768_classifier
def __init__(self, config):
super(oneHiddenLayer_768_classifier, self).__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.fc1 = nn.Linear(in_features=768*5,out_features=768)
self.fc2 = nn.Linear(in_features=768,out_features=2)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
position_ids=None, head_mask=None):
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
# batch size * 768
pooled_output = outputs[1]
# need reshape
pooled_output = pooled_output.view(1,-1)
pooled_output = self.dropout(pooled_output)
logits = self.fc1(pooled_output)
pooled_output = self.dropout(logits)
logits = self.fc2(pooled_output)
outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
model_output = logits
real_label = torch.tensor([labels[0]],device="cuda")
loss_fct = CrossEntropyLoss()
loss = loss_fct(model_output, real_label)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)
class twoHiddenLayer_with_3840_768_classifier(BertPreTrainedModel):
# twoHiddenLayer_with_2304_2304_classifier
def __init__(self, config):
super(twoHiddenLayer_with_3840_768_classifier, self).__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.fc1 = nn.Linear(in_features=768*5,out_features=768*5)
self.fc2 = nn.Linear(in_features=768*5,out_features=768)
self.fc3 = nn.Linear(in_features=768,out_features=2)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
position_ids=None, head_mask=None):
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
# batch size * 768
pooled_output = outputs[1]
# need reshape
pooled_output = pooled_output.view(1,-1)
pooled_output = self.dropout(pooled_output)
logits = self.fc1(pooled_output)
pooled_output = self.dropout(logits)
logits = self.fc2(pooled_output)
pooled_output = self.dropout(logits)
logits = self.fc3(pooled_output)
outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
model_output = logits
real_label = torch.tensor([labels[0]],device="cuda")
loss_fct = CrossEntropyLoss()
loss = loss_fct(model_output, real_label)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)
class twoHiddenLayer_with_3840_3840_classifier(BertPreTrainedModel):
# twoHiddenLayer_with_2304_2304_classifier
def __init__(self, config):
super(twoHiddenLayer_with_3840_3840_classifier, self).__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.fc1 = nn.Linear(in_features=768*5,out_features=768*5)
self.fc2 = nn.Linear(in_features=768*5,out_features=768*5)
self.fc3 = nn.Linear(in_features=768*5,out_features=2)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
position_ids=None, head_mask=None):
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
# batch size * 768
pooled_output = outputs[1]
# need reshape
pooled_output = pooled_output.view(1,-1)
pooled_output = self.dropout(pooled_output)
logits = self.fc1(pooled_output)
pooled_output = self.dropout(logits)
logits = self.fc2(pooled_output)
pooled_output = self.dropout(logits)
logits = self.fc3(pooled_output)
outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
model_output = logits
real_label = torch.tensor([labels[0]],device="cuda")
loss_fct = CrossEntropyLoss()
loss = loss_fct(model_output, real_label)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)
CLASSIFIER_CLASSES = {
'default': BertForSequenceClassification,
'baseLine': Baseline,
'NoHidden': NoHiddenLayerClassification,
'OneHidden':oneHiddenLayer_768_classifier,
'twoHidden_3840_768':twoHiddenLayer_with_3840_768_classifier,
'twoHidden_3840_3840':twoHiddenLayer_with_3840_3840_classifier
}