-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivae_ardae.py
1320 lines (1209 loc) · 59.2 KB
/
ivae_ardae.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 os
import sys
import argparse
import datetime
import time
import math
import glob
import numpy as np
import torch
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
import torchcontrib
import torchvision.utils as vutils
import datasets as dset
import models as net
from utils import Adam
from utils import StepLR
from models.aux import aux_loss_for_grad
from utils import expand_tensor
from utils import load_checkpoint, save_checkpoint, load_end_iter, logging, get_time, annealing_func, EndIterError
from utils import convert_npimage_torchimage, get_scatter_plot, get_quiver_plot, get_data_for_quiver_plot, get_prob_from_energy_func_for_vis, get_imshow_plot, get_2d_histogram_plot, get_grid_image
from tensorboardX import SummaryWriter
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', default='swissroll',
choices=['swissroll', '25gaussians', 'sbmnist', 'dbmnist', 'dbmnist-val5k'],
help='dataset: swissroll | 25gaussians | sbmnist | dbmnist | dbmnist-val5k ')
# net architecture
parser.add_argument('--model', default='mlp-concat',
choices=['mlp-concat',
'mnist-concat',
'mnist-conv',
'resconv', 'resconvct', 'resconv-res', 'resconvct-res', 'resconv-res2', 'resconvct-res2', 'resconvct-res3', 'resconvct-res4',
'auxmlp',
'auxmnist',
'auxconv',
'auxresconv', 'auxresconvct', 'auxresconv-clip', 'auxresconvct-clip',
],
help='model: mlp-concat '
'| mnist-concat '
'| mnist-conv '
'| resconv | resconvct | resconv-res | resconvct-res '
'| auxmlp '
'| auxmnist '
'| auxconv '
'| auxresconv | auxresconvct | auxresconv-clip | auxresconvct-clip '
)
parser.add_argument('--model-z-dim', type=int, default=2,
help='latent variable dim of encoder.')
parser.add_argument('--model-h-dim', type=int, default=128,
help='hidden dim of enc/dec networks.')
parser.add_argument('--model-n-dim', type=int, default=2,
help='noise source dim of encoder.')
parser.add_argument('--model-n-layers', type=int, default=2,
help='number of hidden layers.')
parser.add_argument('--model-nonlin', default='relu',
help='activation function')
parser.add_argument('--model-clip-z0-logvar', default='none',
choices=['none'],
help='clip logvar of z0 in encoder (hierarchical encoder)')
parser.add_argument('--model-clip-z-logvar', default='none',
choices=['none'],
help='clip logvar of z in encoder (hierarchical encoder)')
parser.add_argument('--cdae', default='mlp',
choices=['mlp', 'mlp-res', 'mlp-grad'],
help='cdae: mlp | mlp-res | mlp-grad')
parser.add_argument('--cdae-h-dim', type=int, default=128,
help='hidden dim of denoising autoencoder network.')
parser.add_argument('--cdae-n-layers', type=int, default=2,
help='number of hidden layers.')
parser.add_argument('--cdae-nonlin', default='relu',
help='activation function')
parser.add_argument('--cdae-ctx-type', default='data',
choices=['data', 'lt0', 'hidden1a',],
help='flag context type for cdae')
# conditional dae
parser.add_argument('--std-scale', type=float, default=1.0,
help='std scaling for denoising autoencoder')
parser.add_argument('--delta', type=float, default=1,
help='prior variance for std sampling distribution')
parser.add_argument('--num-cdae-updates', type=int, default=1,
help='number of cdae updates')
# type of data
parser.add_argument('--nheight', type=int, default=1,
help='the height / width of the input to network')
parser.add_argument('--nchannels', type=int, default=2,
help='number of channels in input')
# training
parser.add_argument('--m-lr', type=float, default=0.0001,
help='initial learning rate')
parser.add_argument('--d-lr', type=float, default=0.0001,
help='initial learning rate')
parser.add_argument('--d-lr-min', type=float, default=0.0001,
help='min learning rate')
parser.add_argument('--epochs', type=int, default=30,
help='upper epoch limit')
parser.add_argument('--train-batch-size', type=int, default=1024, metavar='N',
help='input batch size for training (default: 20)')
parser.add_argument('--eval-batch-size', type=int, default=1024, metavar='N',
help='input batch size for test (default: 10)')
parser.add_argument('--start-epoch', type=int, default=1,
help='start epoch')
parser.add_argument('--start-batch-idx', type=int, default=0,
help='start batch-idx')
parser.add_argument('--train-nz-cdae', type=int, default=1, metavar='N',
help='the number of z samples per data point (default: 1)')
parser.add_argument('--train-nz-model', type=int, default=1, metavar='N',
help='the number of z samples per data point (default: 1)')
parser.add_argument('--train-nstd-cdae', type=int, default=1, metavar='N',
help='the number of z samples per data point (default: 1)')
parser.add_argument('--m-optimizer', default='adam',
choices=['sgd', 'adam', 'amsgrad', 'rmsprop'],
help='optimization methods: sgd | adam | amsgrad | rmsprop ')
parser.add_argument('--m-beta1', type=float, default=0.5, help='beta1 for adam or adam-amsgrad. default=0.5') # sgd or rmsprop
parser.add_argument('--m-momentum', type=float, default=0.5, help='momentum for std or rmsprop. default=0.5') # adam
parser.add_argument('--d-optimizer', default='adam',
choices=['sgd', 'adam', 'amsgrad', 'rmsprop'],
help='optimization methods: sgd | adam | amsgrad | rmsprop ')
parser.add_argument('--d-beta1', type=float, default=0.5, help='beta1 for adam or adam-amsgrad. default=0.5') # sgd or rmsprop
parser.add_argument('--d-momentum', type=float, default=0.5, help='momentum for std or rmsprop. default=0.5') # adam
# training (beta, eta, and lmbd annealing)
parser.add_argument('--beta-init', type=float, default=1.0,
help='initial beta value for beta annealing')
parser.add_argument('--beta-fin', type=float, default=1.0,
help='final beta value for beta annealing')
parser.add_argument('--beta-annealing', type=float, default=None,
help='interval to annealing beta')
parser.add_argument('--eta-init', type=float, default=0.,
help='initial eta value for eta annealing')
parser.add_argument('--eta-fin', type=float, default=0.,
help='final eta value for eta annealing')
parser.add_argument('--eta-annealing', type=float, default=None,
help='interval to annealing eta')
parser.add_argument('--lmbd-init', type=float, default=0.,
help='initial lmbd value for lmbd annealing')
parser.add_argument('--lmbd-fin', type=float, default=0.,
help='final lmbd value for lmbd annealing')
parser.add_argument('--lmbd-annealing', type=float, default=None,
help='interval to annealing lmbd')
# eval
parser.add_argument('--iws-samples', type=int, default=512,
help='number of iwae samples (default: 512)')
parser.add_argument('--m-weight-avg', default='none',
choices=['none', 'swa', 'polyak'],
help='weight average method (evaluate): swa | polyak')
parser.add_argument('--m-weight-avg-start', type=int, default=1000,
help='weight average method (evaluate): swa | polyak')
parser.add_argument('--m-weight-avg-decay', type=float, default=0.998,
help='weight average method (evaluate): swa | polyak')
# final mode
parser.add_argument('--train-mode', default='train',
choices=['train', 'final'],
help='training mode: train | final')
# log
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--log-interval', type=int, default=100,
help='log print-out interval (iter)')
parser.add_argument('--vis-interval', type=int, default=1000,
help='visualization interval (iter)')
parser.add_argument('--eval-iws-interval', type=int, default=1000,
help='evaluation interval (iter)')
parser.add_argument('--ckpt-interval', type=int, default=10000,
help='checkpoint interval (iter)')
parser.add_argument('--sav-interval', type=int, default=0,
help='model save interval (epoch)')
# save
parser.add_argument('--resume', dest='resume', action='store_true', default=True,
help='flag to resume the experiments')
parser.add_argument('--no-resume', dest='resume', action='store_false', default=True,
help='flag to resume the experiments')
parser.add_argument('--cache', default=None, help='path to cache')
parser.add_argument('--experiment', default=None, help='name of experiment')
parser.add_argument('--exp-num', type=int, default=None,
help='experiment number')
# parse arguments
opt = parser.parse_args()
# preprocess arguments
opt.cuda = not opt.no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if opt.cuda else "cpu")
opt.best_val_loss = None
if opt.beta_annealing is None or opt.beta_annealing < 1:
opt.beta_annealing = None
if opt.eta_annealing is None or opt.eta_annealing < 1:
opt.eta_annealing = None
if opt.lmbd_annealing is None or opt.lmbd_annealing < 1:
opt.lmbd_annealing = None
# generate cache folder
if opt.cache is None:
opt.cache = 'experiments'
if opt.experiment is None:
opt.experiment = '-'.join(['m{}-mz{}-mh{}-mn{}-mnh{}-ma{}'.format(
opt.model,
opt.model_z_dim,
opt.model_h_dim,
opt.model_n_dim,
opt.model_n_layers,
'sfp' if opt.model_nonlin == 'softplus' else opt.model_nonlin,
#'-mz0cl{}-mzcl{}'.format(
# opt.model_clip_z0_logvar,
# opt.model_clip_z_logvar,
#)
),
'd{}-dh{}-dnh{}-da{}-dct{}'.format(
opt.cdae,
opt.cdae_h_dim,
opt.cdae_n_layers,
'sfp' if opt.cdae_nonlin == 'softplus' else opt.cdae_nonlin,
opt.cdae_ctx_type,
),
'm{}-bt1{}'.format(opt.m_optimizer, opt.m_beta1) if opt.m_optimizer in ['adam', 'amsgrad'] else 'm{}-mt{}'.format(opt.m_optimizer, opt.m_momentum),
'mlr{}'.format(opt.m_lr),
'd{}-bt1{}'.format(opt.d_optimizer, opt.d_beta1) if opt.d_optimizer in ['adam', 'amsgrad'] else 'd{}-mt{}'.format(opt.d_optimizer, opt.d_momentum),
'dlr{}'.format(opt.d_lr),
'tbs{}'.format(opt.train_batch_size),
'nd{}'.format(opt.num_cdae_updates),
'mwa{}{}'.format(opt.m_weight_avg,
'-was{}-wad{}'.format(opt.m_weight_avg_start, opt.m_weight_avg_decay) if opt.m_weight_avg != 'none' else '',
),
'binit{}-bfin{}-bann{:d}'.format(
opt.beta_init if opt.beta_init != opt.beta_fin else 1.,
opt.beta_fin,
int(opt.beta_annealing) if opt.beta_annealing is not None and opt.beta_init != opt.beta_fin else 0,
),
#'etinit{}-etfin{}-etann{:d}'.format(
# opt.eta_init,
# opt.eta_fin,
# int(opt.eta_annealing) if opt.eta_annealing is not None else 0),
#'ldinit{}-ldfin{}-ldann{:d}'.format(
# opt.lmbd_init,
# opt.lmbd_fin,
# int(opt.lmbd_annealing) if opt.lmbd_annealing is not None else 0),
'ssc{}'.format(opt.std_scale),
'del{}'.format(opt.delta),
'nzc{}{}'.format(
opt.train_nz_cdae,
'-nzs{}'.format(opt.train_nstd_cdae) if opt.train_nstd_cdae > 1 else '',
),
'nzm{}'.format(opt.train_nz_model),
'{}'.format(opt.exp_num if opt.exp_num else 0), #'exp{}'.format(opt.exp_num if opt.exp_num else 0),
])
opt.path = os.path.join(opt.cache, opt.experiment)
if opt.resume:
listing = glob.glob(opt.path+'-19*') + glob.glob(opt.path+'-20*')
if len(listing) == 0:
opt.path = '{}-{}'.format(opt.path, get_time())
else:
path_sorted = sorted(listing, key=lambda x: datetime.datetime.strptime(x, opt.path+'-%y%m%d-%H:%M:%S'))
opt.path = path_sorted[-1]
pass
else:
opt.path = '{}-{}'.format(opt.path, get_time())
os.system('mkdir -p {}'.format(opt.path))
# print args
logging(str(opt), path=opt.path)
# init tensorboard
writer = SummaryWriter(opt.path)
# final mode
if opt.train_mode == 'final':
opt.end_iter = load_end_iter(opt, filename='best-model-checkpoint.pth.tar', device=device)
else:
opt.end_iter = None
# init dataset
train_loader, val_loader, test_loader, _ = dset.get_dataset(opt.dataset, opt.train_batch_size, opt.eval_batch_size, opt.cuda, final_mode=(opt.train_mode=='final'))
# init model
if opt.model == 'mlp-concat':
model = net.ToyIPVAE(
input_dim=opt.nchannels*opt.nheight*opt.nheight,
noise_dim=opt.model_n_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
nonlinearity=opt.model_nonlin,
enc_type='concat',
z_dim=opt.model_z_dim,
).to(device)
elif opt.model == 'mnist-concat':
model = net.MNISTIPVAE(
input_dim=opt.nchannels*opt.nheight*opt.nheight,
noise_dim=opt.model_n_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
nonlinearity=opt.model_nonlin,
enc_type='concat',
z_dim=opt.model_z_dim,
).to(device)
elif opt.model == 'mnist-conv':
model = net.ConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
).to(device)
elif opt.model == 'resconv':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
enc_type='mlp',
).to(device)
elif opt.model == 'resconvct':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
enc_type='mlp',
).to(device)
elif opt.model == 'resconv-res':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
enc_type='res-wn-mlp',
).to(device)
elif opt.model == 'resconvct-res':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
enc_type='res-wn-mlp',
).to(device)
elif opt.model == 'resconv-res2':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
enc_type='res-mlp',
).to(device)
elif opt.model == 'resconvct-res2':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
enc_type='res-mlp',
).to(device)
elif opt.model == 'resconv-res3':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
enc_type='res-wn-mlp-lin',
).to(device)
elif opt.model == 'resconvct-res3':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
enc_type='res-wn-mlp-lin',
).to(device)
elif opt.model == 'resconv-res4':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
enc_type='res-mlp-lin',
).to(device)
elif opt.model == 'resconvct-res4':
model = net.ResConvIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
noise_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
enc_type='res-mlp-lin',
).to(device)
elif opt.model == 'auxmlp':
model = net.ToyAuxIPVAE(
input_dim=opt.nchannels*opt.nheight*opt.nheight,
noise_dim=opt.model_n_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
nonlinearity=opt.model_nonlin,
enc_type='simple',
z_dim=opt.model_z_dim,
clip_z0_logvar=opt.model_clip_z0_logvar,
clip_z_logvar=opt.model_clip_z_logvar,
).to(device)
elif opt.model == 'auxmnist':
model = net.MNISTAuxIPVAE(
input_dim=opt.nchannels*opt.nheight*opt.nheight,
noise_dim=opt.model_n_dim,
h_dim=opt.model_h_dim,
num_hidden_layers=opt.model_n_layers,
nonlinearity=opt.model_nonlin,
enc_type='simple',
z_dim=opt.model_z_dim,
clip_z0_logvar=opt.model_clip_z0_logvar,
clip_z_logvar=opt.model_clip_z_logvar,
).to(device)
elif opt.model == 'auxconv':
assert opt.model_h_dim == 0
assert opt.model_n_layers == 0
assert opt.model_clip_z0_logvar == 'none'
assert opt.model_clip_z_logvar == 'none'
model = net.MNISTConvAuxIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z0_dim=opt.model_n_dim,
z_dim=opt.model_z_dim,
nonlinearity=opt.model_nonlin,
).to(device)
elif opt.model == 'auxresconv':
assert opt.model_h_dim == 0
assert opt.model_n_layers == 0
assert opt.model_clip_z0_logvar == 'none'
assert opt.model_clip_z_logvar == 'none'
model = net.MNISTResConvAuxIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
c_dim=450,
z0_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
).to(device)
elif opt.model == 'auxresconvct':
assert opt.model_h_dim == 0
assert opt.model_n_layers == 0
assert opt.model_clip_z0_logvar == 'none'
assert opt.model_clip_z_logvar == 'none'
model = net.MNISTResConvAuxIPVAE(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
c_dim=450,
z0_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
).to(device)
elif opt.model == 'auxresconv-clip':
assert opt.model_h_dim == 0
assert opt.model_n_layers == 0
assert opt.model_clip_z0_logvar == 'none'
assert opt.model_clip_z_logvar == 'none'
model = net.MNISTResConvAuxIPVAEClipped(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
c_dim=450,
z0_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=False,
).to(device)
elif opt.model == 'auxresconvct-clip':
assert opt.model_h_dim == 0
assert opt.model_n_layers == 0
assert opt.model_clip_z0_logvar == 'none'
assert opt.model_clip_z_logvar == 'none'
model = net.MNISTResConvAuxIPVAEClipped(
input_height=opt.nheight,
input_channels=opt.nchannels,
z_dim=opt.model_z_dim,
c_dim=450,
z0_dim=opt.model_n_dim,
nonlinearity=opt.model_nonlin,
do_center=True,
).to(device)
else:
raise NotImplementedError
logging(str(model), path=opt.path)
''' temporary '''
_prob = get_prob_from_energy_func_for_vis(model.energy_func, num=256)
_gtlatent = get_imshow_plot(_prob, val=6 if opt.dataset in ['mnist', 'sbmnist', 'dbmnist', 'dbmnist-val5k'] else 4, use_grid=False)
#img = convert_npimage_torchimage(_img)
#writer.add_image('train/latent', img.float(), 0)
''' --------- '''
# init optimizer
if opt.m_optimizer == 'sgd':
model_optimizer = optim.SGD(model.parameters(), lr=opt.m_lr)
elif opt.m_optimizer == 'adam':
model_optimizer = Adam(model.parameters(), lr=opt.m_lr, betas=(opt.m_beta1, 0.999))
elif opt.m_optimizer == 'amsgrad':
model_optimizer = Adam(model.parameters(), lr=opt.m_lr, betas=(opt.m_beta1, 0.999), amsgrad=True)
elif opt.m_optimizer == 'rmsprop':
model_optimizer = optim.RMSprop(model.parameters(), lr=opt.m_lr, momentum=opt.d_momentum)
else:
raise NotImplementedError('unknown optimizer: {}'.format(opt.model_optimizer))
model_scheduler = None
# init weight avg
if opt.m_weight_avg == 'polyak':
model_optimizer = torchcontrib.optim.Polyak(model_optimizer, polyak_start=opt.m_weight_avg_start, polyak_freq=1, polyak_decay=opt.m_weight_avg_decay)
elif opt.m_weight_avg == 'swa':
model_optimizer = torchcontrib.optim.SWA(model_optimizer, swa_start=opt.m_weight_avg_start, swa_freq=1)
else:
pass
# init cdae
if opt.cdae_ctx_type == 'data':
context_dim = opt.nchannels*opt.nheight*opt.nheight
elif opt.cdae_ctx_type in ['lt0']:
context_dim = opt.model_z_dim
elif opt.cdae_ctx_type in ['hidden1a']:
if opt.model in ['auxmlp', 'auxmnist']:
context_dim = opt.model_h_dim*2
elif opt.model in ['auxconv']:
context_dim = 800*2
elif opt.model in ['auxresconv', 'auxresconvct', 'auxresconv-clip', 'auxresconvct-clip']:
context_dim = 450#*2
else:
context_dim = opt.model_h_dim
else:
raise NotImplementedError
if opt.cdae == 'mlp-res':
cdae = net.MLPResCARDAE(
input_dim=opt.model_z_dim,
context_dim=context_dim,
std=1.,
h_dim=opt.cdae_h_dim,
num_hidden_layers=opt.cdae_n_layers,
nonlinearity=opt.cdae_nonlin,
noise_type='gaussian',
enc_ctx=True,
enc_input=True,
).to(device)
elif opt.cdae == 'mlp-grad':
cdae = net.MLPGradCARDAE(
input_dim=opt.model_z_dim,
context_dim=context_dim,
std=1.,
h_dim=opt.cdae_h_dim,
num_hidden_layers=opt.cdae_n_layers,
nonlinearity=opt.cdae_nonlin,
noise_type='gaussian',
enc_ctx=True,
enc_input=True,
).to(device)
else:
raise NotImplementedError
logging(str(cdae), path=opt.path)
# init params
cdae_params = list(cdae.parameters())
if opt.cdae_ctx_type in ['data', 'lt0', 'hidden1a']:
pass
else:
raise NotImplementedError
# init optimizer
if opt.d_optimizer == 'sgd':
cdae_optimizer = optim.SGD(cdae_params, lr=opt.d_lr)
elif opt.d_optimizer == 'adam':
cdae_optimizer = Adam(cdae_params, lr=opt.d_lr, betas=(opt.d_beta1, 0.999))
elif opt.d_optimizer == 'amsgrad':
cdae_optimizer = Adam(cdae_params, lr=opt.d_lr, betas=(opt.d_beta1, 0.999), amsgrad=True)
elif opt.d_optimizer == 'rmsprop':
cdae_optimizer = optim.RMSprop(cdae_params, lr=opt.d_lr, momentum=opt.d_momentum)
else:
raise NotImplementedError('unknown optimizer: {}'.format(opt.cdae_optimizer))
cdae_scheduler = None
# resume
load_checkpoint(
model, optimizer=model_optimizer, scheduler=model_scheduler,
opt=opt, device=device,
filename='{}model-checkpoint.pth.tar'.format('final-' if opt.train_mode == 'final' else ''),
)
load_checkpoint(
cdae, optimizer=cdae_optimizer, scheduler=cdae_scheduler,
opt=opt,device=device,
filename='{}cdae-checkpoint.pth.tar'.format('final-' if opt.train_mode == 'final' else ''),
)
# define evaluate
def evaluate_iws(eval_loader, model, model_optimizer, name='valid'):
model.eval()
if opt.m_weight_avg != 'none':
model_optimizer.use_buf()
total_loss = 0.
total_elbo = 0.
total_logprob = 0.
num_data = 0
start_time = time.time()
with torch.no_grad():
for batch_idx, (eval_data, _) in enumerate(eval_loader):
# init
batch_size = eval_data.size(0)
# init data
eval_data = eval_data.to(device)
# logprob
logprob = model.logprob(eval_data, sample_size=opt.iws_samples)
# add to total loss
total_logprob += logprob.item() * batch_size
num_data += batch_size
# return
elapsed = time.time() - start_time
model.train()
if opt.m_weight_avg != 'none':
model_optimizer.use_sgd()
return total_logprob / num_data, elapsed
# define train
def train(train_loader,
model, model_optimizer,
cdae, cdae_optimizer,
epoch, start_batch_idx=0,
):
# set global variable
global running_train_data_iter
# init
total_model_loss = 0.
total_recon_loss = 0.
total_prior_loss = 0.
num_data_model = 0
total_cdae_loss = 0.
num_data_cdae = 0
start_time = time.time()
train_num_iters_per_epoch = len(train_loader.dataset) // opt.train_batch_size
for _batch_idx in range(train_num_iters_per_epoch):
# init batch_idx and i_ep
batch_idx = _batch_idx + start_batch_idx
i_ep = (epoch-1)*train_num_iters_per_epoch + batch_idx
# end
if opt.train_mode == 'final' and (i_ep+1) > opt.end_iter:
raise EndIterError('end of training (final)')
# init weights
eta = annealing_func(opt.eta_init, opt.eta_fin, opt.eta_annealing, i_ep)
beta = annealing_func(opt.beta_init, opt.beta_fin, opt.beta_annealing, i_ep)
lmbd = annealing_func(opt.lmbd_init, opt.lmbd_fin, opt.lmbd_annealing, i_ep)
''' update cdae '''
# init model and cdae
model.train()
cdae.train()
# update cdae
for i in range(opt.num_cdae_updates):
# init grad
cdae_optimizer.zero_grad()
# init batch
try:
_train_data, _ = running_train_data_iter.next()
except:
running_train_data_iter = iter(train_loader)
_train_data, _ = running_train_data_iter.next()
# init data
_train_data = _train_data.to(device)
_batch_size = _train_data.size(0)
# get context
if opt.cdae_ctx_type == 'data':
context = _train_data.unsqueeze(1)
if 'mnist' in opt.dataset:
context = 2*context-1
context = context.view(_batch_size, 1, -1)
elif opt.cdae_ctx_type == 'lt0':
std00latent = model.encode(_train_data, std=0).detach()
context = std00latent
elif opt.cdae_ctx_type == 'hidden1a':
hidden = model.encode.forward_hidden(_train_data, std=0).detach()
context = hidden.unsqueeze(1)
else:
raise NotImplementedError
# expand context
if opt.train_nz_cdae > 1 and 'mlp' in opt.cdae:
context = context.view(context.size(0), context.size(1), -1)
# forward
latent_mean = model.encode(_train_data, std=0).detach() # bsz x 1 x dims
latent = model.forward_hidden(_train_data, nz=opt.train_nz_cdae)
latent = latent.detach()
# input
latent_sub_mean = opt.std_scale*(latent-latent_mean)#.detach()
std_qz = torch.std(latent_sub_mean, dim=1, keepdim=True) # bsz x 1 x dims
std = opt.delta*torch.mean(std_qz, dim=2, keepdim=True) # bsz x 1 x 1
cur_mean_std = std.mean().item()
cur_mean_std_max = std.max().item()
cur_mean_std_min = std.min().item()
# init std
stdmat = std*torch.randn(_batch_size, opt.train_nz_cdae*opt.train_nstd_cdae, 1, device=device)
# forward
sz = list(latent_sub_mean.size())
_latent_sub_mean = latent_sub_mean.unsqueeze(2).expand(
_batch_size, opt.train_nz_cdae, opt.train_nstd_cdae, sz[-1],
).reshape(_batch_size, opt.train_nz_cdae*opt.train_nstd_cdae, sz[-1])
output, cdae_loss = cdae(_latent_sub_mean, context, std=stdmat, scale=opt.std_scale)
# backward
cdae_loss.backward()
# add to total loss
cur_cdae_loss = cdae_loss.item()
total_cdae_loss += cur_cdae_loss * _batch_size
num_data_cdae += _batch_size
# update
cdae_optimizer.step()
''' update model '''
# init model and cdae
model.train()
cdae.eval()
# init grad
model_optimizer.zero_grad()
# init batch
try:
_train_data, _ = running_train_data_iter.next()
except:
running_train_data_iter = iter(train_loader)
_train_data, _ = running_train_data_iter.next()
# init data
_train_data = _train_data.to(device)
_batch_size = _train_data.size(0)
# forward
output, _, latent, model_loss, recon_loss, prior_loss = model(_train_data, beta=beta, eta=eta, lmbd=lmbd, nz=opt.train_nz_model)
# backward
model_loss.backward(retain_graph=True)
# get context
if opt.cdae_ctx_type == 'data':
context = _train_data.unsqueeze(1)
if 'mnist' in opt.dataset:
context = 2*context-1
context = context.view(_batch_size, 1, -1)
elif opt.cdae_ctx_type == 'lt0':
std00latent = model.encode(_train_data, std=0).detach()
context = std00latent
elif opt.cdae_ctx_type == 'hidden1a':
hidden = model.encode.forward_hidden(_train_data, std=0).detach()
context = hidden.unsqueeze(1)
else:
raise NotImplementedError
# expand context
if opt.train_nz_model > 1 and 'mlp' in opt.cdae:
context = context.view(context.size(0), context.size(1), -1)
# grad estimate
latent_mean = model.encode(_train_data, std=0).detach() # bsz x 1 x dims
latent_sub_mean = opt.std_scale*(latent-latent_mean).detach()
stdmat = torch.zeros(_batch_size, opt.train_nz_model, 1, device=device).fill_(0)
grad = cdae.glogprob(latent_sub_mean, context, std=stdmat, scale=opt.std_scale).detach()
# aux_loss with cdae forward and backward
#aux_loss = aux_loss_for_grad(opt.std_scale*(latent-latent_mean), beta*grad.detach()/float(_batch_size*opt.train_nz_model))
#aux_loss.backward()
(opt.std_scale*(latent-latent_mean)).backward(beta*grad.detach()/float(_batch_size*opt.train_nz_model))
# add to total loss
cur_model_loss = model_loss.item()
total_model_loss += cur_model_loss * _batch_size
num_data_model += _batch_size
cur_recon_loss = recon_loss.item()
cur_prior_loss = prior_loss.item()
total_recon_loss += cur_recon_loss * _batch_size
total_prior_loss += cur_prior_loss * _batch_size
# update
model_optimizer.step()
''' print '''
#if (batch_idx+1) % opt.log_interval == 0:
if (i_ep+1) % opt.log_interval == 0:
# set log info
elapsed = time.time() - start_time
# get lr
param_group = cdae_optimizer.param_groups[0]
lr = param_group['lr']
# print
logging('| iter {:d} | epoch {:3d} | {:5d}/{:5d} | ms/step {:5.2f} '
'| dlr {:.5f} '
'| (eff) std {:5.3f} '
'| (true) std {:5.3f} '
'| (eff) max std {:5.3f} '
'| (eff) min std {:5.3f} '
'| beta {:5.3f} ' #'| (crs) beta {:5.3f} '
#'| eta {:5.3f} '
#'| lmbd {:5.3f} '
'| loss (vae) {:5.3f} '
'| loss (recon) {:5.3f} '
'| loss (prior) {:5.3f} '
'| loss (cdae) {:5.4f} '
.format(
i_ep+1,
epoch,
batch_idx+1, train_num_iters_per_epoch,
elapsed * 1000 / opt.log_interval,
lr,
cur_mean_std,
cur_mean_std / opt.std_scale,
cur_mean_std_max,
cur_mean_std_min,
beta,
#eta,
#lmbd,
cur_model_loss,# / _batch_size,
cur_recon_loss,# / _batch_size,
cur_prior_loss,# / _batch_size,
cur_cdae_loss,# / _batch_size,
),
path=opt.path)
# write to tensorboard
writer.add_scalar('{}/model/loss/step'.format(opt.train_mode), cur_model_loss, i_ep+1)#/ _batch_size, i_ep+1)
writer.add_scalar('{}/model/recon/step'.format(opt.train_mode), cur_recon_loss, i_ep+1)#/ _batch_size, i_ep+1)
writer.add_scalar('{}/model/prior/step'.format(opt.train_mode), cur_prior_loss, i_ep+1)#/ _batch_size, i_ep+1)
writer.add_scalar('{}/model/beta/step'.format(opt.train_mode), beta, i_ep+1)
#writer.add_scalar('{}/model/eta/step'.format(opt.train_mode), eta, i_ep+1)
#writer.add_scalar('{}/model/lmbd/step'.format(opt.train_mode), lmbd, i_ep+1)
writer.add_scalar('{}/cdae/loss/step'.format(opt.train_mode), cur_cdae_loss, i_ep+1)
writer.add_scalar('{}/cdae/std/eff/mean/step'.format(opt.train_mode), cur_mean_std, i_ep+1)
writer.add_scalar('{}/cdae/std/true/mean/step'.format(opt.train_mode), cur_mean_std/opt.std_scale, i_ep+1)
writer.add_scalar('{}/cdae/std/eff/max/step'.format(opt.train_mode), cur_mean_std_max, i_ep+1)
writer.add_scalar('{}/cdae/std/true/max/step'.format(opt.train_mode), cur_mean_std_max/opt.std_scale, i_ep+1)
writer.add_scalar('{}/cdae/std/eff/min/step'.format(opt.train_mode), cur_mean_std_min, i_ep+1)
writer.add_scalar('{}/cdae/std/true/min/step'.format(opt.train_mode), cur_mean_std_min/opt.std_scale, i_ep+1)
writer.add_scalar('{}/cdae/lr/step'.format(opt.train_mode), lr, i_ep+1)
# reset log info
start_time = time.time()
''' evaluate '''
if opt.train_mode == 'train' and opt.eval_iws_interval > 0 and (i_ep+1) % opt.eval_iws_interval == 0:
logprob, elapsed_evaluate = evaluate_iws(val_loader, model, model_optimizer, name='valid')
writer.add_scalar('val/logprob/iws/step', logprob, i_ep+1)
logging('-' * 89, path=opt.path)
logging('| val '
'| iter {:d} | epoch {:3d} | {:5d}/{:5d} | sec/step {:5.2f} '
'| logprob (iws) {:.4f} '
.format(
i_ep+1, epoch,
batch_idx+1, train_num_iters_per_epoch,
elapsed_evaluate,
logprob,
),
path=opt.path)
logging('-' * 89, path=opt.path)
# Save the model if the validation loss is the best we've seen so far.
if not opt.best_val_loss or logprob > opt.best_val_loss:
opt.best_val_loss = logprob
save_checkpoint({
'epoch': epoch+1 if (batch_idx+1) == train_num_iters_per_epoch else epoch,
'batch_idx': (batch_idx+1) % train_num_iters_per_epoch,
'train_num_iters_per_epoch': train_num_iters_per_epoch,
'model': opt.model,
'state_dict': model.state_dict(),
'best_val_loss': opt.best_val_loss,
'optimizer' : model_optimizer.state_dict(),
'scheduler' : model_scheduler.state_dict() if model_scheduler is not None else None,
}, opt, is_best=False, filename='best-model-checkpoint.pth.tar')
save_checkpoint({
'epoch': epoch+1 if (batch_idx+1) == train_num_iters_per_epoch else epoch,
'batch_idx': (batch_idx+1) % train_num_iters_per_epoch,
'train_num_iters_per_epoch': train_num_iters_per_epoch,
'cdae': opt.cdae,
'state_dict': cdae.state_dict(),
'best_val_loss': opt.best_val_loss,
'optimizer' : cdae_optimizer.state_dict(),
'scheduler' : cdae_scheduler.state_dict() if cdae_scheduler is not None else None,
}, opt, is_best=False, filename='best-cdae-checkpoint.pth.tar')
''' visualize '''
if (i_ep+1) % opt.vis_interval == 0:
# check variance
#_, _, latent, _, _, _ = model(_train_data, nz=64) # bsz x ssz x zdim
latent = model.forward_hidden(_train_data, nz=64) # bsz x ssz x zdim
logvar_qz = torch.log(torch.var(latent.detach(), dim=1) + 1e-10) # bsz x zdim
_logvar_qz = logvar_qz.view(-1).cpu().numpy()
_mean_logvar_qz = torch.mean(logvar_qz).item()
_med_logvar_qz = torch.median(logvar_qz).item()
writer.add_scalar('{}/enc/logvar_qz/mean/step'.format(opt.train_mode), _mean_logvar_qz, i_ep+1)
writer.add_scalar('{}/enc/logvar_qz/median/step'.format(opt.train_mode), _med_logvar_qz, i_ep+1)
__logvar_qz = logvar_qz.view(_batch_size, -1).cpu().numpy()
writer.add_histogram('{}/enc/logvar_qz/hist/step'.format(opt.train_mode), _logvar_qz, i_ep+1)
for ii in range(min(2,_batch_size)):
writer.add_histogram('train{}/enc/logvar_qz/hist/step'.format(opt.train_mode).format(ii), __logvar_qz[ii], i_ep+1)
# visualize
if opt.dataset in ['swissroll', '25gaussians']:
# data
val = 6
gens = []
outputs = []
latents = []
std08latents = []
std05latents = []
std01latents = []
std0latents = []
datas = []
for i in range(int(20000//opt.train_batch_size)+1):
try:
_train_data, _ = running_train_data_iter.next()
except:
running_train_data_iter = iter(train_loader)
_train_data, _ = running_train_data_iter.next()
_train_data = _train_data.to(device)
datas += [_train_data]
gen, _, _ = model.generate(opt.train_batch_size)
gens += [gen.detach()]
std0latent = model.encode(_train_data, std=0)
std0latents += [std0latent.detach()]
std01latent = model.encode(_train_data, std=0.1)
std01latents += [std01latent.detach()]
std05latent = model.encode(_train_data, std=0.5)
std05latents += [std05latent.detach()]
std08latent = model.encode(_train_data, std=0.8)
std08latents += [std08latent.detach()]