-
Notifications
You must be signed in to change notification settings - Fork 2
/
lstm_multivariate.py
169 lines (135 loc) · 5.27 KB
/
lstm_multivariate.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
import numpy as np
import torch.nn as nn
import torch
import torch.optim as optim
import torch.utils.data as Data
import more_itertools
def se2rmse(a):
return torch.sqrt(sum(a.t())/a.shape[1])
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# hyper params
feature_size = None # will be set based on the input data
hidden_len = 64
batch_size = 256
num_layer = 1
lr = 1e-3
weight_decay = 1e-5
epoches = 500
seq_len = 5
class LSTM_multivariate(nn.Module):
def __init__(self):
super(LSTM_multivariate, self).__init__()
self.rnn = nn.LSTM(
input_size=feature_size,
hidden_size=hidden_len, # rnn hidden unit
num_layers=num_layer, # number of rnn layer
batch_first=True, # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
)
self.out = nn.Linear(hidden_len, feature_size)
def forward(self, x):
# x shape (batch, time_step, input_size)
# r_out shape (batch, time_step, output_size)
# h_n shape (n_layers, batch, hidden_size)
# h_c shape (n_layers, batch, hidden_size)
r_out, (h_n, h_c) = self.rnn(x, None) # None represents zero initial hidden state
# choose r_out at the last time step
out = self.out(r_out[:, -1, :])
return out
def get_loss_function():
# MSE
return nn.MSELoss()
# CE
# return nn.CrossEntropyLoss()
def train(X_train, y_train):
global feature_size
feature_size = y_train.shape[1]
model = LSTM_multivariate().to(device)
optimizier = optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
criterion = get_loss_function()
getMSEvec = nn.MSELoss(reduction='none')
model.train()
# X_train = more_itertools.windowed(train_data,n=seq_len,step=1)
# X_train = np.asarray(list(X_train))
# y_train = np.asarray(train_data[seq_len-1:])
print("SHAPE",X_train.shape,y_train.shape)
X_train = torch.from_numpy(X_train).type(torch.float).to(device)
y_train = torch.from_numpy(y_train).type(torch.float).to(device)
torch_dataset = Data.TensorDataset(X_train, y_train)
loader = Data.DataLoader(
dataset=torch_dataset,
batch_size=batch_size,
shuffle=True,
)
for epoch in range(epoches):
for step, (batch_x, batch_y) in enumerate(loader):
output = model(batch_x)
loss = criterion(output, batch_y)
optimizier.zero_grad()
loss.backward()
optimizier.step()
# if epoch % 10 == 0 :
# print('epoch:{}/{}'.format(epoch,step), '|Loss:', loss.item())
if epoch % 10 == 0 :
print('epoch:{}'.format(epoch), '|Loss:', loss.item())
model.eval()
output = model(X_train)
with torch.no_grad():
mse_vec = torch.mean((output - y_train) ** 2, dim=1)
mse_vec_unsort = mse_vec.numpy()
print("max AD score",max(mse_vec))
thres = max(mse_vec)
mse_vec.sort()
pctg = 0.99
thres = mse_vec[int(len(mse_vec)*pctg)] + 0.001 # hack
print("thres:",thres)
return model, thres, mse_vec_unsort
# mse_vec = getMSEvec(output,y_train)
# rmse_vec = se2rmse(mse_vec).cpu().data.numpy()
# rmse_vec_unsort = rmse_vec.copy()
# print("max AD score",max(rmse_vec))
# thres = max(rmse_vec)
# rmse_vec.sort()
# pctg = 0.99
# thres = rmse_vec[int(len(rmse_vec)*pctg)]
# print("thres:",thres)
# return model, thres, rmse_vec_unsort
# @torch.no_grad()
def test(model, thres, X_test, y_test):
global feature_size
feature_size = y_test.shape[1]
getMSEvec = nn.MSELoss(reduction='none')
model.eval()
# X_test = more_itertools.windowed(test_data,n=seq_len,step=1)
# X_test = np.asarray(list(X_test))
# y_test = np.asarray(test_data[seq_len-1:])
# X_test = more_itertools.windowed(test_data,n=seq_len,step=1)
# X_test = np.asarray(list(X_test)[:-1])
# y_test = np.asarray(test_data[seq_len:])
X_test = torch.from_numpy(X_test).type(torch.float).to(device)
y_test = torch.from_numpy(y_test).type(torch.float).to(device)
with torch.no_grad():
output = model(X_test)
# mse_vec = getMSEvec(output,y_test)
# rmse_vec = se2rmse(mse_vec).cpu().data.numpy()
mse_vec = torch.mean((output - y_test) ** 2, dim=1)
# rmse_vec = np.concatenate((np.asarray([0.]*(seq_len-1)),rmse_vec))
# idx_mal = np.where(rmse_vec>thres)
# idx_ben = np.where(rmse_vec<=thres)
# print(len(rmse_vec[idx_ben]),len(rmse_vec[idx_mal]))
return mse_vec # rmse_vec
@torch.no_grad()
def test_from_iter(model, thres, X_test):
model.eval()
y_test = X_test[:,-1,:]
# print("X_test",X_test.shape,"y_test",y_test.shape)
X_test = torch.from_numpy(X_test).type(torch.float).to(device)
y_test = torch.from_numpy(y_test).type(torch.float).to(device)
output = model(X_test)
# print("output",output.size(),"y_test",y_test.size())
mse_vec = getMSEvec(output,y_test)
rmse_vec = se2rmse(mse_vec).cpu().data.numpy()
rmse_vec = np.concatenate((np.asarray([0.]*(seq_len-1)),rmse_vec))
idx_mal = np.where(rmse_vec>thres)
idx_ben = np.where(rmse_vec<=thres)
# print(len(rmse_vec[idx_ben]),len(rmse_vec[idx_mal]))
return rmse_vec