forked from arunppsg/TadGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
251 lines (196 loc) · 8.41 KB
/
main.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
#!/usr/bin/env python
# coding: utf-8
import os
import logging
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import model
import anomaly_detection
logging.basicConfig(filename='train.log', level=logging.DEBUG)
class SignalDataset(Dataset):
def __init__(self, path):
self.signal_df = pd.read_csv(path)
self.signal_columns = self.make_signal_list()
self.make_rolling_signals()
def make_signal_list(self):
signal_list = list()
for i in range(-50, 50):
signal_list.append('signal'+str(i))
return signal_list
def make_rolling_signals(self):
for i in range(-50, 50):
self.signal_df['signal'+str(i)] = self.signal_df['signal'].shift(i)
self.signal_df = self.signal_df.dropna()
self.signal_df = self.signal_df.reset_index(drop=True)
def __len__(self):
return len(self.signal_df)
def __getitem__(self, idx):
row = self.signal_df.loc[idx]
x = row[self.signal_columns].values.astype(float)
x = torch.from_numpy(x)
return {'signal':x, 'anomaly':row['anomaly']}
def critic_x_iteration(sample):
optim_cx.zero_grad()
x = sample['signal'].view(1, batch_size, signal_shape)
valid_x = critic_x(x)
valid_x = torch.squeeze(valid_x)
critic_score_valid_x = torch.mean(torch.ones(valid_x.shape) * valid_x) #Wasserstein Loss
#The sampled z are the anomalous points - points deviating from actual distribution of z (obtained through encoding x)
z = torch.empty(1, batch_size, latent_space_dim).uniform_(0, 1)
x_ = decoder(z)
fake_x = critic_x(x_)
fake_x = torch.squeeze(fake_x)
critic_score_fake_x = torch.mean(torch.ones(fake_x.shape) * fake_x) #Wasserstein Loss
alpha = torch.rand(x.shape)
ix = Variable(alpha * x + (1 - alpha) * x_) #Random Weighted Average
ix.requires_grad_(True)
v_ix = critic_x(ix)
v_ix.mean().backward()
gradients = ix.grad
#Gradient Penalty Loss
gp_loss = torch.sqrt(torch.sum(torch.square(gradients).view(-1)))
#Critic has to maximize Cx(Valid X) - Cx(Fake X).
#Maximizing the above is same as minimizing the negative.
wl = critic_score_fake_x - critic_score_valid_x
loss = wl + gp_loss
loss.backward()
optim_cx.step()
return loss
def critic_z_iteration(sample):
optim_cz.zero_grad()
x = sample['signal'].view(1, batch_size, signal_shape)
z = encoder(x)
valid_z = critic_z(z)
valid_z = torch.squeeze(valid_z)
critic_score_valid_z = torch.mean(torch.ones(valid_z.shape) * valid_z)
z_ = torch.empty(1, batch_size, latent_space_dim).uniform_(0, 1)
fake_z = critic_z(z_)
fake_z = torch.squeeze(fake_z)
critic_score_fake_z = torch.mean(torch.ones(fake_z.shape) * fake_z) #Wasserstein Loss
wl = critic_score_fake_z - critic_score_valid_z
alpha = torch.rand(z.shape)
iz = Variable(alpha * z + (1 - alpha) * z_) #Random Weighted Average
iz.requires_grad_(True)
v_iz = critic_z(iz)
v_iz.mean().backward()
gradients = iz.grad
gp_loss = torch.sqrt(torch.sum(torch.square(gradients).view(-1)))
loss = wl + gp_loss
loss.backward()
optim_cz.step()
return loss
def encoder_iteration(sample):
optim_enc.zero_grad()
x = sample['signal'].view(1, batch_size, signal_shape)
valid_x = critic_x(x)
valid_x = torch.squeeze(valid_x)
critic_score_valid_x = torch.mean(torch.ones(valid_x.shape) * valid_x) #Wasserstein Loss
z = torch.empty(1, batch_size, latent_space_dim).uniform_(0, 1)
x_ = decoder(z)
fake_x = critic_x(x_)
fake_x = torch.squeeze(fake_x)
critic_score_fake_x = torch.mean(torch.ones(fake_x.shape) * fake_x)
enc_z = encoder(x)
gen_x = decoder(enc_z)
mse = mse_loss(x.float(), gen_x.float())
loss_enc = mse + critic_score_valid_x - critic_score_fake_x
loss_enc.backward(retain_graph=True)
optim_enc.step()
return loss_enc
def decoder_iteration(sample):
optim_dec.zero_grad()
x = sample['signal'].view(1, batch_size, signal_shape)
z = encoder(x)
valid_z = critic_z(z)
valid_z = torch.squeeze(valid_z)
critic_score_valid_z = torch.mean(torch.ones(valid_z.shape) * valid_z)
z_ = torch.empty(1, batch_size, latent_space_dim).uniform_(0, 1)
fake_z = critic_z(z_)
fake_z = torch.squeeze(fake_z)
critic_score_fake_z = torch.mean(torch.ones(fake_z.shape) * fake_z)
enc_z = encoder(x)
gen_x = decoder(enc_z)
mse = mse_loss(x.float(), gen_x.float())
loss_dec = mse + critic_score_valid_z - critic_score_fake_z
loss_dec.backward(retain_graph=True)
optim_dec.step()
return loss_dec
def train(n_epochs=2000):
logging.debug('Starting training')
cx_epoch_loss = list()
cz_epoch_loss = list()
encoder_epoch_loss = list()
decoder_epoch_loss = list()
for epoch in range(n_epochs):
logging.debug('Epoch {}'.format(epoch))
n_critics = 5
cx_nc_loss = list()
cz_nc_loss = list()
for i in range(n_critics):
cx_loss = list()
cz_loss = list()
for batch, sample in enumerate(train_loader):
loss = critic_x_iteration(sample)
cx_loss.append(loss)
loss = critic_z_iteration(sample)
cz_loss.append(loss)
cx_nc_loss.append(torch.mean(torch.tensor(cx_loss)))
cz_nc_loss.append(torch.mean(torch.tensor(cz_loss)))
logging.debug('Critic training done in epoch {}'.format(epoch))
encoder_loss = list()
decoder_loss = list()
for batch, sample in enumerate(train_loader):
enc_loss = encoder_iteration(sample)
dec_loss = decoder_iteration(sample)
encoder_loss.append(enc_loss)
decoder_loss.append(dec_loss)
cx_epoch_loss.append(torch.mean(torch.tensor(cx_nc_loss)))
cz_epoch_loss.append(torch.mean(torch.tensor(cz_nc_loss)))
encoder_epoch_loss.append(torch.mean(torch.tensor(encoder_loss)))
decoder_epoch_loss.append(torch.mean(torch.tensor(decoder_loss)))
logging.debug('Encoder decoder training done in epoch {}'.format(epoch))
logging.debug('critic x loss {:.3f} critic z loss {:.3f} \nencoder loss {:.3f} decoder loss {:.3f}\n'.format(cx_epoch_loss[-1], cz_epoch_loss[-1], encoder_epoch_loss[-1], decoder_epoch_loss[-1]))
if epoch % 10 == 0:
torch.save(encoder.state_dict(), encoder.encoder_path)
torch.save(decoder.state_dict(), decoder.decoder_path)
torch.save(critic_x.state_dict(), critic_x.critic_x_path)
torch.save(critic_z.state_dict(), critic_z.critic_z_path)
if __name__ == "__main__":
dataset = pd.read_csv('exchange-2_cpc_results.csv')
#Splitting intro train and test
#TODO could be done in a more pythonic way
train_len = int(0.7 * dataset.shape[0])
dataset[0:train_len].to_csv('train_dataset.csv', index=False)
dataset[train_len:].to_csv('test_dataset.csv', index=False)
train_dataset = SignalDataset(path='train_dataset.csv')
test_dataset = SignalDataset(path='test_dataset.csv')
batch_size = 64
train_loader = DataLoader(train_dataset, batch_size=batch_size, drop_last=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, drop_last=True)
logging.info('Number of train datapoints is {}'.format(len(train_dataset)))
logging.info('Number of samples in train dataset {}'.format(len(train_dataset)))
lr = 1e-6
signal_shape = 100
latent_space_dim = 20
encoder_path = 'models/encoder.pt'
decoder_path = 'models/decoder.pt'
critic_x_path = 'models/critic_x.pt'
critic_z_path = 'models/critic_z.pt'
encoder = model.Encoder(encoder_path, signal_shape)
decoder = model.Decoder(decoder_path, signal_shape)
critic_x = model.CriticX(critic_x_path, signal_shape)
critic_z = model.CriticZ(critic_z_path)
mse_loss = torch.nn.MSELoss()
optim_enc = optim.Adam(encoder.parameters(), lr=lr, betas=(0.5, 0.999))
optim_dec = optim.Adam(decoder.parameters(), lr=lr, betas=(0.5, 0.999))
optim_cx = optim.Adam(critic_x.parameters(), lr=lr, betas=(0.5, 0.999))
optim_cz = optim.Adam(critic_z.parameters(), lr=lr, betas=(0.5, 0.999))
train(n_epochs=1)
anomaly_detection.test(test_loader, encoder, decoder, critic_x)