-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrainer.py
1152 lines (930 loc) · 44.2 KB
/
trainer.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
torch.backends.cudnn.benchmark =True
from torch import nn
from torch.utils.data import TensorDataset, DataLoader
from torch.optim.lr_scheduler import MultiStepLR, StepLR
from torch.utils.tensorboard import SummaryWriter
# PyTorch logger: https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/04-utils/tensorboard/logger.py
import os
import json
import time
import warnings
import numpy as np
from datetime import datetime
from tqdm.auto import tqdm
# # control randomness
# np.random.seed(0)
# torch.manual_seed(0)
import utils
# Save model with epoch number and validation error
def save_network(model, epoch, err, optimizer, save_path):
# deal with multi-gpu parallelism
pa = type(model)
if pa.__name__ == 'DataParallel':
net_save = model.module
else:
net_save = model
# format to save
state = {
'net' : net_save,
'err' : err, # error rate or negative loss (something to minimize)
'epoch': epoch,
'opt' : optimizer,
}
torch.save(state, save_path)
# Load model with epoch number and validation error
def load_network(network, net_path):
optimizer = None
epoch = -1
err = np.nan
if network: # if the given template network is not none
check_pt = torch.load(net_path)
if 'net' in check_pt:
checkpoint = check_pt['net']
epoch = check_pt['epoch']
err = check_pt['err']
if 0:#'opt' in check_pt:
optimizer = check_pt['opt']
else:
checkpoint = check_pt
# assert checkpoint should be a nn.module or parallel nn.module
pa_0 = type(checkpoint)
if pa_0.__name__ == 'DataParallel':
checkpoint = checkpoint.module
else:
pass
new_dict = checkpoint.state_dict()
# same to the network template
pa_1 = type(network)
if pa_1.__name__ == 'DataParallel':
network = network.module
old_dict = network.state_dict()
# make sure new and old state_dict match each other
new_keys = set(new_dict.keys())
old_keys = set(old_dict.keys())
if old_keys == new_keys:
network.load_state_dict(new_dict)
else:
if new_keys-old_keys:
warnings.warn("Ignoring keys in new dict: {}".format(new_keys-old_keys))
if old_keys-new_keys:
warnings.warn("Missing keys in old dict: {}".format(old_keys-new_keys))
# filter out unnecessary keys
new_dict = {k: v for k, v in new_dict.items() if k in old_dict}
# overwrite entries in the existing state dict
old_dict.update(new_dict)
# load the new state dict
network.load_state_dict(old_dict)
# if network used to be DataParallel, now it's time to convert it back to DP
if pa_0.__name__ == 'DataParallel':
network = pa_0(network)
elif pa_1.__name__ == 'DataParallel':
network = pa_1(network)
else: # if not given any network template
checkpoint = torch.load(net_path)
if 'net' in checkpoint:
network = checkpoint['net']
epoch = checkpoint['epoch']
err = checkpoint['err']
if 'opt' in checkpoint:
optimizer = checkpoint['opt']
else:
network = checkpoint
return network, epoch, err, optimizer
# ----------------------------------
# -------- Trainer Wrapper ---------
# ----------------------------------
class _trainer(object):
def __init__(self, model, check_path, optimizer, resume=True, ft_path=None, **kwargs):
super().__init__()
self.bs = None # placeholder for batch_size
self.pbar = None
self.cp = check_path
self.jlogs_path = self.cp.replace('model.pt','logs.json')
self.global_step = 0
if model:
self.model, self.epoch, self.track_minimize, self.optimizer = model, 0, np.inf, None
else:
self.model, self.epoch, self.track_minimize, self.optimizer = load_network(model, self.cp)
self.mu, self.Pc = self.model.mu, self.model.Pc
# optimizer
if self.optimizer is None:
self.set_optimizer(optimizer)
self.device = next(self.model.parameters()).device
# tensorboard
self.logger = SummaryWriter(self.cp+'-logs')
# optional configs
extract_args = lambda a, k: a if not k in kwargs else kwargs[k]
self.l2 = extract_args( 0, 'l2' ) # l2 regularization
self.ds = extract_args( np.inf, 'display_step' )
self.gc = extract_args( None, 'gradient_clipping' )
self.autostop = extract_args( np.inf, 'auto_stop' )
decay_step_ = extract_args( None, 'decay_step' )
if decay_step_ is not None:
steps, gamma = decay_step_
if isinstance(steps, list):
self.lr_scheduler = MultiStepLR(self.optimizer, milestones=steps, gamma=gamma)
elif isinstance(steps, int):
self.lr_scheduler = StepLR(self.optimizer, step_size=steps, gamma=gamma)
else:
self.lr_scheduler = None
self.trackstop = 0
self.stop = False
self.logs = {}
if ft_path:
self._resume_other(ft_path)
if resume:
self._resume()
#------------------------------
# JSON log files i/o
#------------------------------
def save_json_logs(self, desc=''):
if len(self.logs):
with open(self.jlogs_path+desc, 'w') as fp:
json.dump(self.logs, fp)
return True
return False
def load_json_logs(self, desc=''):
if os.path.exists(self.jlogs_path):
with open(self.jlogs_path+desc, 'r') as fp:
self.logs = json.load(fp)
return True
return False
#------------------------------
# Model and checkpoints saving
#------------------------------
def _save_latest(self, desc=None):
if not desc:
desc = "-latest"
else:
desc = "-ep%d"%self.epoch
# save latest checkpoint upon exit
# ref : http://effbot.org/zone/stupid-exceptions-keyboardinterrupt.htm
save_network(self.model, self.epoch, self.track_minimize, self.optimizer, self.cp+desc)
sflag = self.save_json_logs()
utils.print_update("Saving to {} ... (jlogs:{})".format(self.cp+desc, sflag), self.pbar)
# Alias of _save_latest
def save_checkpoint(self, desc=None):
return self._save_latest(desc)
# Save & restore model
def save(self, epoch):
# normal save
save_network(self.model, epoch, self.track_minimize, self.optimizer, self.cp)
utils.print_update("Saving to {} ...".format(self.cp), self.pbar)
# Externally: restore the best epoch
def restore(self):
assert self.model is not None, "Must initialize a model!"
if os.path.exists(self.cp):
self.model, self.epoch, self.track_minimize, optimizer_loaded = load_network(self.model, self.cp)
if optimizer_loaded is not None:
self.optimizer = optimizer_loaded
jflag = self.load_json_logs()
print(f"Loading from {self.cp} at epoch {self.epoch} (jlogs:{jflag}) ...")
if self.lr_scheduler is not None:
self.lr_scheduler._step_count = self.epoch
# Internally: resume from the latest checkpoint. Update epoch and track_min values
def _resume(self):
res_path = self.cp+'-latest'
if os.path.exists(res_path):
self.model, self.epoch, self.track_minimize, optimizer_loaded = load_network(self.model, res_path)
if optimizer_loaded is not None:
self.optimizer = optimizer_loaded
jflag = self.load_json_logs()
if self.lr_scheduler is not None:
self.lr_scheduler._step_count = self.epoch
print(f"Resuming training from {res_path} at epoch {self.epoch} (jlogs:{jflag}) ...")
else:
print("Starting fresh at epoch 0 ...")
# Internally: reload model from some location. Keep fresh epoch and track_min values
def _resume_other(self, res_path):
if os.path.exists(res_path):
self.model, old_epoch, _, _ = load_network(self.model, res_path)
jflag = False#self.load_json_logs()
print(f"Finetuning from {res_path} (epoch:{old_epoch}); Starting at epoch {self.epoch} (jlogs:{jflag}) ...")
else:
raise ValueError("No prior model to resume! Check path or turn off FT mode. Exiting..")
#-------------------------------------
# Monotonic regularizer implementation
#-------------------------------------
# Data augmentation for monotonic regularizer
def unsup_augmentation(self, x, additive, p_shape, cpu=False):
# 1. raplce x new (interpolate)
plin_new = 10**((torch.log10(x[:,-1])*10 + additive)/10)
x_new = torch.cat((x[:,:-1], plin_new.view(-1,1)), 1)
inputs = self.batch_data_handler(x_new)
# 2. predict wsee
y_pred_aug, _ = self.model(*inputs)
y_pred_aug = y_pred_aug.view(p_shape)
if not cpu:
wsee_aug = utils.f_pointwise_ee_torch(y_pred_aug, x_new, self.mu, self.Pc)
else:
device = x.device
wsee_aug = utils.f_pointwise_ee_torch(y_pred_aug.cpu(), x_new.cpu(), self.mu, self.Pc)
wsee_aug = wsee_aug.to(device)
return y_pred_aug, wsee_aug
# Stand alone augmentation loss
def _augmentation_loss(self, y_pred, x, wsee, cpu=False):
additive = torch.empty(x.shape[0]).uniform_(-1 , 1).to(device)
y_pred_new, wsee_new = self.unsup_augmentation( x, additive, y_pred.shape, cpu )
wsee_loss_aug = -wsee_new.sum(1).mean()/ y_pred.shape[-1]
return wsee_loss_aug
def _monotonicity_loss(self, y_pred, x, wsee, factor=1e+3, cpu=False):
additive = -1*torch.ones(x.shape[0]).to(self.device)
with torch.no_grad():
y_pred_new, wsee_new = self.unsup_augmentation( x, additive, y_pred.shape, cpu )
wsee_bk = wsee_new.detach()
y_bk = y_pred_new.detach()
# 3. check monotonicity
delta_wsee = wsee - wsee_bk.sum(1) # positive positions: wsee new < wsee old
delta_pmax = torch.sign(additive) # positive positions: p_max new > p_max old
# 4. scale with loss
delta = delta_pmax*delta_wsee
mask_incorr = delta>0 # where monotonicity is violated
mask_incorr_increm = delta_wsee[mask_incorr]<0 # violations where wsee new > wsee old
incorr = torch.mean((delta[mask_incorr][mask_incorr_increm])**2)
pt_increm = torch.nn.functional.smooth_l1_loss(
y_pred[mask_incorr][mask_incorr_increm],
y_bk[mask_incorr][mask_incorr_increm]
)
mono_loss = incorr + factor * pt_increm
return mono_loss
#--------------------------------
# Set which loss functions to use
#--------------------------------
def set_criteria(self, crit, cweights=None):
# parse criteria, e.g. self.criterion = { 'mse':1, 'wsee':1, 'augm':1, 'mono':1 }
cpool = ['mse', 'rmse', 'mae', 'huber', 'huberh', 'mseh', 'wsee', 'augm', 'mono']
if isinstance(crit, str):
crit = [ct.lower() for ct in crit.split('+')]
elif isinstance(crit, list):
crit = [ct.lower() for ct in crit]
assert np.all([ct in cpool for ct in crit])
if cweights is not None:
wnorm = 1#sum(cweights)
cweights = [w/wnorm for w in cweights]
else:
wnorm = len(crit)
cweights = [1/wnorm for _ in crit]
self.criterion = {k:v for k,v in zip(crit, cweights)}
def compute_objective(self, y, x, cpu, func=utils.f_pointwise_ee_torch):
if not cpu:
uval_pt = func(y, x, self.mu, self.Pc)
else:
device = y_pred.device
uval_pt = func(y.cpu(), x.cpu(), self.mu, self.Pc)
uval_pt = uval_pt.to(device)
return uval_pt
# loss and backprop step (in training and prediction)
def loss_func(self, y_pred, y_true, x, weights=None, cpu=False):
sup, obj, mono = [torch.tensor(0.).to(self.device) for _ in range(3)] # scalar
# unsupvised loss
if 'wsee' in self.criterion:
wsee_pt = self.compute_objective(y_pred, x, cpu, utils.f_pointwise_ee_torch)
wsee = wsee_pt.sum(1)
obj = -wsee.mean()/ y_pred.shape[-1]
obj *= self.criterion['wsee']
else:
raise NotImplemented
# mono penalty
if 'mono' in self.criterion:# and self.model.training:
assert 'wsee' in self.criterion
mono = self._monotonicity_loss(y_pred, x, wsee, cpu)
mono *= self.criterion['mono']
# supervised loss
sup_scale = 1
filtered = torch.any(y_true.isnan(),dim=1)
ytf, ypf = y_true[~filtered], y_pred[~filtered]
if 'mse' in self.criterion and torch.any(~filtered):
sup = sup_scale*((ypf - ytf)**2).mean() # scalar
sup *= self.criterion['mse']
elif 'mae' in self.criterion and torch.any(~filtered):
sup = sup_scale*(torch.abs(ypf - ytf)).mean() # scalar
sup *= self.criterion['mae']
elif 'rmse' in self.criterion and torch.any(~filtered):
sup = sup_scale*((ypf - ytf)**2).mean()**.5 # scalar
sup *= self.criterion['rmse']
elif 'huber' in self.criterion and torch.any(~filtered):
sup = sup_scale*torch.nn.functional.smooth_l1_loss(ypf, ytf)
sup *= self.criterion['huber']
elif 'huberh' in self.criterion and torch.any(~filtered):
wsee_supv = self.compute_objective(ytf, x[~filtered], cpu).sum(1)
sup_filtered = (wsee_supv - wsee[~filtered])>0
if torch.any(sup_filtered):
sup = sup_scale*torch.nn.functional.smooth_l1_loss(ypf[sup_filtered], ytf[sup_filtered])
sup *= self.criterion['huberh']
elif 'mseh' in self.criterion and torch.any(~filtered):
wsee_supv = self.compute_objective(ytf, x[~filtered], cpu).sum(1)
sup_filtered = (wsee_supv - wsee[~filtered])>0
if torch.any(sup_filtered):
sup = sup_scale*((ypf[sup_filtered] - ytf[sup_filtered])**2).mean()
sup *= self.criterion['mseh']
loss = sup + obj + mono
l = loss.item()
m = sup.item()
w = wsee.mean().item()
return loss, (l, m, w)
#------------------------------------------------
# Set the optimizer for training, simple or decay
#------------------------------------------------
def set_optimizer_simple(self, configs):
if configs is None or 'torch.optim' in str(type(configs)):
self.optimizer = configs
else:
opt_inst, opt_config = configs
self.optimizer = opt_inst(filter(lambda p: p.requires_grad, self.model.parameters()), **opt_config)
def set_optimizer_decay(self, configs):
if configs is None:
pass
elif 'torch.optim' in str(type(configs)):
raise
else:
opt_inst, opt_config = configs
module_names = self.model._modules.keys()
# confirm module_names are legal
assert all([m in ['embedding', 'sca', 'final'] for m in module_names])
param_groups = []
if 'embedding' in module_names:
cflag = True
param_groups.append({'params':filter(lambda p: p.requires_grad, self.model.embedding.parameters()),
'name': 'emb'})
else:
cflag = False
if 'sca' in module_names:
nblocks = len(self.model.sca)
for t in range(nblocks):
param_groups.append({'params':filter(lambda p: p.requires_grad, self.model.sca[t].parameters()),
'name': f'sca_{t}'})
else:
raise ValueError('The model must have a module named sca!')
if 'final' in module_names:
param_groups.append({'params':filter(lambda p: p.requires_grad, self.model.final.parameters()),
'name': 'fin'})
if cflag: # combine "emb" and "sca_0"; remove "emb"
assert param_groups[1]['name']=='sca_0'
param_groups[1]['params']=list(param_groups[0]['params'])+list(param_groups[1]['params'])
param_groups.pop(0)
self.optimizer = opt_inst(param_groups, **opt_config)
#---------------------------------------------
# Optimizer step methods, simple or decay
#---------------------------------------------
def opt_step_handler_simple(self, loss):
self.optimizer.zero_grad()
# scalar loss (or vector loss)
loss.backward() #(torch.ones(self.nu).to(self.device))
# gradient clipping
if self.gc:
nn.utils.clip_grad_norm_(self.model.parameters(), self.gc)
self.optimizer.step()
def opt_step_handler_decay(self, loss, decay):
# decay : a list of learning rate decay rates
decay = self.block_decay if decay is None else decay
ng = len(self.optimizer.param_groups)
decay = [decay]*ng if not hasattr(decay, "__len__") else decay
# decay learning rate
for g,d in zip(self.optimizer.param_groups, decay[:ng]):
g['lr'] = self.lr_init * d
self.opt_step_handler_simple(loss)
#------------------------------
# Data handling for training
#------------------------------
# Returns the numbers of nodes and steps
def calc_numbers(self, loader):
max_nodes = loader.dataset[0].x.shape[0]
self.T = min(loader.dataset[0].y.shape[1], self.max_steps)
self.bs = loader.batch_size
n = max_nodes*self.bs*self.T
return max_nodes, n
# Return data loaders
def get_loaders(self, dict_loaders):
# get optimal wsee
self.logs['wsee_opt'] = {k:utils.f_wsee_torch(*v[1::-1], self.mu, self.Pc, 'mean').item() for k,v in dict_loaders.items()}
# get the loaders
if 'simple' in self.__class__.__name__.lower():
return dict_loaders
elif 'seq' in self.__class__.__name__.lower():
return dict_loaders
else:
return {k: DataLoader(TensorDataset(*v[:-1]), batch_size=v[-1], shuffle= k=='tr') for k,v in dict_loaders}
#--------------------------------------
# Load data and labels, simple or graph
#--------------------------------------
def batch_data_handler_simple(self, data):
if isinstance(data, list):
[d.to(self.device) for d in data]
for dd in range(len(data)): # update x steps
data[dd].y = data[dd].y[...,:self.T]
x_true = torch.cat([d.x for d in data],0)
y_true = torch.cat([d.y for d in data],0)
A_coo = torch.cat([d.edge_index for d in data],0)
else:
data.to(self.device)
x_true = data.x
y_true = data.y[...,:self.T]
A_coo = data.edge_index
# if 'CVAE', considering both parallel/nonparallel model cases
try:
assert 'CVAE' in self.model.__class__.__name__ or 'CVAE' in self.model.module.__class__.__name__
return data, y_true, A_coo
except:
return x_true, y_true, A_coo
def batch_data_handler_graph(self, data):
edge_weight_batch = data[:,self.nu:-1].reshape(-1)
y_init = data[:,:self.nu].reshape(-1,1) # inital signals (p_init)
y_constr = data[:,:self.nu].reshape(-1,1) # upper power constraint (p_max)
return (y_init, y_constr), self.edge_index_batch, edge_weight_batch
#-------------------------------------------
# Training data preperation, simple or graph
#-------------------------------------------
# Set configs; return x_train, y_train and index (permutated)
def training_prep_simple(self, epoch, loader):
torch.cuda.empty_cache()
self.model.train() # set to train mode
self.epoch = epoch
self.global_step = epoch*len(loader) if not self.global_step else self.global_step
# get number of users and batch size
X_train, y_train, self.bs = loader
if y_train is not None:
ns, self.nu = y_train.shape
else:
ns = X_train.shape[0]
self.nu = np.floor((X_train.shape[1]-1)**.5).astype(int)
perm_i = np.random.permutation(ns)
return X_train, y_train, perm_i
def training_prep_graph(self, epoch, loader):
X_train, y_train, perm_i = self.training_prep_simple(epoch, loader)
# edge index for mini batches
self.edge_index_batch = self.edge_info_proc(self.ei, self.nu, self.bs)
return X_train, y_train, perm_i
#------------------------
# Performance evaluation
#------------------------
def calculate_metric(self, loader, metric='mse'):
y_true, y_hat, A_coo = self.predict(self.epoch, criterion=None, loader=loader, save=False)
if metric == 'mse':
m = np.mean((y_true- y_hat)**2)
elif metric == 'auc':
from sklearn import metrics
m = metrics.roc_auc_score(y_true.flatten(), y_hat.flatten())
else:
raise NotImplemented
return m, (y_true, y_hat, A_coo)
# Tensorboard logging
def logging(self, info, phase):
step = self.global_step*self.bs if phase.startswith('tr') else self.epoch
# Log scalar values (scalar summary)
for k,v in info.items():
self.logger.add_scalar('%s/%s'%(k,phase), v, step)
if phase.startswith('tr'):
# Log values and gradients of the parameters (histogram summary)
for tag, value in self.model.named_parameters():
if value.requires_grad:#'enc' in tag.lower() or 'lstm' in tag.lower():
try:
tag = '.'.join(tag.split('.')[:-1])+'/'+tag.split('.')[-1]
self.logger.add_histogram(tag, value.data.cpu().numpy(), step)
self.logger.add_histogram(tag+'/grad', value.grad.data.cpu().numpy(), step)
except:
continue
#-----------------------------------------------------------
# Train and Predict methods, and other utility to be implemented
#-----------------------------------------------------------
def train(self):
raise NotImplemented
def predict(self):
raise NotImplemented
def batch_data_handler(self, data):
return self.batch_data_handler_simple(data)
def opt_step_handler(self, loss):
return self.opt_step_handler_simple(loss)
def set_optimizer(self, configs):
return self.set_optimizer_simple(configs)
def training_prep(self, epoch, loader):
return self.training_prep_simple(epoch, loader)
class Simple_Trainer(_trainer):
"""
load all data first; not using pytorch's dataloader
"""
def batch_data_handler(self, data):
return [data]
# returns the numbers
def calc_numbers(self, loader):
self.nu = loader.dataset[0][1].shape[-1] # number of users
self.bs = loader.batch_size # batch size
# simple logging via dict: self.logs
def logging(self, info, phase):
pkey = '%s'%phase[:2]
for k,v in info.items():
if k not in self.logs:
self.logs[k] = {}
if pkey not in self.logs[k]:
self.logs[k][pkey] = [v]
else:
self.logs[k][pkey].append(v)
if not len(self.logs[k][pkey]) < self.epoch+1:
self.logs[k][pkey] = self.logs[k][pkey][:self.epoch+2]
else:
print(self.logs[k][pkey])
raise ValueError
# Train & test functions for a single epoch
def train(self, epoch, loader, pbar=None, **kwargs):
self.pbar = pbar # simple progress bar handling
x_train, y_train, perm_i = self.training_prep(epoch, loader) # set train here
# tracking loss and other stats
running_loss, running_mse, running_wsee = 0,0,0
for i in range(len(perm_i)//self.bs):
i_s, i_e = i*self.bs, (i+1)*self.bs
y_true = y_train[perm_i[i_s:i_e]]
x = x_train[perm_i[i_s:i_e]]
y_pred, gamma = self.model( x )
loss, (l,m,w) = self.loss_func(y_pred.view(y_true.shape), y_true, x, x[:,-1])
self.opt_step_handler(loss)
running_loss += l
running_mse += m
running_wsee += w
# update global step
self.global_step += 1
if self.lr_scheduler is not None:
self.lr_scheduler.step() # _step_count
# training logs
trn_log = {'loss': running_loss/(i+1),
'mse': running_mse/(i+1),
'wsee': running_wsee/(i+1)}
self.logging(trn_log, 'train')
# if increment, reset loss etc. information
if not self.epoch%self.ds:
utils.print_update('==> Epoch %d: training avg '%self.epoch +
', '.join(['{}: {:.6f}'.format(k,v) for k,v in trn_log.items()]), pbar)
def predict(self, epoch, loader=None, save=False, pbar=None, return_serial=False, **kwargs):
phase='val' if save else 'test'
self.pbar = pbar
x, y, self.bs = loader
ns, self.nu = y.shape
self.model.eval()
# validation
y_pred, gamma = self.model( x )
yp = y_pred if gamma is None else y_pred[-1]
ga = gamma if gamma is None else gamma[-1]
loss, (l,m,w) = self.loss_func(yp, y, x, x[:,-1])
val_log = {'loss':l, 'mse':m, 'wsee':w} # wsee/mse loss
self.logging(val_log, phase)
if not self.epoch%self.ds:
utils.print_update('==> %s set '%phase + ', '.join(['{}: {:.6}'.format(k,v) for k,v in val_log.items()]) , pbar)
if save:
# if validation, save the model if it results in a new lowest error/loss
track = val_log['loss'] #test_err
if track < self.track_minimize:
# update min err track
self.trackstop = 0
self.track_minimize = track
self.save(epoch) # save model
else:
self.trackstop += 1
if self.trackstop > self.autostop:
self.stop = True
if return_serial:
return (y_pred, gamma), y
else:
return (yp, ga), y
class Simple_Trainer_G(Simple_Trainer):
"""
load all data first; not using pytorch's dataloader
"""
def __init__(self, model, check_path, optimizer, resume=True, **kwargs):
super(Simple_Trainer_G, self).__init__(model, check_path, optimizer, resume, **kwargs)
extract_args = lambda a, k: a if not k in kwargs else kwargs[k]
self.ft_flag = extract_args( False, 'ft_flag' )
self.ei = self.model.ei
def batch_data_handler(self, data):
return self.batch_data_handler_graph(data)
def training_prep(self, epoch, loader):
return self.training_prep_graph(epoch, loader)
# edge index for mini batches
def edge_info_proc(self, edge_index, nu, bs):
shift = torch.Tensor(
np.array([np.arange(bs)*nu,]*nu**2).T.reshape(-1)
).repeat(1, 2).view(2,-1).long().to(edge_index.device)
edge_index_batch = edge_index.repeat(1, bs)+shift #edge_index_tr=edge_index_va
return edge_index_batch
# Train & test functions for a single epoch
def train(self, epoch, loader, pbar=None, **kwargs):
self.pbar = pbar # simple progress bar handling
x_train, y_train, perm_i = self.training_prep(epoch, loader) # set train here
# tracking loss and other stats
running_loss, running_mse, running_wsee = 0,0,0
for i in range(len(perm_i)//self.bs):
# get batch
idx = perm_i[(i*self.bs) : ((i+1)*self.bs)]
y_true = y_train[idx]
x = x_train[idx]
# format inputs and propogate forward
inputs = self.batch_data_handler(x)
y_pred, gamma = self.model(*inputs)
# training step back prop
loss, (l,m,w) = self.loss_func(y_pred.view(y_true.shape), y_true, x, x[...,-1])
self.opt_step_handler(loss)
running_loss += l
running_mse += m
running_wsee += w
# update global step and lr scheduler step
self.global_step += 1
if self.lr_scheduler is not None:
self.lr_scheduler.step()
# check if stuck at a local minimum; if yes, reset the parameters
if not self.ft_flag and np.isclose(running_wsee, 0, rtol=1e-05, atol=1e-08, equal_nan=True):
utils.print_update('WARNING: re-initializing the points...')
utils.reset_model_parameters(self.model)
# training logs
try:
trn_log = {'loss': running_loss/(i+1),
'mse': running_mse/(i+1),
'wsee': running_wsee/(i+1)}
except:
trn_log = {'loss': running_loss,
'mse': running_mse,
'wsee': running_wsee}
self.logging(trn_log, 'train')
# if increment, reset loss etc. information
if not self.epoch%self.ds:
utils.print_update('==> Epoch %d: training avg '%self.epoch +
', '.join(['{}: {:.6f}'.format(k,v) for k,v in trn_log.items()]), pbar)
def predict(self, epoch, loader=None, save=False, pbar=None, **kwargs):
# predict in single batch
phase='val' if save else 'test'
self.pbar = pbar
self.model.eval()
x, y, bs = loader
n, nu = y.shape[0], y.shape[-1]
# ------ validation # HERE ASSUME: SAME GRAPH TOPOLOGY!!! (self.ei DOES NOT CHANGE)
self.nu = nu
self.edge_index_batch = self.edge_info_proc(self.ei, nu, bs)
yp = []
for i in range(int(np.ceil(n/bs))):
i_s, i_e = i*bs, min((i+1)*bs, n)
inputs = self.batch_data_handler(x[i_s: i_e])
yp_, gamma = self.model(*inputs)
if isinstance(yp_, list):
yp_ = yp_[-1]
yp.append(yp_)
yp = torch.cat(yp)
_, (l,m,w) = self.loss_func(yp.view(y.shape), y, x, x[...,-1], cpu=False)
val_log = {'loss':l, 'mse':m, 'wsee':w} # wsee/mse loss
self.logging(val_log, phase)
if not self.epoch%self.ds:
utils.print_update('==> %s set '%phase + ', '.join(['{}: {:.6}'.format(k,v) for k,v in val_log.items()]) , pbar)
if save:
# if validation, save the model if it results in a new lowest error/loss
track = -val_log['wsee']
if track < self.track_minimize:
# update min err track
self.trackstop = 0
self.track_minimize = track
self.save(epoch) # save model
else:
self.trackstop += 1
if self.trackstop > self.autostop:
self.stop = True
return yp, y
class Decay_Seq_Trainer(Simple_Trainer):
"""
for sequentially training usca-mlp with blockwise learning rate decay
"""
def __init__(self, model, check_path, optimizer, resume=True, **kwargs):
super(Decay_Seq_Trainer, self).__init__(model, check_path, optimizer, resume, **kwargs)
extract_args = lambda a, k: a if not k in kwargs else kwargs[k]
self.block_decay = extract_args( .6, 'block_decay' ) # lr decay block-wise
def set_optimizer(self, configs):
return self.set_optimizer_decay(configs)
def opt_step_handler(self, loss, decay):
return self.opt_step_handler_decay( loss, decay)
def requires_grad_blocks(self, bidx, requires_grad):
#set blocks indexed by bidx non-trainable
for i in bidx:
if i==0: # embedding as the same
try:
for parameter in self.model.embedding.parameters():
parameter.requires_grad = requires_grad
except:
pass
# set sca[i] require_grad False
for parameter in self.model.sca[i].parameters():
parameter.requires_grad = requires_grad
def train(self, epoch, b_curr, loader, pbar=None, **kwargs):
self.pbar = pbar # simple progress bar handling
x_train, y_train, perm_i = self.training_prep(epoch, loader) # set train here
db = b_curr[1] - b_curr[0]
ft_flag = True if db>1 else False
# tracking loss and other stats
running_loss, running_mse, running_wsee = 0,0,0
for i in range(len(perm_i)//self.bs):
# get batch
idx = perm_i[(i*self.bs) : ((i+1)*self.bs)]
y_true = y_train[idx]
x = x_train[idx]
# format inputs and propogate forward
inputs = self.batch_data_handler(x)
y_pred, gamma = self.model(*inputs, start=b_curr[0], end=b_curr[1])
# training step back prop
loss, (l,m,w) = self.loss_func(y_pred.view(y_true.shape), y_true, x, x[...,-1])
self.opt_step_handler(loss, decay=self.block_decay)
running_loss += l
running_mse += m
running_wsee += w
# update global step and lr scheduler step
self.global_step += 1
if self.lr_scheduler is not None:
self.lr_scheduler.step()
# check if stuck at a local minimum; if yes, reset the parameters
if np.isclose(running_wsee, 0, rtol=1e-05, atol=1e-08, equal_nan=True):
utils.print_update('WARNING: re-initializing the points...')
utils.reset_model_parameters(self.model)
# training logs
trn_log = {'loss': running_loss/(i+1),
'mse': running_mse/(i+1),
'wsee': running_wsee/(i+1)}
self.logging(trn_log, 'train')
# if increment, reset loss etc. information
if not self.epoch%self.ds:
utils.print_update('==> Epoch %d: training avg '%self.epoch +
', '.join(['{}: {:.6f}'.format(k,v) for k,v in trn_log.items()]), pbar)
def predict(self, epoch, b_curr, loader=None, save=False, pbar=None, **kwargs):
# predict in single batch
phase='val' if save else 'test'
self.pbar = pbar
self.model.eval()
x, y, bs = loader
n, nu = y.shape[0], y.shape[-1]
yp = []
for i in range(int(np.ceil(n/bs))):
i_s, i_e = i*bs, min((i+1)*bs, n)
inputs = self.batch_data_handler(x[i_s: i_e])
yp_, gamma = self.model(*inputs, start=b_curr[0], end=b_curr[1])
if isinstance(yp_, list):
yp_ = yp_[-1]
yp.append(yp_)
yp = torch.cat(yp)
_, (l,m,w) = self.loss_func(yp.view(y.shape), y, x, x[...,-1], cpu=False)
val_log = {'loss':l, 'mse':m, 'wsee':w} # wsee/mse loss
self.logging(val_log, phase)
if not self.epoch%self.ds:
utils.print_update('==> %s set '%phase + ', '.join(['{}: {:.6}'.format(k,v) for k,v in val_log.items()]) , pbar)
if save:
# if validation, save the model if it results in a new lowest error/loss
track = -val_log['wsee'] #test_err
if track < self.track_minimize:
# update min err track
self.trackstop = 0
self.track_minimize = track
self.save(epoch) # save model
else:
self.trackstop += 1
if self.trackstop > self.autostop:
self.stop = True
return yp, y
class Decay_Seq_Trainer_G(Simple_Trainer_G):
"""
for sequentially training usca-gcn with blockwise learning rate decay
"""
def __init__(self, model, check_path, optimizer, resume=True, **kwargs):
super(Decay_Seq_Trainer_G, self).__init__(model, check_path, optimizer, resume, **kwargs)
extract_args = lambda a, k: a if not k in kwargs else kwargs[k]
self.block_decay = extract_args( .6, 'block_decay' ) # lr decay block-wise
self.weight_sharing = False
self.reset_count = 0
def batch_data_handler(self, data):
return self.batch_data_handler_graph(data)
def set_optimizer(self, configs):
return self.set_optimizer_decay(configs)
def opt_step_handler(self, loss, decay):
return self.opt_step_handler_decay( loss, decay)
def training_prep(self, epoch, loader):
return self.training_prep_graph(epoch, loader)
def requires_grad_blocks(self, bidx, requires_grad):
#set blocks indexed by bidx non-trainable
for i in bidx:
if i==0: # embegging as the same
try:
for parameter in self.model.embedding.parameters():
parameter.requires_grad = requires_grad
except:
pass