-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtshae_models.py
334 lines (281 loc) · 11.8 KB
/
tshae_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import torch
import torch.nn as nn
import numpy as np
from hydra.utils import instantiate
class Encoder(nn.Module):
def __init__(self, input_size, hidden_size, latent_dim, dropout_lstm, dropout=0, num_layers=1, bidirectional=True):
super(Encoder, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.latent_dim = latent_dim
self.num_layers = num_layers
self.bidirectional = bidirectional
self.num_directions = 2 if self.bidirectional else 1
self.p_lstm = dropout_lstm
self.p = dropout
self.lstm = nn.LSTM(
input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
dropout=self.p_lstm,
batch_first=True,
bidirectional=self.bidirectional
)
self.fc_mean = nn.Sequential(
nn.Dropout(self.p),
nn.Linear(
in_features=self.num_directions * hidden_size,
out_features=latent_dim)
)
self.fc_log_var = nn.Sequential(
nn.Dropout(self.p),
nn.Linear(
in_features=self.num_directions * hidden_size,
out_features=latent_dim)
)
def reparameterization(self, mean, var):
epsilon = torch.randn_like(var).to(var.device)
z = mean + var * epsilon
return z
def forward(self, x):
batch_size = x.shape[0]
_, (h_n, _) = self.lstm(x)
# h_n of shape (num_layers * num_directions, batch, hidden_size)
"""
hidden.shape = (num_layers*num_directions, batch, hidden_size)
layers can be separated using h_n.view(num_layers, num_directions, batch, hidden_size)
So you shouldn’t simply do hidden[-1] but first do a view() to separate the num_layers and num_directions (1 or 2). If you do
hidden = hidden.view(num_layers, 2, batch, hidden_size) # 2 for bidirectional
last_hidden = hidden[-1]
then last_hidden.shape = (2, batch, hidden_size) and you can do
last_hidden_fwd = last_hidden[0]
last_hidden_bwd = last_hidden[1]
TODO: check if it same as
# Pass the input through the LSTM
output, (h_n, c_n) = lstm(input_data, (h0, c0))
Extract the last forward and backward outputs
last_forward_output = output[:, -1, :hidden_size]
last_backward_output = output[:, 0, hidden_size:]
"""
h_n = h_n.view(self.num_layers, self.num_directions, batch_size, self.hidden_size)
if self.bidirectional:
h = torch.cat((h_n[-1, -2, :, :], h_n[-1, -1, :, :]), dim=1)
else:
h = h_n[-1, -1, :, :]
mean = self.fc_mean(h)
log_var = self.fc_log_var(h)
z = self.reparameterization(mean, torch.exp(0.5 * log_var)) # takes exponential function (log var -> var)
return z, mean, log_var
class Decoder(nn.Module):
def __init__(self,
input_size,
hidden_size,
latent_dim,
window_size,
dropout_lstm,
dropout_layer,
num_layers=1,
bidirectional=True
):
super(Decoder, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.latent_dim = latent_dim
self.num_layers = num_layers
self.bidirectional = bidirectional
self.window_size = window_size
self.p_lstm = dropout_lstm
self.p_dropout_layer = dropout_layer
self.num_directions = 2 if self.bidirectional else 1
self.lstm_to_hidden = nn.LSTM(
input_size=latent_dim,
hidden_size=hidden_size,
num_layers=num_layers,
dropout=self.p_lstm,
batch_first=True,
bidirectional=self.bidirectional
)
self.dropout_layer = nn.Dropout(self.p_dropout_layer)
self.lstm_to_output = nn.LSTM(
input_size=self.num_directions * hidden_size,
hidden_size=input_size,
batch_first=True
)
def forward(self, z):
latent_z = z.unsqueeze(1).repeat(1, self.window_size, 1)
out, _ = self.lstm_to_hidden(latent_z)
out = self.dropout_layer(out)
out, _ = self.lstm_to_output(out)
return out
class TSHAE(nn.Module):
def __init__(self, encoder, decoder=None, reconstruct=False, dropout_regressor=0, regression_dims=200):
super(TSHAE, self).__init__()
self.decode_mode = reconstruct
if self.decode_mode:
assert isinstance(decoder, nn.Module), "You should to pass a valid decoder"
self.decoder = decoder
self.encoder = encoder
self.p = dropout_regressor
self.regression_dims = regression_dims
self.regressor = nn.Sequential(
nn.Linear(self.encoder.latent_dim, self.regression_dims),
nn.Tanh(),
nn.Dropout(self.p),
nn.Linear(self.regression_dims, 1)
)
def forward(self, x):
z, mean, log_var = self.encoder(x)
y_hat = self.regressor(z)
if self.decode_mode:
x_hat = self.decoder(z)
return y_hat, z, mean, log_var, x_hat
return y_hat, z, mean, log_var
class RVEAttention_MH(nn.Module):
"""
RVE Model with MultiHead attention.
"""
def __init__(self, encoder, attention_embed_dim, batchnorm_dim, batchnorm_affine, attention_num_heads, attention_dropout, decoder=None,
reconstruct=False, dropout_regressor=0, regression_dims=200):
super(RVEAttention_MH, self).__init__()
self.decode_mode = reconstruct
if self.decode_mode:
assert isinstance(decoder, nn.Module), "You should to pass a valid decoder"
self.decoder = decoder
self.encoder = encoder
self.p = dropout_regressor
self.regression_dims = regression_dims
self.self_attention = nn.MultiheadAttention(embed_dim=attention_embed_dim, num_heads=attention_num_heads,
dropout=attention_dropout, batch_first=True)
self.batchnorm = nn.BatchNorm1d(batchnorm_dim, affine=batchnorm_affine)
self.regressor = nn.Sequential(
nn.Linear(self.encoder.latent_dim, self.regression_dims),
nn.Tanh(),
nn.Dropout(self.p),
nn.Linear(self.regression_dims, 1)
)
def forward(self, x):
"""
self attention input_size dims: N, L, E,
where N batch size
L is the target sequence length,
E is the query embedding dimension embed_dim
x input_size: N, L, F,
where N batch size
L is window size,
E number of sensors
For feature-wise attention x should be transposed: (N, E, L)
"""
x = torch.permute(x, (0, 2, 1))
x_attn, _ = self.self_attention(x, x, x)
x = x + x_attn
x = self.batchnorm(x)
x = torch.permute(x, (0, 2, 1))
z, mean, log_var = self.encoder(x)
y_hat = self.regressor(z)
if self.decode_mode:
x_hat = self.decoder(z)
return y_hat, z, mean, log_var, x_hat
return y_hat, z, mean, log_var
class MultiplicativeAttention(nn.Module):
def __init__(self, values_embedding, queries_embedding):
super().__init__()
self.values_embedding = values_embedding
self.queries_embedding = queries_embedding
self.W = torch.nn.Parameter(torch.FloatTensor(
self.queries_embedding, self.values_embedding).uniform_(-0.1, 0.1), requires_grad=True)
def forward(self,
query, # [Batch size, N queries, queries_dim]
values # [Batch size, N values, values_dim]
):
weights = query @ self.W @ values.permute(0, 2, 1) # [Batch size, N queries, N values]
weights /= np.sqrt(self.queries_embedding)
out = weights @ values # [Batch size, N queries, values_dim]
return out
class RVEAttention_MP(nn.Module):
"""
RVE Model with Multiplicative attention.
"""
def __init__(self, encoder, attention_values_embedding, batchnorm_dim, batchnorm_affine, attention_queries_embedding, decoder=None,
reconstruct=False, dropout_regressor=0, regression_dims=200):
super(RVEAttention_MP, self).__init__()
self.decode_mode = reconstruct
if self.decode_mode:
assert isinstance(decoder, nn.Module), "You should to pass a valid decoder"
self.decoder = decoder
self.encoder = encoder
self.p = dropout_regressor
self.regression_dims = regression_dims
self.self_attention = MultiplicativeAttention(values_embedding=attention_values_embedding, queries_embedding=attention_queries_embedding)
self.batchnorm = nn.BatchNorm1d(batchnorm_dim, affine=batchnorm_affine)
self.regressor = nn.Sequential(
nn.Linear(self.encoder.latent_dim, self.regression_dims),
nn.Tanh(),
nn.Dropout(self.p),
nn.Linear(self.regression_dims, 1)
)
def forward(self, x):
"""
self attention input_size dims: N, L, E,
where N batch size
L is the target sequence length,
E is the query embedding dimension embed_dim
x input_size: N, L, F,
where N batch size
L is window size,
E number of sensors
For feature-wise attention x should be transposed: (N, E, L)
"""
x = torch.permute(x, (0, 2, 1))
x = self.self_attention(x, x)
x = self.batchnorm(x)
x = torch.permute(x, (0, 2, 1))
z, mean, log_var = self.encoder(x)
y_hat = self.regressor(z)
if self.decode_mode:
x_hat = self.decoder(z)
return y_hat, z, mean, log_var, x_hat
return y_hat, z, mean, log_var
class OriginalEncoder(nn.Module):
def __init__(self, input_dim, intermediate_dim, latent_dim):
super(OriginalEncoder, self).__init__()
self.hidden_size = intermediate_dim
self.lstm = nn.LSTM(input_size=input_dim, hidden_size=intermediate_dim, bidirectional=True, batch_first=True)
self.mu = nn.Linear(in_features=2*intermediate_dim, out_features=latent_dim)
self.sigma = nn.Linear(in_features=2*intermediate_dim, out_features=latent_dim)
def reparameterization(self, mean, log_var):
epsilon = torch.randn_like(log_var).to(log_var.device)
z = mean + torch.exp(0.5 * log_var) * epsilon
return z
def forward(self, x):
batch_size = x.shape[0]
output, (h_n, c_n) = self.lstm(x)
# Extract the last forward and backward outputs
last_forward_output = output[:, -1, :self.hidden_size]
last_backward_output = output[:, 0, self.hidden_size:]
# Concatenate the last forward and backward outputs
concatenated_output = torch.cat((last_forward_output, last_backward_output), dim=1)
mean = self.mu(concatenated_output)
log_var = self.sigma(concatenated_output)
z = self.reparameterization(mean, log_var)
return z, mean, log_var
class OriginalDecoder(nn.Module):
def __init__(self):
super(OriginalDecoder, self).__init__()
'''
Dummy module
'''
pass
class OriginalRVE(nn.Module):
def __init__(self, encoder, latent_dim, decoder=None):
super(OriginalRVE, self).__init__()
self.decode_mode = False
self.encoder = encoder
self.regressor = nn.Sequential(
nn.Linear(in_features=latent_dim, out_features=200),
nn.Tanh(),
nn.Linear(in_features=200, out_features=1)
)
def forward(self, x):
z, mean, log_var = self.encoder(x)
y_hat = self.regressor(z)
return y_hat, z, mean, log_var, x