-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrain.py
1173 lines (1075 loc) · 62.2 KB
/
Train.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
# -*- coding: utf-8 -*-
import matplotlib
matplotlib.use('Agg')
import shutil
import time
from datetime import timedelta
import math
import sys
import os, inspect
import numpy as np
import torch
import torch.utils.data as data_utils
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from AverageMeter import AverageMeter
from Net import Hbnn
from Mail import mail
CURRENTDIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
PARENTDIR = os.path.dirname(CURRENTDIR)
DEBUG = False
# DEBUG = True
# Done: experiment on cifar-100
# TODO: try to implement a multi-gpu version
class Train():
# incrementally add training data class by class
def __init__(self, name, data_pth, batch_size, epochs, num_net, train_net, test_pr_net, train_pr_net, out_cls,
para_ls,
num_features=456, minus=True, factor=1,
a=10e1, b=10e1, c=10e2, resume=False, print_freq=5,
learning_rate=7e-2,
shuffle=True, parallel=False, val_freq=4, decay=-0.55, lr_func=0, tp1=70, tp2=120):
self.name = name
self.data_pth = data_pth
self.minus = minus
self.factor = factor
self.a = a
self.b = b
self.c = c
self.num_net = num_net
self.out_cls = out_cls
self.train_net = train_net
self.train_pr_net = train_pr_net
self.test_pr_net = test_pr_net
self.num_features = num_features
self.batch_size = batch_size
self.epochs = epochs
self.print_freq = print_freq
self.parallel = parallel
self.if_resume = resume
self.hbnn = Hbnn()
self.elbo = list()
# self.learning_rate = learning_rate
# self.decay = decay
self.para_ls = para_ls # an iterator contains the parameters list to be experimented
self.lr_func = lr_func
self.turn_pt_1 = tp1
self.turn_pt_2 = tp2
print(('=> Experiment parameters:\n'
'factor: [{0}]\t a: [{1}]\t b: [{2}]\t c: [{3}]\t'
'batch: [{4}]\t epoch: [{6}]\t lr: [{5}]\n'
'decay: [{8}]\t cls: [{7}]\t class batch: {9}').format(factor, a, b, c, batch_size, learning_rate,
epochs, train_net, decay, out_cls))
def __parameters(self, prec1_ls, best_prec1_all_ls, epoch, net, state_dict, elbos, prec1_train, fail, message,
code, seed):
"""save the model parameters in a dict"""
parameters = {
'name': self.name,
'data_pth': self.data_pth,
'minus': self.minus,
'f': self.factor,
'a': self.a,
'b': self.b,
'c': self.c,
'num_net': self.num_net,
'out_cls': self.out_cls,
'train_net': self.train_net,
'train_pr_net': self.train_pr_net,
'test_pr_net': self.test_pr_net,
'num_features': self.num_features,
'batch_size': self.batch_size,
'epochs': self.epochs,
'print_freq': self.print_freq,
'parallel': self.parallel,
'if_resume': self.if_resume,
'hbnn': self.hbnn,
'learning_rate': self.learning_rate,
'decay': self.decay,
# these are parameters specific to training
'prec1_ls': prec1_ls,
'best_prec1_all_ls': best_prec1_all_ls,
'epoch': epoch,
'net': net,
'state_dict': state_dict,
'elbos': elbos,
'prec_train': prec1_train,
'fail': fail,
'message': message,
'code': code,
'seed': seed
}
return parameters
def __load_data(self, path):
"""load the data"""
print("=> loading data @ '{}'".format(path))
x_train = np.load(path + '/features_train.npy')
y_train = np.load(path + '/targets_train.npy')
x_test = np.load(path + '/features_test.npy')
y_test = np.load(path + '/targets_test.npy')
print("=> loaded data @ '{}'".format(path))
# reshape training data in class order
x_train_ord = x_train.reshape(self.num_net, self.train_pr_net, self.num_features)
y_train_ord = y_train.reshape(self.num_net, self.train_pr_net)
x_test_ord = x_test.reshape(self.num_net, self.test_pr_net, self.num_features)
y_test_ord = y_test.reshape(self.num_net, self.test_pr_net)
return x_train_ord, y_train_ord, x_test_ord, y_test_ord, x_test, y_test
def resume(self, pth, net_ls, dir_txt, val_freq=10, shuf=True, seed=0, start_epoch=0, epoch_trap=40):
if os.path.isfile(pth):
model = Hbnn()
print("=> loading checkpoint '{}'".format(pth))
cp = torch.load(pth)
# start_epoch = checkpoint['epoch']
# best_prec1_ls = checkpoint['best_prec1_ls']
# cls = checkpoint['cls']
# parameters = checkpoint['parameters']
# self.__reset_params()
for i in range(self.num_net):
model.addnet()
model.load_state_dict(cp['state_dict'])
print("=> loaded checkpoint '{}'".format(pth))
model.cuda()
print('training class {}'.format(net_ls))
directory = CURRENTDIR + "/runs/%s/imgs/" % (self.name)
if not os.path.exists(directory):
os.makedirs(directory)
flag_1 = time.time() # timing flag for whole trainning
fail_cnt = 0
self.start_epoch = start_epoch
# prec1_ls = list() # store the accuracy for every single net
# best_prec1_ls = list() # store the best accuracy for every single net
# best_prec1_all_ls = list() # store the best accuracy for the whole net
# prec1_ls = list() # store the accuracy for every single net
# best_prec1_ls = list() # store the best accuracy for every single net
# best_prec1_all_ls = list() # store the best accuracy for the whole net
prec1_ls = np.zeros(self.train_net) # store the accuracy for every single net
best_prec1_ls = np.zeros(self.train_net) # store the best accuracy for every single net
best_prec1_all_ls = np.zeros(self.train_net) # store the best accuracy for the whole net
elbos = np.zeros((self.train_net, self.epochs, 4))
prec1_train = np.zeros((self.train_net, self.epochs))
# model = Hbnn(self.out_cls)
x_train_ord, y_train_ord, x_test_ord, y_test_ord, x_test, y_test = self.__load_data(self.data_pth)
pngs = ['overall_preresume.png', 'single_preresume.png']
prec_ls, prec_all_ls = np.zeros(self.num_net), np.zeros(self.num_net),
for net in range(self.num_net):
x_testing, y_testing = shuffle(x_test_ord[net], y_test_ord[net], random_state=seed)
x_val, y_val = shuffle(x_test[0: (net + 1) * self.test_pr_net],
y_test[0: (net + 1) * self.test_pr_net], random_state=seed)
testing = data_utils.TensorDataset(torch.FloatTensor(x_testing), torch.IntTensor(y_testing))
testing_loader = data_utils.DataLoader(testing, batch_size=self.batch_size, shuffle=shuf)
val = data_utils.TensorDataset(torch.FloatTensor(x_val), torch.IntTensor(y_val))
val_loader = data_utils.DataLoader(val, batch_size=self.batch_size, shuffle=shuf)
prec = self.__validate_net(testing_loader, model, net)
prec_all = self.validate(val_loader, model, net)
prec_ls[net] = prec
prec_all_ls[net] = prec_all
self.plot(range(self.out_cls, (self.num_net + 1) * self.out_cls, self.out_cls), prec_all_ls, name=pngs[0],
caption='overall test accuracy')
self.plot(range(self.out_cls, (self.num_net + 1) * self.out_cls, self.out_cls), prec_ls, name=pngs[1],
caption='single class accuracy')
mail_body = (
'Your training <<{0}>> has been tested before resume, time cost: {1}.\n'
).format(self.name, str(timedelta(seconds=(time.time() - flag_1))).split('.')[0])
print(mail_body)
mail(subject='%s preresume test' % self.name, pngs=pngs, dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
# if_resume the training
for net in net_ls:
flag_2 = time.time() # timing for a single net
# prepare the training data
if shuf:
# data used to training
x_training, y_training = shuffle(x_train_ord[net], y_train_ord[net], random_state=seed)
# data used to test single class
x_testing, y_testing = shuffle(x_test_ord[net], y_test_ord[net], random_state=seed)
# data used to test the whole net
x_val, y_val = shuffle(x_test[0: (net + 1) * self.test_pr_net],
y_test[0: (net + 1) * self.test_pr_net], random_state=seed)
else:
x_training, y_training = x_train_ord[net], y_train_ord[net]
x_testing, y_testing = x_test_ord[net], y_test_ord[net]
x_val, y_val = x_test[0: (net + 1) * self.test_pr_net], y_test[0: (net + 1) * self.test_pr_net]
# cut the data in minibatches and shuffle
training = data_utils.TensorDataset(torch.FloatTensor(x_training), torch.IntTensor(y_training))
training_loader = data_utils.DataLoader(training, batch_size=self.batch_size, shuffle=shuf)
testing = data_utils.TensorDataset(torch.FloatTensor(x_testing), torch.IntTensor(y_testing))
testing_loader = data_utils.DataLoader(testing, batch_size=self.batch_size, shuffle=shuf)
val = data_utils.TensorDataset(torch.FloatTensor(x_val), torch.IntTensor(y_val))
val_loader = data_utils.DataLoader(val, batch_size=self.batch_size, shuffle=shuf)
# add a subnet for new class
best_prec1 = 0 # best accuracy of a single net
best_prec1_all = 0 # best accuracy for the whole network
para_ls = iter(self.para_ls)
self.learning_rate, self.decay = next(para_ls)
# if self.lr_func == 0:
# self.learning_rate, self.decay = next(para_ls)
# if self.lr_func == 1:
# self.learning_rate, self.decay = next(para_ls)
# best_prec1_ls.append(best_prec1)
# best_prec1_all_ls.append(best_prec1_all)
# put the model on GPU, however, the multi-gpu is not conceivable at the moement
if self.parallel:
model_paral = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])
else:
model.cuda()
print('=> Number of trained parameters: {}'.format(
sum([p.data.nelement() for p in model.params_net(net)])))
print('=> Total model parameters increased to: {}'.format(
sum([p.data.nelement() for p in model.parameters()])))
# set the optimizer
optimizer = torch.optim.RMSprop(model.params_net(net), lr=self.learning_rate, weight_decay=0.9)
# segment of class
print(' ' * 50 + ' Network ' + str(net) + ' ' + " " * 50)
# for epoch in range(self.epochs):
epoch = 0
cnt = 0
is_best_all = False
prec1_all = 0
best_prec1_all = 0
while epoch < self.epochs:
lr = self.__adjust_lr(optimizer, epoch)
# train for one epoch
e = self.__train(training_loader, model, optimizer, net, epoch)
elbos[net, epoch] = np.asarray(e)
print('Learning Rate: [{0:.4f}]').format(lr)
# evaluate on validation set on a single class
prec1 = self.__validate_net(testing_loader, model, net, epoch)
prec1_train[net, epoch] = prec1
# evaluate on the single class
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
if is_best:
best_prec1_ls[net] = best_prec1 # store the best prec
# evaluate on the whole net
if (epoch % val_freq == 0 and epoch != 0) or epoch == self.epochs - 1:
prec1_all = self.validate(val_loader, model, net, epoch)
# is_best_all = prec1_all > best_prec1_all
best_prec1_all = max(prec1_all, best_prec1_all)
if is_best_all:
best_prec1_all_ls[net] = best_prec1_all
# judge whether the training has failed
fail, message, code = self.__judge(net, epoch, self.epochs, prec1_train, elbos, epoch_trap=epoch_trap)
if fail:
filename = 'fail_{}_net_{}_epoch_{}'.format(fail_cnt, net, epoch)
self.__save_checkpoint({
'parameters': self.__parameters(prec1_ls, best_prec1_all_ls, epoch, net, model.state_dict(),
elbos, prec1_train, fail, message, code, seed)}, is_best, prec1,
net, filename)
# pngs = ['overall.png', 'single.png', 'prec_net_{}_train.png'.format(net), 'elbo_net_{}_train.png'.format(net)]
pngs = ['prec_net_{}_train_fail_{}.png'.format(net, fail_cnt),
'elbo_net_{}_train_fail_{}.png'.format(net, fail_cnt),
'lr_net_{}_fail_{}.png'.format(net, fail_cnt)]
self.plot(range(1, epoch + 2), prec1_train[net, 0:epoch + 1], name=pngs[0],
caption='net %s train accuracy' % net, x_label='epoch')
self.plot(range(1, epoch + 2), elbos[net, 0:epoch + 1, 0], name=pngs[1],
caption='net %s train elbo' % net,
x_label='epoch', y_label='elbo')
self.__plot_lr(name=pngs[2])
fail_cnt += 1
cnt += 1
try:
# reset the parameters and train again
lr, dc = self.learning_rate, self.decay
self.learning_rate, self.decay = next(para_ls)
# model.reset(net)
# model.cuda()
model_tmp = Hbnn(self.out_cls)
for i in range(self.num_net):
model_tmp.addnet()
model_tmp.load_state_dict(cp['state_dict'])
model_tmp.cuda()
model.hbnn[net] = model_tmp.hbnn[net]
model.mu_gamma_g[net] = model_tmp.mu_gamma_g[net]
model.sigma_gamma_g[net] = model_tmp.sigma_gamma_g[net]
mail_body = (
'Your training <<{0}>> has failed {1}({12}) times in epoch {2}, net {3}, class batch {4}.\n'
'The model has been stored. The parameters have been adjusted and start to train this net again.\n'
'The reason is {5}, the exit code is {6}.\n'
'Total time this time for network {3}: {11}.\n'
'learning rate: [{7}] => [{8}]\t decay: [{9}] => [{10}]\n'
).format(self.name, cnt, epoch, net, self.out_cls, message, code, lr, self.learning_rate,
dc, self.decay, str(timedelta(seconds=(time.time() - flag_2))).split('.')[0], fail_cnt)
print(mail_body)
mail(subject='%s failed' % self.name, pngs=pngs,
dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
epoch = 0
flag_2 = time.time() # timing for a single net
except StopIteration:
mail_body = (
'Your training <<{0}>> has failed {1}({11}) times in epoch {2}, net {3}, class batch {4}.\n'
'The model has been stored. The parameters list has been exhausted and training has stopped.\n'
'Move on to next net {9}.\n'
'The reason is {5}, the exit code is {6}.\n'
'Total time this time for network {3}: {10}.\n'
'learning rate: [{7}] decay: [{8}]\n'
).format(self.name, cnt, epoch, net, self.out_cls, message, code, self.learning_rate,
self.decay, net + 1, str(timedelta(seconds=(time.time() - flag_2))).split('.')[0], fail_cnt)
mail(subject='%s failed' % self.name, pngs=pngs,
dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
print(mail_body)
print ('Total training time for {0} network: {1}.\t').format(net + 1, str(
timedelta(seconds=(time.time() - flag_2))).split(
'.')[0])
epoch = self.epochs
# sys.exit('All the experiments have been failed, please try again!')
# mail(subject='%s failed' % self.name, pngs=pngs,
# dir_pic=CURRENTDIR + "runs/%s/imgs/" % (self.name),
# txts=self.name, dir_txt=CURRENTDIR, info=mail_body)
else:
# next iteration
self.__save_checkpoint({
'parameters': self.__parameters(prec1_ls, best_prec1_all_ls, epoch, net, model.state_dict(),
elbos, prec1_train, fail, message, code, seed)}, is_best, prec1,
net)
is_best_all = False
if epoch % 2 == 0:
print('\n') # segment every two epochs
if epoch == self.epochs - 1:
# prec1_ls.append(prec1)
prec1_ls[net] = prec1
pngs = ['prec_net_{}_train_success.png'.format(net),
'elbo_net_{}_train_success.png'.format(net),
'lr_net_{}_success.png'.format(net)]
# self.plot(net, best_prec1_all_ls, name=pngs[0], caption='overall test accuracy')
# self.plot(net, prec1_ls, name=pngs[1], caption='single class accuracy')
self.plot(range(1, epoch + 2), prec1_train[net, 0:epoch + 1], name=pngs[0],
caption='net %s train accuracy' % net, x_label='epoch')
self.plot(range(1, epoch + 2), elbos[net, 0:epoch + 1, 0], name=pngs[1],
caption='net %s train elbo' % net,
x_label='epoch', y_label='elbo')
self.__plot_lr(name=pngs[2])
mail_body = (
'Your training <<{0}>> has succeeded in net {1}, prec {2} after {3}({5}) times failures.\n'
'The model has been stored, the parameters have been recorded in outpout.\n'
'Total time this time for network {1}: {4}.\n'
).format(self.name, net, prec1, cnt, str(timedelta(seconds=(time.time() - flag_2))).split('.')[0], fail_cnt)
print(mail_body)
mail(subject='%s succeed' % self.name, pngs=pngs, dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
epoch += 1
print ('Total time for network {3}: {2}.\t'
'Best single network accuracy among {0}: {1: .2f}').format(self.epochs, best_prec1,
str(timedelta(seconds=(
time.time() - flag_2))).split(
'.')[0], net)
# print ('Total time for network {3}: {2:.1f} min.\t'
# 'Best single network accuracy among {0}: {1: .2f}').format(self.epochs, best_prec1,
# (time.time() - flag_2) / 60, net)
print ('Overall accuracy after {0} epochs: {1: .2f}, the best is {2: .2f}').format(self.epochs, prec1_all,
best_prec1_all)
print('-' * 50 + ' Network ' + str(net) + ' ' + "-" * 50)
# self.plot(net, best_prec1_all_ls, name='overall', caption='overall test accuracy')
# self.plot(net, prec1_ls, name='single', caption='single class accuracy')
print('\n' * 2) # segment different classes
# print ('Total training time for {1} network: {0:.1f} min.\t').format((time.time() - flag_1) / 60,
# the training has succeeded
print ('Total training time for {0} network: {1}.\t').format(self.train_net, str(
timedelta(seconds=(time.time() - flag_1))).split(
'.')[0])
pngs = ['overall.png', 'single.png']
prec_ls, prec_all_ls = np.zeros(self.num_net), np.zeros(self.num_net),
for net in range(self.num_net):
x_testing, y_testing = shuffle(x_test_ord[net], y_test_ord[net], random_state=seed)
x_val, y_val = shuffle(x_test[0: (net + 1) * self.test_pr_net],
y_test[0: (net + 1) * self.test_pr_net], random_state=seed)
testing = data_utils.TensorDataset(torch.FloatTensor(x_testing), torch.IntTensor(y_testing))
testing_loader = data_utils.DataLoader(testing, batch_size=self.batch_size, shuffle=shuf)
val = data_utils.TensorDataset(torch.FloatTensor(x_val), torch.IntTensor(y_val))
val_loader = data_utils.DataLoader(val, batch_size=self.batch_size, shuffle=shuf)
prec = self.__validate_net(testing_loader, model, net)
prec_all = self.validate(val_loader, model, net)
prec_ls[net] = prec
prec_all_ls[net] = prec_all
self.plot(range(self.out_cls, (net + 2) * self.out_cls, self.out_cls), prec_all_ls, name=pngs[0],
caption='overall test accuracy')
self.plot(range(self.out_cls, (net + 2) * self.out_cls, self.out_cls), prec_ls, name=pngs[1],
caption='single class accuracy')
self.__save_checkpoint({
'parameters': self.__parameters(prec1_ls, best_prec1_all_ls, epoch, net, model.state_dict(),
elbos, prec1_train, fail, message, code, seed)}, is_best=False, prec=0,
net=0, filename='final_success_model')
mail_body = (
'Your training <<{0}>> has finished after {1} times failures.\n'
'The model has been stored, the parameters have been recorded in outpout.\n'
'Total time for network {2}: {3}.\n'
).format(self.name, fail_cnt, len(net_ls), str(timedelta(seconds=(time.time() - flag_1))).split('.')[0])
print(mail_body)
mail(subject='%s finish' % self.name, pngs=pngs, dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
# self.plot(epoch, prec1_train[net], name=pngs[2],
# caption='net {} train accuracy', x_label='epoch')
# self.plot(epoch, elbos[net], name=pngs[3], caption='net {} train elbo',
# x_label='epoch', y_label='elbo') # self.train_net)
# print(('=> Loaded Experiment parameters:\n'
# 'elbo minus: [{0}]\t a: [{1}]\t b: [{2}]\t c: [{3}]\t'
# 'batch: [{4}]\t lr: [{5}]\t epoch: [{6}]\t cls: [{7}]').format(self.minus, self.a, self.b, self.c, self.batch_size, self.learning_rate,
# self.epochs, self.train_net))
def __load_parameters(self, pth):
if os.path.isfile(pth):
cp = torch.load(pth)
try:
self.data_pth = cp['data_pth']
self.minus = cp['minus']
self.factor = cp['f']
self.a = cp['a']
self.b = cp['b']
self.c = cp['c']
self.num_net = cp['num_net']
self.out_cls = cp['out_cls']
self.train_net = cp['train_net']
self.train_pr_net = cp['train_pr_net']
self.test_pr_net = cp['test_pr_net']
self.num_features = cp['num_features']
self.batch_size = cp['batch_size']
self.epochs = cp['epochs']
self.print_freq = cp['print_freq']
self.parallel = cp['parallel']
self.if_resume = cp['if_resume']
self.hbnn = cp['hbnn']
self.learning_rate = cp['learning_rate']
self.decay = cp['decay']
# these are parameters specific to training
prec1_ls = cp['prec1_ls']
best_prec1_all_ls = cp['best_prec1_all_ls']
epoch = cp['epoch']
net = cp['net']
state_dict = cp['state_dict']
elbos = cp['elbos']
prec1_train = cp['prec_train']
fail = cp['fail']
message = cp['message']
code = cp['code']
seed = cp['seed']
except KeyError:
model = cp['hbnn']
def train(self, dir_txt, seed=0, val_freq=10, start_net=0, shuf=True):
"""if_resume the training process from a certain model"""
print('start over training from class 0')
directory = CURRENTDIR + "/runs/%s/imgs/" % (self.name)
if not os.path.exists(directory):
os.makedirs(directory)
flag_1 = time.time() # timing flag for whole trainning
fail_cnt = 0
# prec1_ls = list() # store the accuracy for every single net
# best_prec1_ls = list() # store the best accuracy for every single net
# best_prec1_all_ls = list() # store the best accuracy for the whole net
prec1_ls = np.zeros(self.train_net) # store the accuracy for every single net
best_prec1_ls = np.zeros(self.train_net) # store the best accuracy for every single net
best_prec1_all_ls = np.zeros(self.train_net) # store the best accuracy for the whole net
elbos = np.zeros((self.train_net, self.epochs, 4))
prec1_train = np.zeros((self.train_net, self.epochs))
model = Hbnn(self.out_cls)
x_train_ord, y_train_ord, x_test_ord, y_test_ord, x_test, y_test = self.__load_data(self.data_pth)
# if_resume the training
for net in range(start_net, start_net + self.train_net):
flag_2 = time.time() # timing for a single net
# prepare the training data
if shuf:
# data used to training
x_training, y_training = shuffle(x_train_ord[net], y_train_ord[net], random_state=seed)
# data used to test single class
x_testing, y_testing = shuffle(x_test_ord[net], y_test_ord[net], random_state=seed)
# data used to test the whole net
x_val, y_val = shuffle(x_test[0: (net + 1) * self.test_pr_net],
y_test[0: (net + 1) * self.test_pr_net], random_state=seed)
else:
x_training, y_training = x_train_ord[net], y_train_ord[net]
x_testing, y_testing = x_test_ord[net], y_test_ord[net]
x_val, y_val = x_test[0: (net + 1) * self.test_pr_net], y_test[0: (net + 1) * self.test_pr_net]
# cut the data in minibatches and shuffle
training = data_utils.TensorDataset(torch.FloatTensor(x_training), torch.IntTensor(y_training))
training_loader = data_utils.DataLoader(training, batch_size=self.batch_size, shuffle=shuf)
testing = data_utils.TensorDataset(torch.FloatTensor(x_testing), torch.IntTensor(y_testing))
testing_loader = data_utils.DataLoader(testing, batch_size=self.batch_size, shuffle=shuf)
val = data_utils.TensorDataset(torch.FloatTensor(x_val), torch.IntTensor(y_val))
val_loader = data_utils.DataLoader(val, batch_size=self.batch_size, shuffle=shuf)
# add a subnet for new class
best_prec1 = 0 # best accuracy of a single net
best_prec1_all = 0 # best accuracy for the whole network
para_ls = iter(self.para_ls)
self.learning_rate, self.decay = next(para_ls)
# if self.lr_func == 0:
# self.learning_rate, self.decay = next(para_ls)
# if self.lr_func == 1:
# self.learning_rate, self.decay = next(para_ls)
# best_prec1_ls.append(best_prec1)
# best_prec1_all_ls.append(best_prec1_all)
# if not self.if_resume:
model.addnet()
# put the model on GPU, however, the multi-gpu is not conceivable at the moement
if self.parallel:
model_paral = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])
else:
model.cuda()
print('=> Number of trained parameters: {}'.format(
sum([p.data.nelement() for p in model.params_net(net)])))
print('=> Total model parameters increased to: {}'.format(
sum([p.data.nelement() for p in model.parameters()])))
# set the optimizer
optimizer = torch.optim.RMSprop(model.params_net(net), lr=self.learning_rate, weight_decay=0.9)
# segment of class
print(' ' * 50 + ' Network ' + str(net) + ' ' + " " * 50)
# for epoch in range(self.epochs):
epoch = 0
cnt = 0
is_best_all = False
prec1_all = 0
best_prec1_all = 0
while epoch < self.epochs:
lr = self.__adjust_lr(optimizer, epoch)
# train for one epoch
e = self.__train(training_loader, model, optimizer, net, epoch)
elbos[net, epoch] = np.asarray(e)
print('Learning Rate: [{0:.4f}]').format(lr)
# evaluate on validation set on a single class
prec1 = self.__validate_net(testing_loader, model, net, epoch)
prec1_train[net, epoch] = prec1
# evaluate on the single class
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
if is_best:
best_prec1_ls[net] = best_prec1 # store the best prec
# evaluate on the whole net
if (epoch % val_freq == 0 and epoch != 0) or epoch == self.epochs - 1:
prec1_all = self.validate(val_loader, model, net, epoch)
is_best_all = prec1_all > best_prec1_all
best_prec1_all = max(prec1_all, best_prec1_all)
if is_best_all:
best_prec1_all_ls[net] = best_prec1_all
# judge whether the training has failed
fail, message, code = self.__judge(net, epoch, self.epochs, prec1_train, elbos)
if fail:
filename = 'fail_{}_net_{}_epoch_{}'.format(fail_cnt, net, epoch)
self.__save_checkpoint({
'parameters': self.__parameters(prec1_ls, best_prec1_all_ls, epoch, net, model.state_dict(),
elbos, prec1_train, fail, message, code, seed)}, is_best, prec1,
net, filename)
# pngs = ['overall.png', 'single.png', 'prec_net_{}_train.png'.format(net), 'elbo_net_{}_train.png'.format(net)]
pngs = ['prec_net_{}_train_fail_{}.png'.format(net, fail_cnt),
'elbo_net_{}_train_fail_{}.png'.format(net, fail_cnt),
'lr_net_{}_fail_{}.png'.format(net, fail_cnt)]
self.plot(range(1, epoch + 2), prec1_train[net, 0:epoch + 1], name=pngs[0],
caption='net %s train accuracy' % net, x_label='epoch')
self.plot(range(1, epoch + 2), elbos[net, 0:epoch + 1, 0], name=pngs[1],
caption='net %s train elbo' % net,
x_label='epoch', y_label='elbo')
self.__plot_lr(name=pngs[2])
fail_cnt += 1
cnt += 1
try:
# reset the parameters and train again
lr, dc = self.learning_rate, self.decay
self.learning_rate, self.decay = next(para_ls)
model.reset(net)
model.cuda()
mail_body = (
'Your training <<{0}>> has failed {1}({12}) times in epoch {2}, net {3}, class batch {4}.\n'
'The model has been stored. The parameters have been adjusted and start to train this net again.\n'
'The reason is {5}, the exit code is {6}.\n'
'Total time this time for network {3}: {11}.\n'
'learning rate: [{7}] => [{8}]\t decay: [{9}] => [{10}]\n'
).format(self.name, cnt, epoch, net, self.out_cls, message, code, lr, self.learning_rate,
dc, self.decay, str(timedelta(seconds=(time.time() - flag_2))).split('.')[0], fail_cnt)
print(mail_body)
mail(subject='%s failed' % self.name, pngs=pngs,
dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
epoch = 0
flag_2 = time.time() # timing for a single net
except StopIteration:
mail_body = (
'Your training <<{0}>> has failed {1}({11}) times in epoch {2}, net {3}, class batch {4}.\n'
'The model has been stored. The parameters list has been exhausted and training has stopped.\n'
'Move on to next net {9}.\n'
'The reason is {5}, the exit code is {6}.\n'
'Total time this time for network {3}: {10}.\n'
'learning rate: [{7}] decay: [{8}]\n'
).format(self.name, cnt, epoch, net, self.out_cls, message, code, self.learning_rate,
self.decay, net + 1, str(timedelta(seconds=(time.time() - flag_2))).split('.')[0], fail_cnt)
mail(subject='%s failed' % self.name, pngs=pngs,
dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
print(mail_body)
epoch = self.epochs
# sys.exit('All the experiments have been failed, please try again!')
# mail(subject='%s failed' % self.name, pngs=pngs,
# dir_pic=CURRENTDIR + "runs/%s/imgs/" % (self.name),
# txts=self.name, dir_txt=CURRENTDIR, info=mail_body)
else:
# next iteration
self.__save_checkpoint({
'parameters': self.__parameters(prec1_ls, best_prec1_all_ls, epoch, net, model.state_dict(),
elbos, prec1_train, fail, message, code, seed)}, is_best, prec1,
net)
is_best_all = False
if epoch % 2 == 0:
print('\n') # segment every two epochs
if epoch == self.epochs - 1:
# prec1_ls.append(prec1)
prec1_ls[net] = prec1
pngs = ['prec_net_{}_train_success.png'.format(net),
'elbo_net_{}_train_success.png'.format(net),
'lr_net_{}_success.png'.format(net)]
# self.plot(net, best_prec1_all_ls, name=pngs[0], caption='overall test accuracy')
# self.plot(net, prec1_ls, name=pngs[1], caption='single class accuracy')
self.plot(range(1, epoch + 2), prec1_train[net, 0:epoch + 1], name=pngs[0],
caption='net %s train accuracy' % net, x_label='epoch')
self.plot(range(1, epoch + 2), elbos[net, 0:epoch + 1, 0], name=pngs[1],
caption='net %s train elbo' % net,
x_label='epoch', y_label='elbo')
self.__plot_lr(name=pngs[2])
mail_body = (
'Your training <<{0}>> has succeeded in net {1}, prec {2} after {3}({5}) times failures.\n'
'The model has been stored, the parameters have been recorded in outpout.\n'
'Total time this time for network {1}: {4}.\n'
).format(self.name, net, prec1, cnt, str(timedelta(seconds=(time.time() - flag_2))).split('.')[0], fail_cnt)
print(mail_body)
mail(subject='%s succeed' % self.name, pngs=pngs, dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
epoch += 1
print ('Total time for network {3}: {2}.\t'
'Best single network accuracy among {0}: {1: .2f}').format(self.epochs, best_prec1,
str(timedelta(seconds=(
time.time() - flag_2))).split(
'.')[0], net)
# print ('Total time for network {3}: {2:.1f} min.\t'
# 'Best single network accuracy among {0}: {1: .2f}').format(self.epochs, best_prec1,
# (time.time() - flag_2) / 60, net)
print ('Overall accuracy after {0} epochs: {1: .2f}, the best is {2: .2f}').format(self.epochs, prec1_all,
best_prec1_all)
print('-' * 50 + ' Network ' + str(net) + ' ' + "-" * 50)
# self.plot(net, best_prec1_all_ls, name='overall', caption='overall test accuracy')
# self.plot(net, prec1_ls, name='single', caption='single class accuracy')
print('\n' * 2) # segment different classes
# print ('Total training time for {1} network: {0:.1f} min.\t').format((time.time() - flag_1) / 60,
# the training has succeeded
print ('Total training time for {0} network: {1}.\t').format(self.train_net, str(
timedelta(seconds=(time.time() - flag_1))).split(
'.')[0])
pngs = ['overall.png', 'single.png']
fail, message, code = False, 'success', 0
self.plot(range(self.out_cls, (net + 2) * self.out_cls, self.out_cls), best_prec1_all_ls, name=pngs[0],
caption='overall test accuracy')
self.plot(range(self.out_cls, (net + 2) * self.out_cls, self.out_cls), prec1_ls, name=pngs[1],
caption='single class accuracy')
self.__save_checkpoint({
'parameters': self.__parameters(prec1_ls, best_prec1_all_ls, epoch, net, model.state_dict(),
elbos, prec1_train, fail, message, code, seed)}, is_best, prec1,
net, filename='final_success_model')
mail_body = (
'Your training <<{0}>> has finished after {1} times failures.\n'
'The model has been stored, the parameters have been recorded in outpout.\n'
'Total time: {2}.\n'
).format(self.name, fail_cnt, str(timedelta(seconds=(time.time() - flag_1))).split('.')[0])
print(mail_body)
mail(subject='%s finish' % self.name, pngs=pngs, dir_pic=directory,
txts=[self.name + '.txt'], dir_txt=dir_txt, info=mail_body)
# self.plot(epoch, prec1_train[net], name=pngs[2],
# caption='net {} train accuracy', x_label='epoch')
# self.plot(epoch, elbos[net], name=pngs[3], caption='net {} train elbo',
# x_label='epoch', y_label='elbo') # self.train_net)
def plot(self, x, precls, name, caption, y_label='accuracy', x_label='class'):
"""draw the pic and save"""
directory = CURRENTDIR + "/runs/%s/imgs/" % (self.name)
if not os.path.exists(directory):
os.makedirs(directory)
# plt.plot(range(1 * self.out_cls, (net + 2) * self.out_cls, self.out_cls), precls, '.r--')
plt.plot(x, precls, '.r--')
ax = plt.gca()
ax.yaxis.grid(True, linestyle='--')
ax.xaxis.grid(True, linestyle='--')
plt.ylabel(y_label)
plt.xlabel(x_label)
plt.suptitle(caption)
# img = dir_pic + str(net) + '.png'
img = directory + name
plt.savefig(img, bbox_inches='tight')
plt.close()
print('picture saved @ %s' % img)
def __resume(self):
"""if_resume the training process from a certain model"""
x_train_ord, y_train_ord, x_test_ord, y_test_ord, x_test, y_test = self.__load_data(self.data_pth)
# initialize the model
model = self.hbnn
# if there is saved model, load it.
if self.if_resume:
test = data_utils.TensorDataset(torch.FloatTensor(x_test), torch.IntTensor(y_test))
val_loader = data_utils.DataLoader(test, batch_size=self.batch_size, shuffle=shuf)
stop_epoch, best_prec_ls, start_cls, model = self.load_model(model=model, resume=self.if_resume,
val_loader=val_loader)
else:
start_cls = 0
# if_resume the training
for cls in range(start_cls, self.train_net):
# shuffle the training data
if shuf:
x_training, y_training = shuffle(x_train_ord[cls], y_train_ord[cls], random_state=0)
else:
x_training = x_train_ord[cls]
y_training = y_train_ord[cls]
# cut the data in minibatches and shuffle
training = data_utils.TensorDataset(torch.FloatTensor(x_training), torch.IntTensor(y_training))
training_loader = data_utils.DataLoader(training, batch_size=self.batch_size, shuffle=shuf)
# add new net
if self.if_resume and cls == start_cls:
best_prec1_ls = best_prec_ls
best_prec1 = best_prec_ls[start_cls]
start_epoch = stop_epoch
print('if_resume training from class {0}, epoch {1}').format(start_cls, start_epoch)
print('=> Number of trained parameters: {}'.format(
sum([p.data.nelement() for p in model.params_net(cls)])))
print('=> Number of model parameters: {}'.format(
sum([p.data.nelement() for p in model.parameters()])))
# print('=> Number of model parameters by myself: {}'.format(
# sum([p.data.nelement() for p in model.params()])))
else:
best_prec1 = 0
best_prec1_ls.append(best_prec1)
start_epoch = 0
model.addnet()
model = model.cuda()
print('if_resume training class {0}, epoch {1}').format(cls, start_epoch)
print('=> Number of trained parameters: {}'.format(
sum([p.data.nelement() for p in model.params_net(cls)])))
print('=> Number of model parameters: {}'.format(
sum([p.data.nelement() for p in model.parameters()])))
# print('=> Number of model parameters by myself: {}'.format(
# sum([p.data.nelement() for p in model.params()])))
# set the optimizer
optimizer = torch.optim.RMSprop(model.params_net(cls), lr=self.learning_rate, weight_decay=0.9)
# segment of class
print(' ' * 50 + ' Class ' + str(cls) + ' ' + " " * 50)
for epoch in range(start_epoch, self.epochs):
self.__adjust_lr(optimizer, epoch)
# train for one epoch
self.__train(training_loader, model, optimizer, cls, epoch)
# evaluate on validation set on a single class
prec1 = self.__validate_net(val_loader, model, cls, epoch)
# remember best prec@1 and save checkpoint
is_best = prec1 > best_prec1
if is_best:
best_prec1_ls[cls] = best_prec1 # store the best prec
best_prec1 = max(prec1, best_prec1)
self.__save_checkpoint({
'epoch': epoch,
'state_dict': model.state_dict(),
'best_prec1_ls': best_prec1_ls,
'cls': cls
}, is_best, prec1, cls)
print ('Best accuracy among {0}: {1: .2f}').format(self.epochs, best_prec1)
print('\n') # segment different classes
def __train(self, training_loader, model, optimizer, net, epoch=0):
batch_time = AverageMeter()
fwd_time = AverageMeter()
losses = AverageMeter()
data_term = AverageMeter()
entro = AverageMeter()
cross = AverageMeter()
top1 = AverageMeter()
elbo = list()
# switch to train mode
model.train()
# if_resume the timing
for i, (input, target) in enumerate(training_loader):
flag_1 = time.time()
target = target.cuda(async=True)
target = target - self.out_cls * net
input = input.cuda()
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
# get the elbo
loss, d, e, c = model.elbo(input_var, target_var, minus=self.minus, factor=self.factor, a=self.a, b=self.b,
c=self.c,
train_net=net, batchs=len(training_loader))
flag_2 = time.time()
output = model.forward_single_net(input_var, net)
fwd_time.update(time.time() - flag_2)
prec1 = self.__accuracy_net(output.data, target, num=net, topk=(1,))[0]
# update the data
losses.update(loss.data[0], input.size(0))
data_term.update(d.data[0], input.size(0))
entro.update(e.data[0], input.size(0))
cross.update(c.data[0], input.size(0))
top1.update(prec1[0], input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - flag_1)
# end = time.time()
# print the result
if i == 0:
print('-' * 50 + ' Training ' + '-' * 50)
if i % self.print_freq == 0 or i == len(training_loader) - 1:
print(
'Class: [{3}/{4}][{5}/{6}]\t'
'Epoch: [{0}][{1}/{2}]\t'
'Time: {batch_time.val:.1f} ({batch_time.avg:.1f}|{fwd_time.avg:.1f})\t'
'Loss: {loss.val:.2f} ({loss.avg:.1f})\t'
'Prec@1: {top1.val:.2f} ({top1.avg:.2f})'.format(
epoch + 1, i + 1, len(training_loader), net + 1, self.train_net, (net + 1) * self.out_cls,
self.train_net * self.out_cls,
batch_time=batch_time, fwd_time=fwd_time,
loss=losses, top1=top1))
print ('data: {0:.1f}({3:.1f})\t cross: {2:.1f}({5:.1f})\t entro: {1:.1f}({4:.1f})\t').format(
data_term.val, entro.val,
cross.val, data_term.avg,
entro.avg, cross.avg)
elbo.append([losses.avg, data_term.avg, entro.avg, cross.avg])
return elbo
def __validate_net(self, val_loader, model, net, epoch=0):
"""Perform validation on the validation set"""
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
# switch to evaluate mode
model.eval()
num_batches = len(val_loader) # the number of batches
for i, (input, target) in enumerate(val_loader):
start = time.time()
target = target.cuda(async=True)
target = target - net * self.out_cls
input = input.cuda()
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
# compute output
output = model.forward_single_net(input_var, net)
# get the loss
# loss, d, e, c = model.elbo(input_var, target_var, train_net=net, factor=self.factor, batchs=num_batches)
# measure accuracy and record loss
prec1 = self.__accuracy_net(output.data, target, net, topk=(1,))[0]
# losses.update(loss.data[0], input.size(0))
top1.update(prec1[0], input.size(0))
# measure elapsed time
batch_time.update(time.time() - start)
end = time.time()
# print the result
if i == 0:
print('-' * 50 + ' Testing ' + '-' * 50)
if i % self.print_freq == 0 or i == len(val_loader) - 1:
print(
'Class: [{3}/{4}][{5}/{6}]\t'
'Epoch: [{0}][{1}/{2}]\t'
'Time: {batch_time.val:.1f} ({batch_time.avg:.1f})\t'
# 'Loss: {loss.val:.2f} ({loss.avg:.1f})\t'
'Prec@1: {top1.val:.2f} ({top1.avg:.2f})'.format(
epoch + 1, i + 1, len(val_loader), net + 1, self.train_net, (net + 1) * self.out_cls,
self.train_net * self.out_cls,
batch_time=batch_time,
loss=losses, top1=top1))
print(' * Prec@1 {top1.avg:.2f}'.format(top1=top1))
return top1.avg
def __accuracy_cls(self, output, target, cls, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
target = target.eq(cls).long()
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = -1 * pred.eq(target.view(1, -1).expand_as(pred)) + 1
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def __accuracy_net(self, output, target, num, topk=(1,)):
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def __adjust_lr(self, optimizer, epoch):
"""adjust learning rate according to epoch"""
# a = 7e-2
a = self.learning_rate
# self.turn_pt_1 = 70
# self.turn_pt_2 = 120
# lr = a * (b + (epoch + 1)) ** (- 0.55)
# lr = a * (b + (epoch + 1)) ** (-0.55)
if self.lr_func == 0:
b = - 0.5
lr = a * (b + (epoch + self.start_epoch)) ** self.decay
if self.lr_func == 1:
b = self.decay
# turn_point_1 = 100
turn_point_1 = self.turn_pt_1
turn_point_2 = self.turn_pt_2
lr = a * b ** (epoch // turn_point_1) * b ** (epoch // turn_point_2)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def __plot_lr(self, name, plot_range=200):
a = self.learning_rate
x = np.arange(plot_range)
if self.lr_func == 0:
b = - 0.5
y = a * (b + (x + self.start_epoch)) ** self.decay
if self.lr_func == 1:
b = self.decay
# turn_point_1 = 100
turn_point_1 = self.turn_pt_1
turn_point_2 = self.turn_pt_2
y = a * b ** (x // turn_point_1) * b ** (x // turn_point_2)