-
Notifications
You must be signed in to change notification settings - Fork 1
/
neural_network_classes.py
400 lines (336 loc) · 15.8 KB
/
neural_network_classes.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
This file contains custom neural network class objects that can be called to
initialize a model or pass input information through the model to return an
output prediction.
The purpose of these class objects is to contain model information and
functionality to be passed to different training and evaluation scripts.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F # relu, tanh, etc.
from torch.autograd import Variable
# %% LSTM
class LSTM(nn.Module):
def __init__(self, num_outputs, input_size, hidden_size, num_layers, seq_length, device, dropout=0.1):
super(LSTM, self).__init__()
self.num_outputs = num_outputs # model output size
self.num_layers = num_layers # number of lstm layers
self.input_size = input_size # model input size
self.hidden_size = hidden_size # number of hidden units in each lstm layer
self.seq_length = seq_length # rolling lookback window length
self.device = device # training device (i.e., 'cpu' or 'cuda')
# Define network layer types
self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,
num_layers=num_layers, batch_first=True, dropout=dropout)
self.fc = nn.Linear(hidden_size, num_outputs)
# Define activation function
self.relu = nn.ReLU()
def forward(self, x, y_hist, h_0, c_0):
# Propagate input through LSTM
self.lstm.flatten_parameters()
# combine x and y history tensors
x = torch.cat((x, y_hist), dim=-1)
output, (hn, cn) = self.lstm(x, (h_0, c_0)) # lstm with input, hidden, and internal state
out = self.relu(output[:,-1,:])
out = self.fc(out)
return out, hn, cn
def init_hidden_internal(self, batch_size):
h_0 = Variable(torch.zeros(
self.num_layers, batch_size, self.hidden_size)).to(self.device)
c_0 = Variable(torch.zeros(
self.num_layers, batch_size, self.hidden_size)).to(self.device)
return h_0, c_0
# %% Dual-Stage Attention-Based LSTM
class InputAttentionEncoder(nn.Module):
def __init__(self, N, M, T, device, stateful=False):
"""
:param: N: int
number of time serieses
:param: M:
number of LSTM units
:param: T:
number of timesteps
:param: stateful:
decides whether to initialize cell state of new time window with values of the last cell state
of previous time window or to initialize it with zeros
"""
super(self.__class__, self).__init__()
self.N = N
self.M = M
self.T = T
self.device = device
self.encoder_lstm = nn.LSTMCell(input_size=self.N, hidden_size=self.M)
#equation 8 matrices
self.W_e = nn.Linear(2*self.M, self.T)
self.U_e = nn.Linear(self.T, self.T, bias=False)
self.v_e = nn.Linear(self.T, 1, bias=False)
def forward(self, inputs):
encoded_inputs = torch.zeros((inputs.size(0), self.T, self.M)).to(self.device)
#initiale hidden states
h_tm1 = torch.zeros((inputs.size(0), self.M)).to(self.device)
s_tm1 = torch.zeros((inputs.size(0), self.M)).to(self.device)
for t in range(self.T):
#concatenate hidden states
h_c_concat = torch.cat((h_tm1, s_tm1), dim=1)
#attention weights for each k in N (equation 8)
x = self.W_e(h_c_concat).unsqueeze_(1).repeat(1, self.N, 1)
y = self.U_e(inputs.permute(0, 2, 1))
z = torch.tanh(x + y)
e_k_t = torch.squeeze(self.v_e(z))
#normalize attention weights (equation 9)
# CHANGED
if len(e_k_t.size()) > 2:
alpha_k_t = F.softmax(e_k_t, dim=1)
else:
alpha_k_t = F.softmax(e_k_t, dim=-1)
# alpha_k_t = F.softmax(e_k_t, dim=1)
#weight inputs (equation 10)
weighted_inputs = alpha_k_t * inputs[:, t, :]
#calculate next hidden states (equation 11)
h_tm1, s_tm1 = self.encoder_lstm(weighted_inputs, (h_tm1, s_tm1))
encoded_inputs[:, t, :] = h_tm1
return encoded_inputs, alpha_k_t
class TemporalAttentionDecoder(nn.Module):
def __init__(self, M, P, T, device, stateful=False):
"""
:param: M: int
number of encoder LSTM units
:param: P:
number of deocder LSTM units
:param: T:
number of timesteps
:param: stateful:
decides whether to initialize cell state of new time window with values of the last cell state
of previous time window or to initialize it with zeros
"""
super(self.__class__, self).__init__()
self.M = M
self.P = P
self.T = T
self.device = device
self.stateful = stateful
self.decoder_lstm = nn.LSTMCell(input_size=1, hidden_size=self.P)
#equation 12 matrices
self.W_d = nn.Linear(2*self.P, self.M)
self.U_d = nn.Linear(self.M, self.M, bias=False)
self.v_d = nn.Linear(self.M, 1, bias = False)
#equation 15 matrix
self.w_tilda = nn.Linear(self.M + 1, 1)
#equation 22 matrices
self.W_y = nn.Linear(self.P + self.M, self.P)
self.v_y = nn.Linear(self.P, 1)
def forward(self, encoded_inputs, y):
#initializing hidden states
d_tm1 = torch.zeros((encoded_inputs.size(0), self.P)).to(self.device)
s_prime_tm1 = torch.zeros((encoded_inputs.size(0), self.P)).to(self.device)
for t in range(self.T):
#concatenate hidden states
d_s_prime_concat = torch.cat((d_tm1, s_prime_tm1), dim=1)
#print(d_s_prime_concat)
#temporal attention weights (equation 12)
x1 = self.W_d(d_s_prime_concat).unsqueeze_(1).repeat(1, encoded_inputs.shape[1], 1)
y1 = self.U_d(encoded_inputs)
z1 = torch.tanh(x1 + y1)
l_i_t = self.v_d(z1)
#normalized attention weights (equation 13)
beta_i_t = F.softmax(l_i_t, dim=1)
#create context vector (equation_14)
c_t = torch.sum(beta_i_t * encoded_inputs, dim=1)
#concatenate c_t and y_t
y_c_concat = torch.cat((c_t, y[:, t, :]), dim=1)
#create y_tilda
y_tilda_t = self.w_tilda(y_c_concat)
#calculate next hidden states (equation 16)
d_tm1, s_prime_tm1 = self.decoder_lstm(y_tilda_t, (d_tm1, s_prime_tm1))
#concatenate context vector at step T and hidden state at step T
d_c_concat = torch.cat((d_tm1, c_t), dim=1)
#calculate output
y_Tp1 = self.v_y(self.W_y(d_c_concat))
return y_Tp1, beta_i_t
class DARNN(nn.Module):
def __init__(self, N, M, P, T, device, stateful_encoder=False, stateful_decoder=False):
"""
:param: N: int
number of time series
:param: M: int
number of encoder LSTM units
:param: P:
number of deocder LSTM units
:param: T:
number of timesteps
:param: stateful_encoder & stateful_decoder:
decides whether to initialize cell state of new time window with
values of the last cell state of previous time window or to
initialize it with zeros
"""
super(self.__class__, self).__init__()
self.encoder = InputAttentionEncoder(N, M, T, device, stateful_encoder).to(device)
self.decoder = TemporalAttentionDecoder(M, P, T, device, stateful_decoder).to(device)
def forward(self, X_history, y_history):
encoder_out, alpha = self.encoder(X_history)
out, beta = self.decoder(encoder_out, y_history)
return out, alpha, beta
# %% Hierarchical-Attention-Based-Recurrent-Highway-Network
class HSGLayer(nn.Module):
def __init__(self, n_units, init_gates_closed, device):
super(HSGLayer, self).__init__()
self.W_R = nn.Linear(n_units, n_units, bias=False)
self.W_F = nn.Linear(n_units, n_units)
if init_gates_closed:
self.W_F.bias = nn.Parameter(torch.Tensor([-2.5]*n_units).to(device))
def forward(self, s_L_t, s_prime_tm1):
g = torch.sigmoid(self.W_R(s_prime_tm1) + self.W_F(s_L_t))
s_prime_t = g*s_prime_tm1 + (1 - g)*s_L_t
return s_prime_t
class RHNCell(nn.Module):
def __init__(self, in_feats, n_units, rec_depth=3, couple_gates=True,
use_HSG=False, init_gates_closed=False, device='cpu'):
super(RHNCell, self).__init__()
self.rec_depth = rec_depth
self.in_feats = in_feats
self.n_units = n_units
self.couple_gates = couple_gates
self.use_HSG = use_HSG
self.W_H = nn.Linear(in_feats, n_units, bias=False)
self.W_T = nn.Linear(in_feats, n_units, bias=False)
if not couple_gates:
self.W_C = nn.Linear(in_feats, n_units, bias=False)
self.R_H = nn.ModuleList([nn.Linear(n_units, n_units) for _ in range(rec_depth)])
self.R_T = nn.ModuleList([nn.Linear(n_units, n_units) for _ in range(rec_depth)])
if not couple_gates:
self.R_C = nn.ModuleList([nn.Linear(n_units, n_units) for _ in range(rec_depth)])
if use_HSG:
self.HSG = HSGLayer(n_units, init_gates_closed, device)
if init_gates_closed:
for l in range(rec_depth):
self.R_T[l].bias = nn.Parameter(torch.Tensor([-2.5]*n_units).to(device))
if not couple_gates:
self.R_C[l].bias = nn.Parameter(torch.Tensor([-2.5]*n_units).to(device))
def forward(self, x, s):
if self.use_HSG:
s_prime_tm1 = s
preds = []
for l in range(self.rec_depth):
if l == 0:
h_l_t = torch.tanh(self.W_H(x) + self.R_H[l](s))
t_l_t = torch.sigmoid(self.W_T(x) + self.R_T[l](s))
if not self.couple_gates:
c_l_t = torch.sigmoid(self.W_C(x) + self.R_C[l](s))
else:
h_l_t = torch.tanh(self.R_H[l](s))
t_l_t = torch.sigmoid(self.R_T[l](s))
if not self.couple_gates:
c_l_t = torch.sigmoid(self.R_C[l](s))
if not self.couple_gates:
s = h_l_t*t_l_t + c_l_t*s
else:
s = h_l_t*t_l_t + (1 - t_l_t)*s
preds.append(s)
if self.use_HSG:
s = self.HSG(s, s_prime_tm1)
preds.pop()
preds.append(s)
preds = torch.stack(preds)
return s, preds
class RHN(nn.Module):
def __init__(self, in_feats, out_feats, n_units=32, rec_depth=3, couple_gates=True, use_HSG=False,
init_gates_closed=False, use_batch_norm=False, device='cpu'):
super(RHN, self).__init__()
assert rec_depth > 0
self.rec_depth = rec_depth
self.in_feats = in_feats
self.n_units = n_units
self.init_gates_closed = init_gates_closed
self.couple_gates = couple_gates
self.use_HSG = use_HSG
self.use_batch_norm = use_batch_norm
self.device = device
self.RHNCell = RHNCell(in_feats, n_units, rec_depth, couple_gates=couple_gates,
use_HSG=use_HSG, init_gates_closed=init_gates_closed,
device=self.device)
if use_batch_norm:
self.bn_x = nn.BatchNorm1d(in_feats)
self.bn_s = nn.BatchNorm1d(n_units)
def forward(self, x):
s = torch.zeros(x.shape[0], self.n_units).to(self.device)
preds = []
highway_states = []
for t in range(x.shape[1]):
if self.use_batch_norm:
x_inp = self.bn_x(x[:, t, :])
s = self.bn_s(s)
else:
x_inp = x[:, t, :]
s, all_s = self.RHNCell(x_inp, s)
preds.append(s)
highway_states.append(all_s)
preds = torch.stack(preds)
preds = preds.permute(1, 0, 2)
highway_states = torch.stack(highway_states)
highway_states = highway_states.permute(2, 0, 3, 1)
out = preds
return out, highway_states
class ConvBlock(nn.Module):
def __init__(self, T, in_channels, n_filters=32, filter_size=5):
super(ConvBlock, self).__init__()
padding1 = self._calc_padding(T, filter_size)
# Conv
self.conv = nn.Conv1d(in_channels, n_filters, filter_size, padding=padding1)
self.relu = nn.ReLU()
self.maxpool = nn.AdaptiveMaxPool1d(T, return_indices=True)
self.zp = nn.ConstantPad1d((1, 0), 0)
def _calc_padding(self, Lin, kernel, stride=1, dilation=1):
p = int(((Lin - 1) * stride + 1 + dilation * (kernel - 1) - Lin) / 2)
return p
def forward(self, x):
x = x.permute(0, 2, 1)
x = self.conv(x)
x = self.relu(x)
x, indices = self.maxpool(x)
x = x.permute(0, 2, 1)
return x
class HARHN(nn.Module):
def __init__(self, n_conv_layers, T, in_feats, target_feats, n_units_enc=32, n_units_dec=32, enc_input_size=32, rec_depth=3,
out_feats=1, n_filters=32, filter_size=5, device='cpu'):
super(HARHN, self).__init__()
assert n_conv_layers > 0
self.n_convs = n_conv_layers
self.n_units_enc = n_units_enc
self.n_units_dec = n_units_dec
self.rec_depth = rec_depth
self.T = T
self.device = device
self.convs = nn.ModuleList([ConvBlock(T, in_feats, n_filters=n_filters, filter_size=filter_size) if i == 0 else ConvBlock(T, n_filters, n_filters=n_filters, filter_size=filter_size) for i in range(n_conv_layers)])
self.conv_to_enc = nn.Linear(n_filters, enc_input_size)
self.RHNEncoder = RHN(enc_input_size, out_feats=n_units_enc, n_units=n_units_enc, rec_depth=rec_depth, device=self.device)
self.RHNDecoder = RHNCell(target_feats, n_units_dec, rec_depth=rec_depth, device=self.device)
self.T_k = nn.ModuleList([nn.Linear(n_units_dec, n_units_enc, bias=False) for i in range(self.rec_depth)])
self.U_k = nn.ModuleList([nn.Linear(n_units_enc, n_units_enc) for i in range(self.rec_depth)])
self.v_k = nn.ModuleList([nn.Linear(n_units_enc, 1) for i in range(self.rec_depth)])
self.W_tilda = nn.Linear(target_feats, target_feats, bias=False)
self.V_tilda = nn.Linear(rec_depth*n_units_enc, target_feats)
self.W = nn.Linear(n_units_dec, target_feats)
self.V = nn.Linear(rec_depth*n_units_enc, target_feats)
def forward(self, x, y):
for l in range(self.n_convs):
x = self.convs[l](x)
x = self.conv_to_enc(x)
x, h_T_L = self.RHNEncoder(x) # h_T_L.shape = (batch_size, T, n_units_enc, rec_depth)
s = torch.zeros(x.shape[0], self.n_units_dec).to(self.device)
for t in range(self.T):
s_rep = s.unsqueeze(1)
s_rep = s_rep.repeat(1, self.T, 1)
d_t = []
for k in range(self.rec_depth):
h_T_k = h_T_L[..., k]
a = self.U_k[k](h_T_k)
b = self.T_k[k](s_rep)
e_t_k = self.v_k[k](torch.tanh(b + a))
alpha_t_k = torch.softmax(e_t_k, 1)
d_t_k = torch.sum(h_T_k*alpha_t_k, dim=1)
d_t.append(d_t_k)
d_t = torch.cat(d_t, dim=1)
y_tilda_t = self.W_tilda(y[:, t, :]) + self.V_tilda(d_t)
s, _ = self.RHNDecoder(y_tilda_t, s)
y_T = self.W(s) + self.V(d_t)
return y_T