-
Notifications
You must be signed in to change notification settings - Fork 15
/
model.py
213 lines (184 loc) · 7.11 KB
/
model.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
#! /usr/bin/env python
import torch
import torch.nn.functional as F
from torch import nn
class SimpleEncClassifier(nn.Module):
def __init__(self, enc_dims, mlp_dims, dropout=0.2, verbose=1):
super().__init__()
self.enc_dims = enc_dims
self.mlp_dims = mlp_dims
self.encoder_model = None
self.mlp_model = None
self.encoded = None
self.mlp_out = None
self.encoder_modules = []
self.mlp_modules = []
self.verbose = verbose
# encoder
n_stacks = len(self.enc_dims) - 1
# internal layers in encoder
for i in range(n_stacks - 1):
self.encoder_modules.append(nn.Linear(self.enc_dims[i], self.enc_dims[i + 1]))
self.encoder_modules.append(nn.ReLU())
# encoded features layer. no activation.
self.encoder_modules.append(nn.Linear(self.enc_dims[-2], self.enc_dims[-1]))
# encoder model
self.encoder_model = nn.Sequential(*(self.encoder_modules))
# MLP
m_stacks = len(self.mlp_dims) - 1
for i in range(m_stacks - 1):
self.mlp_modules.append(nn.Linear(self.mlp_dims[i], self.mlp_dims[i + 1]))
self.mlp_modules.append(nn.ReLU())
if dropout > 0:
self.mlp_modules.append(nn.Dropout(p=dropout))
# mlp output
self.mlp_modules.append(nn.Linear(self.mlp_dims[-2], self.mlp_dims[-1]))
self.mlp_modules.append(nn.Softmax(dim=1))
self.mlp_model = nn.Sequential(*(self.mlp_modules))
if self.verbose:
print(self.encoder_model)
print(self.mlp_model)
return
def update_mlp_head(self, dropout=0.2):
self.mlp_out = None
self.mlp_modules = []
# MLP
m_stacks = len(self.mlp_dims) - 1
for i in range(m_stacks - 1):
self.mlp_modules.append(nn.Linear(self.mlp_dims[i], self.mlp_dims[i + 1]))
self.mlp_modules.append(nn.ReLU())
if dropout > 0:
self.mlp_modules.append(nn.Dropout(p=dropout))
# mlp output
self.mlp_modules.append(nn.Linear(self.mlp_dims[-2], self.mlp_dims[-1]))
self.mlp_modules.append(nn.Softmax(dim=1))
self.mlp_model = nn.Sequential(*(self.mlp_modules))
if self.verbose:
print(self.encoder_model)
print(self.mlp_model)
return
def forward(self, x):
self.encoded = self.encoder_model(x)
self.out = self.mlp_model(self.encoded)
return self.encoded, self.encoded, self.out
def predict_proba(self, x):
_, _, mlp_out = self.forward(x)
return mlp_out
def predict(self, x):
self.encoded = self.encoder_model(x)
self.out = self.mlp_model(self.encoded)
preds = self.out.max(1)[1]
return preds
def encode(self, x):
self.encoded = self.encoder_model(x)
return self.encoded
class Enc(nn.Module):
def __init__(self, enc_dims, verbose=1):
super().__init__()
self.enc_dims = enc_dims
self.encoder_model = None
self.encoded = None
self.encoder_modules = []
self.verbose = verbose
# encoder
n_stacks = len(self.enc_dims) - 1
# internal layers in encoder
for i in range(n_stacks - 1):
self.encoder_modules.append(nn.Linear(self.enc_dims[i], self.enc_dims[i + 1]))
self.encoder_modules.append(nn.ReLU())
# encoded features layer. no activation.
self.encoder_modules.append(nn.Linear(self.enc_dims[-2], self.enc_dims[-1]))
# encoder model
self.encoder_model = nn.Sequential(*(self.encoder_modules))
if self.verbose:
print(self.encoder_model)
return
def forward(self, x):
self.encoded = self.encoder_model(x)
return self.encoded
def encode(self, x):
self.encoded = self.encoder_model(x)
return self.encoded
class CAE(nn.Module):
def __init__(self, enc_dims, verbose=1):
super().__init__()
self.enc_dims = enc_dims
self.encoder_model = None
self.decoder_model = None
self.encoded = None
self.decoded = None
self.encoder_modules = []
self.decoder_modules = []
self.verbose = verbose
# encoder
n_stacks = len(self.enc_dims) - 1
# internal layers in encoder
for i in range(n_stacks - 1):
self.encoder_modules.append(nn.Linear(self.enc_dims[i], self.enc_dims[i + 1]))
self.encoder_modules.append(nn.ReLU())
# encoded features layer. no activation.
self.encoder_modules.append(nn.Linear(self.enc_dims[-2], self.enc_dims[-1]))
# encoder model
self.encoder_model = nn.Sequential(*(self.encoder_modules))
self.encoder_model.apply(self.init_weights)
# decoder
# internal layers in decoder
for i in range(n_stacks - 1, 0, -1):
self.decoder_modules.append(nn.Linear(self.enc_dims[i + 1], self.enc_dims[i]))
self.decoder_modules.append(nn.ReLU())
# decoded output. no activation.
self.decoder_modules.append(nn.Linear(self.enc_dims[1], self.enc_dims[0]))
# decoder model
self.decoder_model = nn.Sequential(*(self.decoder_modules))
self.decoder_model.apply(self.init_weights)
if self.verbose:
print(self.encoder_model)
print(self.decoder_model)
return
def init_weights(self, m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
return
def forward(self, x):
self.encoded = self.encoder_model(x)
self.decoded = self.decoder_model(self.encoded)
return self.encoded, self.decoded
def encode(self, x):
self.encoded = self.encoder_model(x)
return self.encoded
class MLPClassifier(nn.Module):
def __init__(self, mlp_dims, dropout=0.2, verbose=1):
super().__init__()
self.mlp_dims = mlp_dims
self.mlp_model = None
self.mlp_out = None
self.mlp_modules = []
self.verbose = verbose
# MLP
m_stacks = len(self.mlp_dims) - 1
for i in range(m_stacks - 1):
self.mlp_modules.append(nn.Linear(self.mlp_dims[i], self.mlp_dims[i + 1]))
self.mlp_modules.append(nn.ReLU())
if dropout > 0:
self.mlp_modules.append(nn.Dropout(p=dropout))
# mlp output
self.mlp_modules.append(nn.Linear(self.mlp_dims[-2], self.mlp_dims[-1]))
self.mlp_modules.append(nn.Softmax(dim=1))
self.mlp_model = nn.Sequential(*(self.mlp_modules))
if self.verbose:
print(self.mlp_model)
return
def forward(self, x):
self.mlp_out = self.mlp_model(x)
return self.mlp_out
def predict_proba(self, x):
mlp_out = self.forward(x)
return mlp_out
def predict(self, x):
self.mlp_out = self.mlp_model(x)
preds = self.mlp_out.max(1)[1]
return preds
def encode(self, x):
self.encoded = self.mlp_model[:-2](x)
return self.encoded