-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
234 lines (186 loc) · 7.45 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
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
from dataclasses import dataclass
import torch
from torch import nn
class LogisticRegression(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, dropout=0):
super().__init__()
print(f'Logistic Regression classifier of dim ({in_dim} {hid_dim} {out_dim})')
self.nn = nn.Sequential(
nn.Dropout(p=dropout),
nn.Linear(in_dim, hid_dim, bias=True),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout(p=dropout),
nn.Linear(hid_dim, out_dim, bias=True),
)
def forward(self, x, return_feat=False):
out = self.nn(x)
if return_feat:
return out, x
return out
class MLP2Layer(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, dropout=0):
super().__init__()
print(f'Logistic Regression classifier of dim ({in_dim} {hid_dim} {out_dim})')
self.nn = nn.Sequential(
nn.Dropout(p=dropout),
nn.Linear(in_dim, hid_dim, bias=True),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout(p=dropout),
nn.Linear(hid_dim, hid_dim / 2, bias=True),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout(p=dropout),
nn.Linear(hid_dim, out_dim, bias=True),
)
def forward(self, x):
return self.nn(x)
class BertClassifier(nn.Module):
FEAT_LEN = 768
def __init__(self, raw_bert, classifier):
super().__init__()
self.bert = raw_bert
self.fc = classifier
def forward(self, x, return_feat=False):
# x is a tokenized input
# feature = self.bert(input_ids=x[0], token_type_ids=x[1], attention_mask=x[2])
feature = self.bert(input_ids=x[0], attention_mask=x[2])
# out = self.fc(feature.pooler_output.flatten(1)) # not good for our task # (BS, E)
out = self.fc(feature.last_hidden_state.flatten(1)) # (BS, T, E)
if return_feat:
return out, feature.last_hidden_state.flatten(1)
return out
@dataclass
class BertClassiferHyperparams:
mlp_size: int
token_len: int
embed_len: int
class SimpleEnsemble(nn.Module):
"""
The simplest ensemble model, ie, averaging
"""
def __init__(self, components): # components is a list of models
super(SimpleEnsemble, self).__init__()
self.components = components
def forward(self, inputs):
assert len(self.components) == len(inputs)
preds = []
for model, input in zip(self.components, inputs):
preds.append(model(input))
return sum(preds) / len(preds)
class FixedWeightEnsemble(nn.Module):
"""
Learn a fixed set of weights
"""
def __init__(self, components):
super(FixedWeightEnsemble, self).__init__()
self.components = components
self.weights = nn.Linear(1, len(components), bias=False)
self.weightsInput = torch.tensor([1], dtype=torch.float).cuda()
def forward(self, inputs):
assert len(self.components) == len(inputs)
preds = []
for model, input in zip(self.components, inputs):
pred = model(input)
preds.append(pred)
weights = self.weights(self.weightsInput)
for i, weight in enumerate(weights):
preds[i] = preds[i] * weight
return sum(preds)
class DynamicWeightEnsemble(nn.Module):
"""
Learn the dynamic weights for different components
"""
def __init__(self, components, total_feat_len, dropout=0.2, hidden_len=256):
super(DynamicWeightEnsemble, self).__init__()
self.components = components
self.attention = nn.Sequential(
nn.Dropout(dropout),
nn.Linear(total_feat_len, hidden_len, bias=True),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout(dropout),
nn.Linear(hidden_len, len(components), bias=True),
nn.Softmax()
)
def forward(self, inputs):
assert len(self.components) == len(inputs)
preds, feats = [], []
for model, input in zip(self.components, inputs):
pred, feat = model(input, return_feat=True)
preds.append(pred)
feats.append(feat)
weights = self.attention(torch.cat(feats, dim=1))
weights = torch.transpose(weights, 0, 1)
for i in range(weights.size(0)):
for j in range(weights.size(1)):
preds[i][j] *= weights[i][j]
return sum(preds)
class AggregateFeatEnsemble(nn.Module):
"""
Learn the dynamic weights for different components
"""
def __init__(self, components, total_feat_len, num_classes, dropout=0.2, hidden_len=256):
super(AggregateFeatEnsemble, self).__init__()
self.components = nn.ModuleList(components)
self.nn = nn.Sequential(
nn.Dropout(dropout),
nn.Linear(total_feat_len, hidden_len, bias=True),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout(dropout),
nn.Linear(hidden_len, num_classes, bias=True)
)
# self.nn2 = nn.Sequential(
# nn.Dropout(dropout),
# nn.Linear(total_feat_len, hidden_len, bias=True),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Dropout(dropout),
# nn.Linear(hidden_len, num_classes, bias=True)
# )
print(f'aggregate feat ensemble, input feat len {total_feat_len}, hidden size {hidden_len}')
def forward(self, inputs, return_feats=False, return_preds=False):
assert len(self.components) == len(inputs)
preds, feats = [], []
for model, input in zip(self.components, inputs):
pred, feat = model(input, return_feat=True)
preds.append(pred)
feats.append(feat)
# hidden_feat = self.nn(torch.cat(feats, dim=1))
# pred = self.nn2(hidden_feat)
pred = self.nn(torch.cat(feats, dim=1))
out = [pred]
if return_feats:
out.append(feats)
if return_preds:
out.append(preds)
if len(out) == 1:
return out[0]
else:
return out
# def forward(self, feats):
# return self.nn(feats)
class EnsembleClassifier(nn.Module):
FEAT_LEN = 768
def __init__(self, raw_bert, styleClassifier, charClassifier, bertClassifier, finalClassifier):
super().__init__()
self.bert = raw_bert
self.styleClassifier = styleClassifier
self.charClassifier = charClassifier
self.bertClassifier = bertClassifier
self.finalClassifier = finalClassifier
def forward(self, x, return_feat=False):
# x is a tokenized input
# print("ENS Forward")
stylePred = self.styleClassifier(x[0])
charPred = self.charClassifier(x[1])
bertFeature = self.bert(x[2], x[3]).last_hidden_state.flatten(1)
bertPred = self.bertClassifier(bertFeature)
# print(stylePred.shape)
# print(charPred.shape)
# print(bertFeature.shape)
# print(bertPred.shape)
# print(x[0].shape)
# print(x[1].shape)
ensembleTensor = torch.cat((stylePred, charPred, bertPred, x[0], x[1], bertFeature), dim=1)
# out = self.fc(feature.pooler_output.flatten(1))
out = self.finalClassifier(ensembleTensor)
if return_feat:
return out, bertFeature
return out