-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVAE_up.py
1461 lines (1243 loc) · 60.6 KB
/
VAE_up.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 -*-
from __future__ import print_function
import utils
import torch
import time
import os
import pickle
import datetime
import torch._utils
# maintaining upward compatibility
try:
torch._utils._rebuild_tensor_v2
except AttributeError:
def _rebuild_tensor_v2(storage, storage_offset, size, stride, requires_grad, backward_hooks):
tensor = torch._utils._rebuild_tensor(storage, storage_offset, size, stride)
tensor.requires_grad = requires_grad
tensor._backward_hooks = backward_hooks
return tensor
torch._utils._rebuild_tensor_v2 = _rebuild_tensor_v2
import numpy as np
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torch.nn.functional as F
from utils_peptide import convert_given_representation
from utils_peptide import estimateProperties
class ARDprior:
'''Helper class for calculating contributions from the ARD prior.
This class provides calculates log p(\theta) while \theta being the parameters
the ARD prior is applied. This assumes an independent precision for every \theta.
See Eq. (20) in **paper_link**.
'''
def __init__(self, a0, paramlist, model, gpu):
self.model = model
self.paramlist = paramlist
self.a0 = a0
self.b0 = a0
self.gpu = gpu
def getlogpiorARD(self):
i = 0
if self.gpu:
totalsumlogptheta = torch.autograd.Variable(torch.zeros(1)).cuda()
else:
totalsumlogptheta = torch.autograd.Variable(torch.zeros(1))
for paramitem in self.paramlist:
par = paramitem['params']
psqu = par.pow(2.)
denominator = psqu.mul(0.5) + self.b0
nominator = torch.zeros_like(denominator)
nominator.fill_(self.a0 + 0.5)
expectau = nominator.div(denominator)
logptheta = expectau.mul(psqu)
logptheta.mul_(-0.5)
sumlogptheta = logptheta.sum()
totalsumlogptheta.add_(sumlogptheta)
return totalsumlogptheta
class PseudoGibbs:
'''Helper calss for the implementation of Metropolis-within-Gibbs Sampler.
This class contains Algorithm 2 in **paper_link**.
The original reference is https://arxiv.org/abs/1802.04826.
'''
def __init__(self, x_init, z_init, model):
self.x_init = x_init
self.z_init = z_init
self.model = model
self.model.bgetlogvar = True
self.n_skip = 10
self.n_init = 5000
def sampleposterior(self, x):
mu, logvar = self.model.encode(x)
std = logvar.mul(0.5).exp_()
post = torch.distributions.Normal(mu, std)
sample = post.sample()
return sample
def samplepredictive(self, z):
self.model.bgetlogvar = True
mu, logvar = self.model.decode(z)
std = logvar.mul(0.5).exp_()
pred = torch.distributions.Normal(mu, std)
x = pred.sample()
return x
mu, logvar = self.model.encode(x)
post = torch.distributions.Normal(mu, logvar.exp_())
sample = post.sample()
return sample
def evallogprobposterior(self, x, z):
mu, logvar = self.model.encode(x)
std = logvar.mul(0.5).exp_()
post = torch.distributions.Normal(mu, std)
logprob = post.log_prob(z).sum()
return logprob
def evallogprobcondpred(self, x, z):
self.model.bgetlogvar = True
mu, logvar = self.model.decode(z)
std = logvar.mul(0.5).exp_()
post = torch.distributions.Normal(mu, std)
logprob = post.log_prob(x).sum()
return logprob
def evallogprobprior(self, z):
mu = torch.zeros_like(z)
scale = torch.ones_like(z)
prior = torch.distributions.Normal(mu, scale)
logprob = prior.log_prob(z).sum()
return logprob
def calcacceptanceratio(self, ztm1, ztprop, xtm1):
p_xtm1_given_ztprop = self.evallogprobcondpred(xtm1, ztprop)
p_xtm1_given_ztm1 = self.evallogprobcondpred(xtm1, ztm1)
p_ztprop = self.evallogprobprior(ztprop)
p_ztm1 = self.evallogprobprior(ztm1)
q_ztm1_given_xtm1 = self.evallogprobposterior(xtm1, ztm1)
q_ztprop_given_xtm1 = self.evallogprobposterior(xtm1, ztprop)
ratio_pxgz = p_xtm1_given_ztprop - p_xtm1_given_ztm1
ratio_pz = p_ztprop - p_ztm1
ratio_qzgx = q_ztm1_given_xtm1 - q_ztprop_given_xtm1
logroh = ratio_pxgz + ratio_pz + ratio_qzgx
return logroh.exp()
def getboolofvariable(self, bytetensor):
res = bool(bytetensor[0])
return res
def sample(self, N):
n_tot = self.n_init + N*self.n_skip
n_accepted = 0
xtm1 = self.x_init
ztm1 = self.z_init
x_samples = xtm1
for i in range(n_tot):
ztprop = self.sampleposterior(xtm1)
rhot = self.calcacceptanceratio(ztm1, ztprop, xtm1)
rhottensor = rhot.data[0]
if rhottensor > 1.:
zt = ztprop
n_accepted += 1
else:
r = torch.rand(1)
if rhottensor > r[0]:
zt = ztprop
n_accepted += 1
else:
zt = ztm1
xt = self.samplepredictive(zt)
s = i - self.n_init
if s > 1 and s % self.n_skip == 0:
x_samples = torch.cat((x_samples, xt), 0)
ztm1 = zt
xtm1 = xt
accept_ratio = n_accepted/float(n_tot)
print(accept_ratio)
return x_samples
class MVN:
'''Helper class for multivariate Gaussian.
'''
def __init__(self, mean, cov):
self.mean = mean.copy()
self.cov = cov.copy()
def sample(self):
return np.random.multivariate_normal(self.mean, self.cov)
class UQ:
'''Helper class for options with regard to uncertainty quantification of
the model parameters.
'''
def __init__(self, bdouq=False, bcalchess=False, blayercov=False, buqbias=False):
self.bdouq = bdouq
self.npostsamples = 100
self.bhessavailable = bcalchess
self.blayercov = blayercov
self.buqbias = buqbias
def checkandcreatefolder(dir):
'''This function checks if a directory exists, if not it is created augmented by a date and time dependent postfix.
:param dir: Complete path of the directory
:return: Actual directory created.
'''
if not os.path.exists(dir):
os.makedirs(dir)
diraug = dir
else:
datetimepostfix = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
diraug = os.path.join(dir, datetimepostfix)
os.makedirs(diraug)
return diraug
def count_parameters(model):
'''This function counts the parameters of a PyTorch model.
:param model: PyTorch model.
:return: Number of parameters requiring gradient.
'''
return sum(p.numel() for p in model.parameters() if p.requires_grad)
class TensorDatasetDataOnly(torch.utils.data.Dataset):
"""Dataset wrapping only data tensors.
Each sample will be retrieved by indexing both tensors along the first
dimension.
Arguments:
data_tensor (Tensor): contains sample data.
"""
def __init__(self, data_tensor):
self.data_tensor = data_tensor
def __getitem__(self, index):
return self.data_tensor[index]
def __len__(self):
return self.data_tensor.size(0)
class VAEpeptide(object):
def __init__(self, args):
'''Initialize the VAE object.
:param args: Arguments containing settings.
'''
# parameters
self.epoch = args.epoch
self.sample_num = 64
self.batch_size = args.batch_size
self.save_dir = args.save_dir
self.result_dir = args.result_dir
self.dataset = args.dataset
self.log_dir = args.log_dir
self.gpu_mode = bool(args.gpu_mode) and torch.cuda.is_available()
self.model_name = args.mod_type
self.c = args.clipping # clipping value
self.n_critic = args.n_critic # the number of iterations of the critic per generator iteration
self.z_dim = args.z_dim
self.n_samples = args.samples_pred
self.bClusterND = bool(args.clusterND)
self.output_postfix = args.outPostFix
self.angulardata = args.useangulardat
self.autoencvarbayes = bool(args.AEVB)
self.L = args.L # amount of eps ~ p(eps) = N(0,1)
self.n_samples_per_mu = args.samples_per_mean # if 0, just use mean prediction: x = mu(z)
self.lambdaexpprior = args.exppriorvar
# Employ Metropolis-within-Gibbs Sampler
self.exactlikeli = bool(args.exactlikeli)
# perform UQ
bqu = bool(args.npostS)
self.uqoptions = UQ(bdouq=bqu, bcalchess=True, blayercov=False, buqbias=bool(args.uqbias))
self.uqoptions.npostsamples = args.npostS
# \sigma_\theta depending on z or not. If not, optimized as parameter
self.bfixlogvar = bool(args.sharedlogvar)
# Check if a trained model should be loaded
self.filemodel = args.loadtrainedmodel
self.bloadmodel = bool(self.filemodel)
# Visualize training process?
self.bvislatent_training = True
self.bvismean_and_samples = False
# ARD prior
if args.ard > 0.:
self.bard = True
self.arda0 = args.ard
else:
self.bard = False
self.arda0 = 0.
# We can only sample if p(x|z) is defined like in Autoencoding Var. Bayes.
if not self.autoencvarbayes:
self.n_samples_per_mu = 0
# Specify input data for e.g. angular. This is not scope of the work **paper_link** and therefore not content of the code.
if False:
self.x_dim = (22 - 1) * 3
print('Error: Model for %s not implemented yet.' % self.angulardata)
quit()
elif self.angulardata == 'ang_augmented':
self.x_dim = (22 - 1) * 5
print('Error: Model for %s not implemented yet.' % self.angulardata)
quit()
elif self.angulardata == 'ang_auggrouped':
self.x_dim = (22 - 1) * 5
if self.autoencvarbayes:
from VAEmodel import VAEmodauggrouped as VAEmod
else:
print('Not covered in **paper**.')
quit()
else:
if 'ala_15' in self.dataset:
self.x_dim = 162 * 3
else:
self.x_dim = 66
if self.autoencvarbayes:
from VAEmodel import VAEmod
else:
print('Not covered in **paper**.')
quit()
# seed the calculation for testing
if not args.seed == 0:
torch.manual_seed(args.seed)
if bool(args.gpu_mode):
torch.cuda.manual_seed(args.seed)
# pre-sepcify foldername variable for dataset
foldername = self.dataset
predictprefix = ''
# Select dataset
self.selectdataset()
# Specify as model_name the general kind of dataset: mixed or separate
self.postfix_setting = self.dataset + '_z_' + str(self.z_dim) + '_' + str(self.batch_size) + '_' + str(self.epoch)
self.predprefix = predictprefix
# Saving directory
working_dir = os.getcwd()
tempdir = os.path.join(working_dir, self.result_dir, self.model_name, foldername, self.output_postfix)
self.output_dir = checkandcreatefolder(dir=tempdir)
# Initialize VAE model
self.vaemodel = VAEmod(args, self.x_dim, self.bfixlogvar)
# This should be changed for later versions of PyTorch
if self.gpu_mode:
self.vaemodel.cuda()
# Initialize optimizer
self.optimizer = optim.Adam(self.vaemodel.parameters(), lr=1e-3)
# Initialize ARD prior
if self.bard:
self.ardprior = ARDprior(self.arda0, self.getdecweightlist(), self.vaemodel, self.gpu_mode)
def getweightlist(self):
'''This function obtains a parameter list of the model.
:return: Dict list of parameters.
'''
weight_list = []
id = 0
for name, param in self.vaemodel.named_parameters():
if param.requires_grad:
weight_list.append({'name': name, 'id': id, 'params': param})
#pclone = param.clone()
#params_dec_copy.append({'name': name, 'id': id, 'params': pclone})
print(name) # , param.data
id = id + 1
return weight_list
def getdecweightlist(self):
'''This function obtains a parameter list of the decoding part of the model.
:return: Dict list of parameters.
'''
decoding_weight_list = []
id = 0
for name, param in self.vaemodel.named_parameters():
if param.requires_grad:
# UQ only for decoding network
if 'dec_' in name:
# check if we want to uq bias uncertainty
if not ('.bias' in name) and not ('logvar' in name):
decoding_weight_list.append({'name': name, 'id': id, 'params': param})
print(name) # , param.data
id = id + 1
return decoding_weight_list
def loss_function_autoencvarbayes(self, recon_mu, recon_logvar, x, mu, logvar, x_dim=784):
''' This function computes the objective according Autoencoding Variational Bayes of D. Kingma and M. Welling, 2014.
:param recon_mu: Reconstructed mean of p(x|z) with z is the encoded data.
:param recon_logvar: Reconstructed variance of p(x|z) with z is the encoded data.
:param x: Data.
:param mu: Mean of encoded x (data).
:param logvar: Variance of encoded x (data).
:param x_dim: Dim(x_i) of a single datum.
:return:
'''
pointwiseMSEloss = 0.5 * F.mse_loss(recon_mu, x.view(-1, x_dim), size_average=False, reduce=False)
# Maug is here the augmentet bacht size: explicitly: M*L while L indicates the sample for reparametrization \epsilon ~ p(\epsilon)
Maug = pointwiseMSEloss.shape[0]
sigsq = recon_logvar.exp()
weight = sigsq.reciprocal()
logvarobjective = 0.5 * recon_logvar.sum()
pointwiseWeightedMSEloss = pointwiseMSEloss.mul(weight)
# This implies the contribution from a Gaussian p(x|z) with the diagonal covariance entries sigsq.
WeightedMSEloss = pointwiseWeightedMSEloss.sum()
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
self.train_hist['kl_qp'].append(KLD)
# Prior on predictive variance
# This is not used in the **paper_link**, thus not relevant.
psigsqlamb = self.lambdaexpprior
# employ prior if desired
if psigsqlamb > 0.:
lamb = torch.FloatTensor(1)
lamb.fill_(psigsqlamb)
if self.gpu_mode:
lambvariable = Variable(lamb.cuda())
else:
lambvariable = Variable(lamb)
loglamb = lambvariable.log()
# minus here becuase of minimization; expression stems from max log-likelihood
logpriorpvarexpanded = - (loglamb.expand_as(sigsq) - sigsq.mul(psigsqlamb))
logpriorvarsum = logpriorpvarexpanded.sum()
logpriorvar = logpriorvarsum.div(Maug)
else:
logpriorvar = torch.zeros_like(KLD)
# return (WeightedMSEloss + KLD); logpriorvar is zero - since inactive for the paper.
loss = (logvarobjective + WeightedMSEloss + KLD + logpriorvar)
# chekc if ARD prior is active, if so, add its contribution to the loss.
if self.bard:
ardcontrib = self.ardprior.getlogpiorARD()
ardcontrib.mul_(float(Maug)/self.N)
loss.add_(-ardcontrib[0])
# Can be removed since it runs stable.
lossnp = loss.data.cpu().numpy()
if lossnp != lossnp:
print('Error: Loss is NaN')
return loss
def trainepoch(self, epoch):
'''Train one epoch.
:param epoch: Epoch number
'''
self.vaemodel.train()
train_loss = 0
for batch_idx, data in enumerate(self.data_loader):
# Copy the data tensor for using more eps ~ p(eps) samples Eq. (7) in AEVB paper
L = self.L
dataaug = data.clone()
for l in range(L - 1):
dataaug = torch.cat((dataaug, data), 0)
data = Variable(dataaug)
# can be removed for newer PyTorch versions
if self.gpu_mode:
data = data.cuda()
# Set gradient to zero.
self.optimizer.zero_grad()
# This is actually the only version used in the **paper**: self.autoencvarbayes is True.
if self.autoencvarbayes:
recon_batch, mu, logvar = self.vaemodel(data)
recon_mu = recon_batch[0]
recon_logvar = recon_batch[1]
loss = self.loss_function_autoencvarbayes(recon_mu, recon_logvar, data, mu, logvar, x_dim=self.x_dim)
else:
## Other options not considered
print('Use self.autoencvarbayes = True, other options are not in accordance with the paper and thus not included here.')
quit()
# loss.backward(retain_graph=True)
loss.backward()
train_loss += loss.data[0]
self.optimizer.step()
# Just some monitoring options.
log_interval = 20
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(self.data_loader.dataset),
100. * batch_idx / len(self.data_loader),
loss.data[0] / len(data)))
self.train_hist['Total_loss'].append(loss.data[0] / len(data))
def samplePosterior(self, npostsamples):
''' This function samples p(\theta|X)
and produces a trajectory for every \theta_i by sampling the generative model p(x|\theta_i).
:param npostsamples: Amount of posterior samples p(\theta|X) and thus trajectories stored.
'''
# Compute the Hessian based on the converged MAP estimate.
pert_param_list, params_dec_copy, hess_list = self.getHessian()
# Obtain p(\theta|X)
list_normals = []
id = 0
for hess in hess_list:
if not hess['fullcov']:
var = hess['diaghessian'].reciprocal()
scale = var.sqrt()
mean = params_dec_copy[id]['params']
list_normals.append(torch.distributions.Normal(mean.data, scale))
else:
if not hess['parisvector']:
shape = params_dec_copy[id]['params'].shape()
elements = shape[0]*shape[1]
mean = params_dec_copy[id]['params'].reshape(elements)
cov = hess['diaghessian'].inverse()
meannp = mean.data.cpu().numpy()
covnp = cov.cpu().numpy()
list_normals.append(MVN(meannp, covnp))
else:
mean = params_dec_copy[id]['params']
cov = hess['diaghessian'].inverse()
meannp = mean.data.cpu().numpy()
covnp = cov.cpu().numpy()
list_normals.append(MVN(meannp, covnp))
id += 1
# Perform the actual sampling process.
for i in range(npostsamples):
id = 0
for parlistitem in pert_param_list:
hess = hess_list[id]
if not (parlistitem['name'] == 'dec_logvar'):
if not hess['fullcov']:
sample = list_normals[id].sample()
parlistitem['params'].data.set_(sample)
else:
if not hess['parisvector']:
shape = params_dec_copy[id]['params'].shape()
samplenp = list_normals[id].sample()
samplevec = torch.from_numpy(samplenp)
sample = samplevec.resize(shape[0], shape[1])
parlistitem['params'].data.set_(sample)
else:
samplenp = list_normals[id].sample()
sample = torch.from_numpy(samplenp).float()
parlistitem['params'].data.set_(sample)
id += 1
# Make predictions given the current posterior sample \theta ~ p(\theta|X)
# and store it in a file.
self.gen_samples(n_samples=self.n_samples, postsampid=i)
def getHessian(self):
'''This function computes the diagonal entries of the Hessian matrix of the
decoding NN parameters.
:return pert_param_list, params_dec_copy, hess_list
'''
blayercov = self.uqoptions.blayercov
self.uqoptions.bhessavailable = True
bvartemp = self.vaemodel.bgetlogvar
self.vaemodel.bgetlogvar = True
data_loader_hessian_approx = DataLoader(TensorDatasetDataOnly(self.data_tensor), batch_size=self.N,
batch_sampler=None,
shuffle=False, **self.kwargsdatloader)
for index, data in enumerate(data_loader_hessian_approx):
data = Variable(data)
if self.gpu_mode:
data = data.cuda()
# Resetting any gradient
self.optimizer.zero_grad()
if self.autoencvarbayes:
recon_batch, mu, logvar = self.vaemodel(data)
recon_mu = recon_batch[0]
recon_logvar = recon_batch[1]
# print(np.exp(recon_logvar.data.cpu().numpy()))
loss = self.loss_function_autoencvarbayes(recon_mu, recon_logvar, data, mu, logvar, x_dim=self.x_dim)
else:
recon_batch, mu, logvar = self.vaemodel(data)
loss = self.loss_function(recon_batch, data, mu, logvar, x_dim=self.x_dim)
# Calculate gradient
loss.backward(retain_graph=True)
# Identify the parameters the second derivative is required
pert_param_list = []
params_dec_copy = []
id = 0
for name, param in self.vaemodel.named_parameters():
if param.requires_grad:
# UQ only for decoding network
if 'dec_' in name:
# check if we want to uq bias uncertainty
if '.bias' in name and self.uqoptions.buqbias:
pert_param_list.append({'name': name, 'id': id, 'params': param})
pclone = param.clone()
params_dec_copy.append({'name': name, 'id': id, 'params': pclone})
print(name) # , param.data
else:
pert_param_list.append({'name': name, 'id': id, 'params': param})
pclone = param.clone()
params_dec_copy.append({'name': name, 'id': id, 'params': pclone})
print(name) # , param.data
id = id + 1
hess_list = []
# for group in param_groups:
# for p in group['params'][2*nLayerEnc:]:
for parentry in pert_param_list:
p = parentry['params']
if parentry['name'] == 'dec_logvar':
blayercov = False
else:
blayercov = self.uqoptions.blayercov
grad_params = torch.autograd.grad(loss, p, create_graph=True)
# hv = torch.autograd.grad(g000, p, create_graph=True)
hess_params = torch.zeros_like(grad_params[0])
# print(hess_params.size())
bfullcov = False
bparisvector = False
dim_grad = grad_params[0].dim()
if dim_grad == 1:
bparisvector = True
if blayercov:
bfullcov = True
size = grad_params[0].size()
elements = size[0]
unrolled_grad_params = grad_params[0]
hess_params = torch.autograd.Variable(torch.torch.zeros(elements, elements))
for i in range(elements):
# gives the row of the hessian
hessrow = torch.autograd.grad(unrolled_grad_params[i], p, retain_graph=True)[0].resize(elements)
hess_params[i, :] = hessrow
else:
bfullcov = False
for i in range(grad_params[0].size(0)):
hess_params[i] = torch.autograd.grad(grad_params[0][i], p, retain_graph=True)[0][i]
else:
bparisvector = False
#if blayercov:
# TODO sparse storage of matrix required needed
if False:
bfullcov = True
size = grad_params[0].size()
elements = size[0]*size[1]
unrolled_grad_params = grad_params[0].resize(elements)
hess_params = torch.autograd.Variable(torch.torch.zeros(elements, elements))
for i in range(elements):
# gives the row of the hessian
hessrow = torch.autograd.grad(unrolled_grad_params[i], p, retain_graph=True)[0].resize(elements)
hess_params[i, :] = hessrow
else:
bfullcov = False
for i in range(grad_params[0].size(0)):
for j in range(grad_params[0].size(1)):
hess_params[i, j] = torch.autograd.grad(grad_params[0][i][j], p, retain_graph=True)[0][i, j]
if not bfullcov:
hess_params[hess_params < 0.5] = 10.*self.N #1.e5
hess_list.append({'name': parentry['name'], 'id': parentry['id'], 'diaghessian': hess_params.data, 'fullcov': bfullcov, 'parisvector': bparisvector})
np.savetxt(os.path.join(self.output_dir, parentry['name']+'_' + str(self.N) + '.txt'), hess_params.data.cpu().numpy())
self.vaemodel.bgetlogvar = bvartemp
return pert_param_list, params_dec_copy, hess_list
def train(self):
'''This functions is the main training function.
'''
self.train_hist = {}
self.train_hist['Total_loss'] = []
self.train_hist['per_epoch_time'] = []
self.train_hist['total_time'] = []
self.train_hist['kl_qp'] = []
# Store intermediate steps
intermediate = False
# If the model from previous training process is loaded, no need to train it.
if not self.bloadmodel:
print('Training started.')
start_time = time.time()
for epoch in range(1, self.epoch + 1):
epoch_start_time = time.time()
# Train single epoch
self.trainepoch(epoch)
# visualize intermediate latent space
self.visLatentTraining(epoch)
# Make intermediate predictions during the training procedure
if intermediate:
# sample the prior
sample = Variable(torch.randn(64, self.z_dim))
# This can be removed in newer PyTorch versions.
if self.gpu_mode:
sample = sample.cuda()
# Decode the samples
sample = self.vaemodel.decode(sample).cpu()
# The step below does not affect the prediction, but in case of providing Cartesian
# coordinates as done in the **paper**.
sampleout = convert_given_representation(samples=sample, coordrep=self.angulardata)
np.savetxt(self.output_dir + '/samples' + self.predprefix + '.txt', sampleout)
# store training time for one epoch
self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
# Training has been completed, save the trained model.
torch.save(self.vaemodel, self.output_dir + '/model.pth')
# Store total training data
self.train_hist['total_time'].append(time.time() - start_time)
print("Avg one epoch time: %.2f, total %d epochs time: %.2f" % (np.mean(self.train_hist['per_epoch_time']),
self.epoch,
self.train_hist['total_time'][0]))
print("Training finish!... save training results")
print("Final KLD loss %.3f" % (self.train_hist['kl_qp'][-1]))
# utils.generate_animation(self.result_dir + '/' + self.dataset + '/' + self.model_name + '/' + self.model_name,
# self.epoch)
utils.loss_plot(self.train_hist,
self.output_dir,
self.model_name + self.predprefix, bvae=True)
else:
# Load the vae model which was trained before for making predictions
self.vaemodel = torch.load(self.filemodel)
# Count active parameters
if self.bard:
nonactiveparams = self.countzeroweights(paramlist=self.ardprior.paramlist, threshold=0.0001)
else:
nonactiveparams = self.countzeroweights(paramlist=self.getdecweightlist(), threshold=0.0001)
nap = np.ones(1)*nonactiveparams
np.savetxt(self.output_dir + '/nonactiveparams.txt', nap)
# Model has been trained
self.vaemodel.eval()
# Generate samples for predictions (MAP estimate)
self.gen_samples(self.n_samples)
# Calculate the hessian if we require UQ
if self.uqoptions.bdouq:
self.samplePosterior(self.uqoptions.npostsamples)
def vis_phipsilatent(self, path):
'''Visualize the phi and psi landscape for the learned latent representation.
This function creates a grid and corresponding to the CVs make predictions x.
:param path: string, provide path where to save the prediction.
:return:
'''
# Create grid with numpy
x = np.linspace(-4., 4., 101)
y = np.linspace(-4., 4., 101)
X, Y = np.meshgrid(x, y)
Xvec = X.flatten()
Yvec = Y.flatten()
# Convert numpy array to torch
Xtorch = torch.from_numpy(Xvec).float()
Xtorch.unsqueeze_(1)
Ytorch = torch.from_numpy(Yvec).float()
Ytorch.unsqueeze(1)
# Not required for newer PyTorch
if self.gpu_mode:
samples_z = Variable(torch.cat((Xtorch, Ytorch), 1).cuda())
else:
samples_z = Variable(torch.cat((Xtorch, Ytorch), 1))
# Decode the CVs z to x
torchsamples = self.vaemodel.decode(samples_z)
# Convert to numpy
samples = torchsamples[0].data.cpu().numpy()
# Convert the samples if they are in the angular format - this function
# does not show any effect when using a Cartesian representation as in the **paper**.
samplesout = convert_given_representation(samples=samples, coordrep=self.angulardata)
np.savetxt(path + '/samples_vis_phipsi' + self.predprefix + '.txt', samplesout)
def countzeroweights(self, paramlist, threshold=0.0001):
'''This function counts the inactive weights of the decoding neural network.
:param paramlist: Torch parameter list of parameters which should be considered.
:param threshold: Threshold considering parameter as inactive.
:return: Amount of inactive parameters.
'''
counter = 0
for paramitem in paramlist:
par = paramitem['params']
abspar = par.abs()
abspardat = abspar.data
smaller = abspardat[abspardat < threshold]
if smaller.dim() > 0:
counter = counter + int(smaller.size()[0])
return counter
def vis_realizations(self):
'''This function visualizes a specific amount of realizations per prediction of the decoded data points.
For showing that the variance of p(x|z) captures outer Hydrogen atoms as noise.
:return:
'''
ic = 0
# load the
for batch_idx, data in enumerate(self.data_loader):
if ic == 0:
data_vis = data
x = Variable(data_vis)
n_samples = x.shape[0]
# Encode the data set into latent space
muenc, log = self.vaemodel.encode(x)
# Decode to mean and variance of predictions
self.vaemodel.bgetlogvar = True
if self.gpu_mode:
samplesTorchmu, samplesTorchlogvar = self.vaemodel.decode(muenc).gpu()
else:
samplesTorchmu, samplesTorchlogvar = self.vaemodel.decode(muenc)
mu = samplesTorchmu.data.cpu().numpy()
vartorch = samplesTorchlogvar.exp()
var = vartorch.data.cpu().numpy()
# Init storage for total amount of samples.
nsamples_per_dp = 300
nsamples_per_dp_tot = nsamples_per_dp + 1
n_samples_tot = n_samples * nsamples_per_dp_tot
samples_aevb = np.zeros([n_samples_tot, self.x_dim])
for n in range(n_samples):
samples_aevb[n * nsamples_per_dp_tot, :] = mu[n, :]
samples_aevb[n * nsamples_per_dp_tot + 1: (n + 1) * nsamples_per_dp_tot, :] = np.random.multivariate_normal(
mu[n, :], np.diag(var[n, :]), nsamples_per_dp)
self.vaemodel.bgetlogvar = False
# Only takes action when non Cartesian coordinates are used - not the case here.
samplesoutaevb = convert_given_representation(samples=samples_aevb, coordrep=self.angulardata)
np.savetxt(self.output_dir + '/samples_aevb' + self.predprefix + '_vis_mean_samples_' + '.txt', samplesoutaevb)
def vis_latentpredictions(self, yb, ny, path):
'''This function predicts atomistic configurations along a provided path in the CV space. Those z are mapped
to full atomistic configurations. Currently just ny and path are required. Only visualization.
:param ny: Number of points in y direction.
:param path: String of path for storing the prediction.
:return:
'''
# This allows to plot the latent representation and augment it with an indicator at the current position in the latent space
# This is only valid for ALA2
bVisualizeStar = False
if bVisualizeStar == True:
ny = 251
bShowTraining = True
if bShowTraining:
xt = Variable(self.data_tensor)
else:
xt = None
bRnd = False
#y = torch.linspace(yb[0], yb[1], ny)
# y coordinates
y = torch.linspace(-4, 4, ny)
#y1 = torch.linspace(-4, 0, ny*3)
#y2 = torch.linspace(0, 4, ny*2)
#nges = 5*ny
#y = torch.cat((y1,y2))
# x coordinates
x = torch.zeros(ny)
# For different plots experiments visualizing a path defined in the latent space.
# This path was used for ALA15 path in Fig. 14 in **paper**.
if False:
x = torch.linspace(-1, 3, ny)
y12 = -2.*x
y13 = -2./3.*x
y = torch.zeros_like(y12)
y.copy_(y12)
y[20:] = y13[20:]
# Check if gpu mode is active - drop for newer PyTorch.
if self.gpu_mode:
y = y.cuda()
x = x.cuda()
# summarize x and y in torch variable
y = y.unsqueeze(1)
x = x.unsqueeze(1)
samples_z = Variable(torch.cat((y, x), 1))
# This is for showing a marker at the current position in the latent space.
# E.g. for visualizing atomistic configurations for given CVs.
if bVisualizeStar:
for i in range(0, ny):
xnp_curr = samples_z[i, 0].data.numpy()
ynp_curr = samples_z[i, 1].data.numpy()
if i==0:
n = self.vaemodel.plotlatentrep(x=Variable(self.data_tensor_vis_1527), z_dim=self.z_dim, path=self.output_dir,
iter=i, x_curr=xnp_curr, y_curr=ynp_curr, nprov=False, x_train=xt)
else:
n = self.vaemodel.plotlatentrep(x=Variable(self.data_tensor_vis_1527), z_dim=self.z_dim, path=self.output_dir,
iter=i, x_curr=xnp_curr, y_curr=ynp_curr, nprov=True, normaltemp=n, x_train=xt)
torchsamples = self.vaemodel.decode(samples_z)
samples = torchsamples[0].data.cpu().numpy()
# Convert the samples if they are in the angular format - not relevant for this **paper**
samplesout = convert_given_representation(samples=samples, coordrep=self.angulardata)
np.savetxt(path + '/samples_vis_latent' + self.predprefix + '.txt', samplesout)
def gen_samples(self, n_samples=4000, postsampid=None):
'''This function samples predicted atomistic configurations involving the
generative part in p(z) and p(x|z) while the variance is considererd.
:param n_samples: Amount of required samples.
:param postsampid: This parameter is automatically set in case UQ tasks are performed.
:return:
'''
if postsampid == None and 'ala_15' not in self.dataset:
self.postprocessing(n_samples=4000, postsampid=None)
elif postsampid == None and 'ala_15' in self.dataset:
self.postprocessingALA15(postsampid=None)
# Saving samples with postfix - important for UQ
if postsampid == None:
postsamplepostfix = ''
else:
postsamplepostfix = '_postS_' + str(postsampid)
# Convert the samples if GPU mode is active - deprecated for more
# recent PyTorch versions
if self.gpu_mode: