-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_LP.py
More file actions
241 lines (204 loc) · 9.92 KB
/
Copy pathtrain_LP.py
File metadata and controls
241 lines (204 loc) · 9.92 KB
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
from sklearn.metrics import roc_auc_score
from src.utils_LP import *
criterion = torch.nn.functional.cross_entropy
def search(model, dataloaders, args, logger):
device = get_device(args)
model.to(device)
train_loader, val_loader, test_loader = dataloaders
optimizer = get_optimizer(model, args)
# for name, param in model.named_parameters():
# print(name, '\t\t', param.shape)
metric = args.metric
recorder = SearchRecorder(metric)
for step in range(args.epoch):
# print(model.log_alpha_agg)
# print(model.Z_agg_hard)
# if step < 2:
# print('#########################################################################################')
# for n, p in model.named_parameters():
# print(n)
# print(p)
optimize_model(model, train_loader, optimizer, device, args)
train_loss, train_acc, train_auc = eval_model(model, train_loader, device)
val_loss, val_acc, val_auc = eval_model(model, val_loader, device)
# test_loss, test_acc, test_auc = eval_model(model, test_loader, device)
# recorder.update(train_acc, train_auc, val_acc, val_auc, test_acc, test_auc)
#####################################################################################################
#####################################################################################################
model.update_z_hard()
if step > 30 and step % 5 == 0 and model.temperature >= 1e-20:
model.temperature *= 1e-1
# model.temperature /= 1.1
#####################################################################################################
#####################################################################################################
recorder.update(step, train_acc, train_auc, val_acc, val_auc)
logger.info('epoch %d best val %s: %.4f, train loss: %.4f; train %s: %.4f val %s: %.4f' %
(step, metric, recorder.get_best_metric()[0], train_loss,
metric, train_auc,
metric, val_auc))
# logger.info('(With validation) final test %s: %.4f (epoch: %d, val %s: %.4f)' %
# (metric, recorder.get_best_metric(val=True)[0],
# recorder.get_best_metric(val=True)[1], metric, recorder.get_best_val_metric(val=True)[0]))
logger.info('(Search Stage) best val acc: %.4f (epoch: %d) ' % recorder.get_best_acc())
logger.info('(Search Stage) best val auc: %.4f (epoch: %d) ' % recorder.get_best_auc())
results, max_step = recorder.get_best_metric()
model.max_step = max_step
model.best_metric_search = results
return model
def retrain(model, dataloaders, args, logger):
device = get_device(args)
model.derive_arch()
logger.info('Derived z')
logger.info(model.searched_arch_z)
logger.info('Derived arch')
logger.info(model.searched_arch_op)
def weight_reset(m):
reset_parameters = getattr(m, "reset_parameters", None)
if callable(reset_parameters):
m.reset_parameters()
model.apply(weight_reset)
model.to(device)
train_loader, val_loader, test_loader = dataloaders
optimizer = get_optimizer(model, args)
metric = args.metric
# recorder = Recorder(metric)
recorder = RetrainRecorder(metric)
for step in range(args.retrain_epoch):
optimize_model(model, train_loader, optimizer, device, args)
train_loss, train_acc, train_auc = eval_model(model, train_loader, device)
# val_loss, val_acc, val_auc = eval_model(model, val_loader, device)
test_loss, test_acc, test_auc, test_labels, test_predictions = eval_model(model, test_loader, device, return_predictions=True)
# recorder.update(train_acc, train_auc, val_acc, val_auc, test_acc, test_auc)
# recorder.update(train_acc, train_auc, val_acc, val_auc)
recorder.update(step, train_acc, train_auc, test_acc, test_auc, test_labels, test_predictions)
logger.info('epoch %d best test %s: %.4f, retrain loss: %.4f; retrain %s: %.4f test %s: %.4f' %
(step, metric, recorder.get_best_metric()[0], train_loss,
metric, train_auc,
metric, test_auc))
# logger.info('(With validation) final test %s: %.4f (epoch: %d, val %s: %.4f)' %
# (metric, recorder.get_best_metric(val=True)[0],
# recorder.get_best_metric(val=True)[1], metric, recorder.get_best_val_metric(val=True)[0]))
logger.info('(Retrain Stage) best test acc: %.4f (epoch: %d) ' % recorder.get_best_acc())
logger.info('(Retrain Stage) best test auc: %.4f (epoch: %d) ' % recorder.get_best_auc())
# return recorder.get_best_metric()[0], recorder.get_best_metric()[0]
# return recorder.get_best_metric()[0]
best_metric, max_step = recorder.get_best_metric()
model.max_step = max_step
model.best_metric_retrain = best_metric
test_predictions, test_labels = recorder.get_best_predicitons()
return model, test_predictions.cpu().detach().numpy(), test_labels.cpu().detach().numpy()
def optimize_model(model, dataloader, optimizer, device, args):
model.train()
# setting of data shuffling move to dataloader creation
for batch in dataloader:
batch = batch.to(device)
label = batch.y
prediction = model(batch)
loss = criterion(prediction, label, reduction='mean')
# loss.backward()
loss.backward(retain_graph=True)
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=args.clip)
optimizer.step()
def eval_model(model, dataloader, device, return_predictions=False):
model.eval()
predictions = []
labels = []
with torch.no_grad():
for batch in dataloader:
batch = batch.to(device)
labels.append(batch.y)
prediction = model(batch)
predictions.append(prediction)
predictions = torch.cat(predictions, dim=0)
labels = torch.cat(labels, dim=0)
loss, acc, auc = compute_metric(predictions, labels)
if not return_predictions:
return loss, acc, auc
else:
return loss, acc, auc, labels, predictions
def compute_metric(predictions, labels):
with torch.no_grad():
# compute loss:
loss = criterion(predictions, labels, reduction='mean').item()
# compute acc:
correct_predictions = (torch.argmax(predictions, dim=1) == labels)
acc = correct_predictions.sum().cpu().item()/labels.shape[0]
# compute auc:
predictions = torch.nn.functional.softmax(predictions, dim=-1)
multi_class = 'ovr'
if predictions.size(1) == 2:
predictions = predictions[:, 1]
multi_class = 'raise'
auc = roc_auc_score(labels.cpu().numpy(), predictions.cpu().numpy(), multi_class=multi_class)
return loss, acc, auc
class SearchRecorder:
"""
always return test numbers except the last method
"""
def __init__(self, metric):
self.metric = metric
self.max_step = 0
self.train_acc, self.val_acc, self.train_auc, self.val_auc = 0., 0., 0., 0.
def update(self, step, train_acc, train_auc, val_acc, val_auc):
if self.val_auc < val_auc:
self.train_acc=train_acc
self.train_auc=train_auc
self.val_acc=val_acc
self.val_auc=val_auc
self.max_step=step
def get_best_metric(self):
dic = {'acc': self.get_best_acc(), 'auc': self.get_best_auc()}
return dic[self.metric]
def get_best_acc(self):
# if val:
# max_step = int(np.argmax(np.array(self.val_accs)))
# else:
# max_step = int(np.argmax(np.array(self.test_accs)))
# return self.test_accs[max_step], max_step
return self.val_acc, self.max_step
def get_best_auc(self):
# if val:
# max_step = int(np.argmax(np.array(self.val_aucs)))
# else:
# max_step = int(np.argmax(np.array(self.test_aucs)))
# return self.test_aucs[max_step], max_step
return self.val_auc, self.max_step
class RetrainRecorder:
"""
always return test numbers except the last method
"""
def __init__(self, metric):
self.metric = metric
self.max_step = 0
self.train_acc, self.test_acc, self.train_auc, self.test_auc = 0., 0., 0., 0.
self.test_labels, self.test_predictions = [], []
def update(self, step, train_acc, train_auc, test_acc, test_auc, test_labels, test_predictions):
if self.test_auc < test_auc:
self.train_acc=train_acc
self.train_auc=train_auc
self.test_acc=test_acc
self.test_auc=test_auc
self.max_step = step
self.test_labels = test_labels
self.test_predictions = test_predictions
def get_best_metric(self):
dic = {'acc': self.get_best_acc(), 'auc': self.get_best_auc()}
return dic[self.metric]
def get_best_acc(self):
# if val:
# max_step = int(np.argmax(np.array(self.val_accs)))
# else:
# max_step = int(np.argmax(np.array(self.test_accs)))
# return self.test_accs[max_step], max_step
# max_step = int(np.argmax(np.array(self.val_accs)))
return self.test_acc, self.max_step
def get_best_auc(self):
# if val:
# max_step = int(np.argmax(np.array(self.val_aucs)))
# else:
# max_step = int(np.argmax(np.array(self.test_aucs)))
# return self.test_aucs[max_step], max_step
# max_step = int(np.argmax(np.array(self.val_aucs)))
return self.test_auc, self.max_step
def get_best_predicitons(self):
return self.test_predictions, self.test_labels