-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta.py
203 lines (159 loc) · 8.12 KB
/
meta.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
import torch
from torch import nn
from torch import optim
from torch.nn import functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch import optim
import numpy as np
from learner import Learner
from copy import deepcopy
from sklearn.metrics import auc, roc_curve
from utils import aucPerformance
def dev_loss(y_true, y_prediction):
'''
z-score based deviation loss
:param y_true: true anomaly labels
:param y_prediction: predicted anomaly label
:return: loss in training
'''
confidence_margin = 5.0
ref = torch.tensor(np.random.normal(loc=0.0, scale=1.0, size=5000), dtype=torch.float32)
dev = (y_prediction - torch.mean(ref)) / torch.std(ref)
inlier_loss = torch.abs(dev)
outlier_loss = confidence_margin - dev
outlier_loss[outlier_loss < 0.] = 0
return torch.mean((1 - y_true) * inlier_loss.flatten() + y_true * outlier_loss.flatten())
class Meta(nn.Module):
def __init__(self, args, config):
super(Meta, self).__init__()
self.update_lr = args.update_lr
self.meta_lr = args.meta_lr
self.task_num = args.num_graph - 1
self.update_step = args.update_step
self.update_step_test = args.update_step_test
self.net = Learner(config)
self.meta_optim = optim.Adam(self.net.parameters(), lr=self.meta_lr)
def forward(self, x_train, y_train, x_qry, y_qry):
'''
:param x_train: [nb_task, batch_size, attr_dimension]
:param y_train: [nb_task, batch_size]
:param x_qry: [nb_task, qry_batch_size, attr_dimension]
:param y_qry: [nb_task, qry_batch_size]
:return:
'''
num_task = len(x_train)
losses = [0 for _ in range(self.update_step + 1)]
results = []
for t in range(num_task):
prediction,encoder = self.net(x_train[t], vars=None, bn_training=True)
loss = dev_loss(y_train[t], prediction)
grad = torch.autograd.grad(loss, self.net.parameters())
# update the parameters
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
#before the first update
with torch.no_grad():
prediction_q,encoder_q = self.net(x_qry[t], self.net.parameters(), bn_training=True)
loss_q = dev_loss(y_qry[t], prediction_q)
losses[0] += loss_q
# after the first update
with torch.no_grad():
prediction_q,encoder_q = self.net(x_qry[t], adapt_weights, bn_training=True)
loss_q = dev_loss(y_qry[t], prediction_q)
losses[1] += loss_q
# for multiple step update
for k in range(1, self.update_step):
# evaluate the i-th task
prediction,encoder_q = self.net(x_train[t], adapt_weights, bn_training=True)
loss = dev_loss(y_train[t], prediction)
# compute gradients on theta'
grad = torch.autograd.grad(loss, adapt_weights)
# perform one-step update step i + 1
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, adapt_weights)))
prediction_q,encoder_q = self.net(x_qry[t], adapt_weights, bn_training=True)
loss_q = dev_loss(y_qry[t], prediction_q)
losses[k+1] += loss_q
# evaluation can be done here
# finish all tasks
loss_f = losses[-1] / num_task
# update parameters
self.meta_optim.zero_grad()
loss_f.backward()
self.meta_optim.step()
# evaluate
return loss_f
def evaluate(self, x_train, y_train, x_test, y_test):
prediction,encoder = self.net(x_train[0], vars=None, bn_training=True)
criterion = nn.MSELoss()
loss = dev_loss(y_train[0], prediction[:prediction.shape[0]//2])+ criterion(encoder[0][:prediction.shape[0]//2], encoder[0][prediction.shape[0]//2:])
grad = torch.autograd.grad(loss, self.net.parameters())
# update the parameters
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# for multiple step update
for k in range(1, self.update_step_test):
# evaluate the i-th task
prediction,encoder = self.net(x_train[0], adapt_weights, bn_training=True)
loss = dev_loss(y_train[0], prediction[:prediction.shape[0]//2])+ criterion(encoder[0][:prediction.shape[0]//2], encoder[0][prediction.shape[0]//2:])
# compute gradients on theta'
grad = torch.autograd.grad(loss, adapt_weights)
# perform one-step update step i + 1
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, adapt_weights)))
for i in range(1, len(x_train)):
# for multiple step update
for k in range(self.update_step_test):
# evaluate the i-th task
prediction,encoder = self.net(x_train[i], adapt_weights, bn_training=True)
loss = dev_loss(y_train[i], prediction[:prediction.shape[0]//2])+ criterion(encoder[0][:prediction.shape[0]//2], encoder[0][prediction.shape[0]//2:])
# compute gradients on theta'
grad = torch.autograd.grad(loss, adapt_weights)
# perform one-step update step i + 1
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, adapt_weights)))
y_pred,_ = self.net(x_test, adapt_weights, bn_training=True)
y_test = y_test.detach().cpu().numpy()
y_pred = y_pred.detach().cpu().numpy()
auc_roc, auc_pr, ap = aucPerformance(y_test, y_pred)
return auc_roc, auc_pr, ap
def evaluate2(self, x_train, y_train, x_test, y_test):
for i in range(len(x_train)):
prediction = self.net(x_train[i], vars=None, bn_training=True)
loss = dev_loss(y_train[i], prediction)
grad = torch.autograd.grad(loss, self.net.parameters())
# update the parameters
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# for multiple step update
for k in range(1, self.update_step_test):
# evaluate the i-th task
prediction = self.net(x_train[i], adapt_weights, bn_training=True)
loss = dev_loss(y_train[i], prediction)
# compute gradients on theta'
grad = torch.autograd.grad(loss, adapt_weights)
# perform one-step update step i + 1
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, adapt_weights)))
y_pred = self.net(x_test, adapt_weights, bn_training=True)
y_test = y_test.detach().cpu().numpy()
y_pred = y_pred.detach().cpu().numpy()
auc_roc, auc_pr, ap = aucPerformance(y_test, y_pred)
return auc_roc, auc_pr, ap
def evaluate_backup(self, x_train, y_train, x_test, y_test):
prediction = self.net(x_train, vars=None, bn_training=True)
loss = dev_loss(y_train, prediction)
grad = torch.autograd.grad(loss, self.net.parameters())
# update the parameters
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# for multiple step update
for k in range(1, self.update_step_test):
# evaluate the i-th task
prediction = self.net(x_train, adapt_weights, bn_training=True)
loss = dev_loss(y_train, prediction)
# compute gradients on theta'
grad = torch.autograd.grad(loss, adapt_weights)
# perform one-step update step i + 1
adapt_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, adapt_weights)))
y_pred = self.net(x_test, adapt_weights, bn_training=True)
y_test = y_test.detach().cpu().numpy()
y_pred = y_pred.detach().cpu().numpy()
auc_roc, auc_pr, ap = aucPerformance(y_test, y_pred)
return auc_roc, auc_pr, ap
def main():
pass
if __name__ == '__main__':
main()