-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplmodules.py
More file actions
1873 lines (1531 loc) · 84.2 KB
/
plmodules.py
File metadata and controls
1873 lines (1531 loc) · 84.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
from typing import Union, List
from omegaconf import DictConfig
import numpy as np
import rootutils
import itertools
import os.path
import wandb
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import pytorch_lightning as pl
from torchmetrics.functional.image import \
structural_similarity_index_measure as ssim
from torchmetrics.functional.image import \
peak_signal_noise_ratio as psnr
from models import Func_C2P, \
Generator_D2O, Generator_O2D, \
Discriminator_O, Discriminator_D, \
Encoder_O, Encoder_D
from func import L_ID_SCHEDULE
base = rootutils.find_root(search_from=__file__, indicator=".project-root")
class BaseModule(pl.LightningModule):
'''
Base module for all GAN-based models
accept common args, implement common methods
'''
def __init__(self,
G_D2O_config: DictConfig = None,
D_O_config: DictConfig = None,
Encoder_config: DictConfig = None,
GAN_mode: str = 'WGAN-GP', # [ 'vanilla' | 'hinge' | 'lsGAN' | 'WGAN' | 'WGAN-GP' ]
loss_id: str = 'L2', # [ 'L1' | 'L2' ]
optimizer: str = "RMSprop", # [ 'Adam' | 'RMSprop' ]
lr: float = 1e-5,
b1: float = 0.5,
b2: float = 0.999,
weight_clipping: bool = False,
clip_value: float = 0.01,
lambda_gp: float = 10,
n_opt_G: int = 1,
n_opt_D: int = 5,
log_images: bool = True,
embed_lead: bool = False, # lead is in dataset but not to model
idx_of_lead: int = 0, # position of `lead time` in aux_info
*args, **kwargs
):
super(BaseModule, self).__init__()
self.save_hyperparameters()
# basic check inside config here, currently none
pass
def on_train_start(self):
if self.hparams.debug:
return
# save critical code
if '/home/sw/Work4-GAN' in str(base):
wandb.save(os.path.join(base, 'scripts/train/*.py'))
elif '/home/pod/shared-nvme/Work1-GAN' in str(base):
wandb.save(os.path.join(base, 'train/*.py'))
def weight_clipping(self, model, clip_value):
for p in model.parameters():
p.data.clamp_(-clip_value, clip_value)
def compute_gradient_penalty(self, D, condition, real_samples, fake_samples):
'''
ref1: https://github.com/nocotan/pytorch-lightning-gans/blob/master/models/wgan_gp.py
ref2 to scale to patchGAN: https://github.com/p-hss/weather-gan/blob/main/src/model.py
conditions, real_samples, fake_samples: Tensor or Tuple[Tensor]
e.g. `compute_gradient_penalty(self.D, info, (obs_circu, obs_tp), (D2O_circu, D2O_tp))`
static conditions, real inputs, fake inputs
e.g. `compute_gradient_penalty(self.D, None, real_tp, fake_tp)`
'''
item_samples = real_samples[0] if isinstance(real_samples, tuple) else real_samples
alpha = torch.rand(item_samples.size(0), 1, 1, 1).type_as(item_samples)
interpolates = []
for real_items, fake_items in zip(real_samples, fake_samples):
interpolates += [
(alpha * real_items + ((1 - alpha) * fake_items)).requires_grad_(True)
]
if not isinstance(real_samples, tuple) or len(real_samples) == 1:
interpolates = [interpolates[0]] # strange but necessary
d_interpolates = D(condition, *interpolates)
gradients = torch.autograd.grad(
outputs=d_interpolates,
inputs=interpolates,
grad_outputs=torch.ones(d_interpolates.size()).type_as(item_samples),
create_graph=True,
retain_graph=True,
only_inputs=True,
)[0]
gradients = gradients.flatten(start_dim=1)
if self.hparams.GAN_mode == 'WGAN-GP':
slopes = gradients.norm(2, dim=1)
gradient_penalty = ((slopes - 1) ** 2).mean()
else:
raise NotImplementedError
return gradient_penalty
def pixelwise_score(self, fake, real):
if self.hparams.loss_id == 'L2':
return F.mse_loss(fake, real, reduce=False)
elif self.hparams.loss_id == 'L1':
return F.l1_loss(fake, real, reduce=False)
else:
raise NotImplementedError
def valid_rmse_score(self, y, y_hat, valid_grid_2d):
'''
RMSE score for valid grid only
input (y / y_hat): [B, C, H, W]
valid_grid_2d: [B, 1, H, W]
'''
y = (y * valid_grid_2d).flatten(start_dim=1)
y_hat = (y_hat * valid_grid_2d).flatten(start_dim=1)
return torch.sqrt(F.mse_loss(y, y_hat))
def valid_pcc_score(self, y, y_hat, valid_grid_2d):
'''
Pattern Corr Coeff score for valid grid only
input (y / y_hat): [B, C, H, W]
valid_grid_2d: [B, 1, H, W]
'''
y = (y * valid_grid_2d).flatten(start_dim=1)
y_hat = (y_hat * valid_grid_2d).flatten(start_dim=1)
y_mean = torch.mean(y, dim=-1, keepdim=True)
y_hat_mean = torch.mean(y_hat, dim=-1, keepdim=True)
return torch.sum((y - y_mean) * (y_hat - y_hat_mean)) / \
torch.sqrt(torch.sum((y - y_mean) ** 2) * torch.sum((y_hat - y_hat_mean) ** 2))
def valid_crps_score(self, y, y_hat, valid_grid_2d):
'''
CRPS score for valid grid only
CRPS(F, x) = E_F|X - x| - 1/2 * E_F|X - X'|
y: [B, C, H, W]
y_hat: [E, B, C, H, W] or [B, C, H, W]
valid_grid_2d: [B, 1, H, W]
NOTE: mind that y & y_hat can not be exchanged
'''
# [_, H, W] -> [_, spatial]
y = (y * valid_grid_2d).flatten(start_dim=1)
if y.dim() != y_hat.dim():
valid_grid_2d = valid_grid_2d[None]
y_hat = (y_hat * valid_grid_2d).flatten(start_dim=2)
else:
y_hat = (y_hat * valid_grid_2d).flatten(start_dim=1)
if y.dim() == y_hat.dim(): # single value CRPS degenrated to MAE
return F.l1_loss(y, y_hat)
else:
# squeeze channel dim [B, C, S] -> [B, S], [E, B, C, S] -> [E, B, S]
y = y.squeeze(1) if y.dim() == 3 else y
y_hat = y_hat.squeeze(2) if y_hat.dim() == 4 else y_hat
# make y_hat & y same shape
y_expand = y.unsqueeze(0).expand(y_hat.shape)
CRPS = torch.abs(y_hat - y_expand).mean(axis=(0, 2)) - \
0.5 * (torch.abs(y_hat.unsqueeze(1) - y_hat.unsqueeze(0)).mean(axis=(0, 1, 3)))
# leave batch dim when averaging over ensemble and spatial, then ave
return CRPS.mean()
def log_precip_map(self, names, imgs_list):
'''
names: List[str]
imgs_list: List[torch.Tensor] -> [B, C, H, W]
should be same length
'''
sample = imgs_list[0]
B = sample.shape[0]
idx_sel = torch.randint(0, B, (4,))
imgs = [wandb.Image(imgs[idx_sel],
mode='L',
caption=f"{self.current_epoch:03d} {name}",
) for name, imgs in zip(names, imgs_list)]
wandb.log({'images': imgs})
def get_optimizers(self, params):
if self.hparams.optimizer == "Adam":
opt = optim.Adam(params,
lr=self.hparams.lr,
betas=(self.hparams.b1, self.hparams.b2)
)
elif self.hparams.optimizer == "RMSprop":
opt = optim.RMSprop(params,
lr=self.hparams.lr
)
else:
raise NotImplementedError
return opt
def reparameterize(self, mu, logvar, ensemble=1):
if ensemble == 1:
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
elif ensemble > 1: # [B, L] -> [E, B, L] -> [E*B, L]
_, L = mu.shape
std = torch.exp(0.5 * logvar)\
.unsqueeze(0).expand(ensemble, -1, -1).reshape(-1, L)
mu = mu.unsqueeze(0).expand(ensemble, -1, -1).reshape(-1, L)
eps = torch.randn_like(std)
return mu + eps * std
def kl_divergence(self, mu1, logvar1, mu2, logvar2):
kl_loss = 0.5 * (logvar2 - logvar1 + torch.exp(logvar1) / torch.exp(logvar2) + (mu1 - mu2)**2 / torch.exp(logvar2) - 1)
return torch.mean(kl_loss)
def get_adversarial_loss(self, critic, is_real):
if self.hparams.GAN_mode == 'vanilla':
if is_real:
return nn.BCEWithLogitsLoss()(critic, torch.ones_like(critic).type_as(critic))
else:
return nn.BCEWithLogitsLoss()(critic, torch.zeros_like(critic).type_as(critic))
elif self.hparams.GAN_mode == 'hinge':
if is_real:
return torch.mean(F.relu(1 - critic))
else:
return torch.mean(F.relu(1 + critic))
elif self.hparams.GAN_mode in ['lsGAN']:
if is_real:
return F.mse_loss(critic, torch.ones_like(critic).type_as(critic))
else:
return F.mse_loss(critic, -torch.ones_like(critic).type_as(critic))
elif self.hparams.GAN_mode in ['WGAN', 'WGAN-GP']:
if is_real:
return -torch.mean(critic)
else:
return torch.mean(critic)
else:
raise NotImplementedError
def get_coarse_bool(self, valid_grid_2d):
valid_grid_2d = valid_grid_2d.float()
return F.interpolate(valid_grid_2d, scale_factor=0.1,
mode="bilinear", align_corners=False)\
.bool()
def drop_lead_in_aux(self, info):
'''
drop lead time in aux info
'''
S1, S2, valid_grid_2d, coord, aux = info
mask = torch.ones(aux.size(), dtype=torch.bool)
mask[:, self.hparams.idx_of_lead] = False
aux = aux[mask].view(-1, aux.size(1) - 1)
return S1, S2, valid_grid_2d, coord, aux
class PerfectProgModule(BaseModule):
'''
circu -> tp
dont need lead weighting schedule
'''
def __init__(self,
Func_C2P_config: DictConfig = None,
scheduler: str = None, # [ None | 'ReduceLROnPlateau' ]
*args, **kwargs
):
super(PerfectProgModule, self).__init__(
*args, **kwargs
)
self.save_hyperparameters()
print('haprams:', self.hparams)
self.PPmodel = Func_C2P(**Func_C2P_config)
# train mode
def forward(self, info, circu):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
# obs circu is finer than dyn grid
# thus avgpool to //4 here
circu = F.avg_pool2d(circu, 4)
pred_tp = self.PPmodel(info, circu)
_, _, valid_grid_2d, _, _ = info
return pred_tp * valid_grid_2d
@torch.no_grad()
def inference(self, info, circu):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
pred_tp = self.PPmodel(info, circu)
_, _, valid_grid_2d, _, _ = info
return pred_tp * valid_grid_2d
def common_step(self, batch):
info, circu, obs_tp = batch
_, _, valid_grid_2d, _, _ = info
obs_tp = obs_tp * valid_grid_2d
circu = F.avg_pool2d(circu, 4) # during training
pred_tp = self.PPmodel(info, circu)
pred_tp = pred_tp * valid_grid_2d
return pred_tp, obs_tp
def common_log(self, stage, pred_tp, obs_tp, valid_grid_2d):
self.log(f'{stage}_tp_ssim', ssim(pred_tp, obs_tp))
self.log(f'{stage}_tp_psnr', psnr(pred_tp, obs_tp))
self.log(f'{stage}_tp_rmse', self.valid_rmse_score(pred_tp, obs_tp, valid_grid_2d))
self.log(f'{stage}_tp_pcc', self.valid_pcc_score(pred_tp, obs_tp, valid_grid_2d))
self.log(f'{stage}_tp_crps', self.valid_crps_score(pred_tp, obs_tp, valid_grid_2d))
def training_step(self, batch, batch_idx):
pred_tp, obs_tp = self.common_step(batch)
info, _, _ = batch
_, _, valid_grid_2d, _, _ = info
self.common_log('train', pred_tp, obs_tp, valid_grid_2d)
tp_id_loss = self.pixelwise_score(pred_tp, obs_tp).mean()
return tp_id_loss
def validation_step(self, batch, batch_idx):
pred_tp, obs_tp = self.common_step(batch)
info, _, _ = batch
_, _, valid_grid_2d, _, _ = info
self.common_log('val', pred_tp, obs_tp, valid_grid_2d)
if batch_idx == 0 and self.hparams.log_images:
self.log_precip_map(['OBS tp', 'PRED tp'], [obs_tp, pred_tp])
def test_step(self, batch, batch_idx):
pred_tp, obs_tp = self.common_step(batch)
info, _, _ = batch
_, _, valid_grid_2d, _, _ = info
self.common_log('test', pred_tp, obs_tp, valid_grid_2d)
def configure_optimizers(self):
trainable_params = self.PPmodel.parameters()
if self.hparams.scheduler == 'ReduceLROnPlateau':
opt = self.get_optimizers(trainable_params)
sch = torch.optim.lr_scheduler.ReduceLROnPlateau(
opt, 'min', patience=2, threshold=1e-3,
threshold_mode='abs', min_lr=1e-5, verbose=True)
return {
'optimizer': opt,
'lr_scheduler': sch,
'monitor': 'val_crps'
}
else:
opt = self.get_optimizers(trainable_params)
return opt
class DetermModule(BaseModule):
'''
model state (circu, tp) -> obs state (circu, tp)
pairwise training with deterministic model
constrain only tp here
'''
def __init__(self,
G_D2O_class: str = "Func_C2P", # [ "Func_C2P" | "Generator_D2O" ]
G_D2O_config: DictConfig = None,
scheduler: str = None, # [ None | 'ReduceLROnPlateau' ]
lambda_id: Union[float, int, str] = 1, # some fixed value or 'function'
lead_thres: int = 3, # control lead weighting function
init_lambda_id: int = 1, # control lead weighting function
*args, **kwargs
):
super(DetermModule, self).__init__(
G_D2O_config=G_D2O_config, *args, **kwargs
)
self.save_hyperparameters()
print('haprams:', self.hparams)
G_D2O_config.Net_config.nz = 0 # deterministic
if self.hparams.G_D2O_class == "Func_C2P":
assert G_D2O_config.Net_class == "C2P_Net"
elif self.hparams.G_D2O_class == "Generator_D2O":
assert G_D2O_config.Net_class == "D2O_Net"
G_class = globals()[G_D2O_class]
self.Generator_D2O = G_class(**G_D2O_config)
def forward(self, info, dyn_state):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
_, _, valid_grid_2d, _, _ = info
if self.hparams.G_D2O_class == "Func_C2P":
D2O_tp = self.Generator_D2O(info, dyn_state) # natural deterministic
return D2O_tp * valid_grid_2d
elif self.hparams.G_D2O_class == "Generator_D2O":
_, _, valid_grid_2d, _, _ = info
D2O_circu, D2O_tp = self.Generator_D2O(info, dyn_state, None) # no z means deterministic
return D2O_circu, D2O_tp * valid_grid_2d
@torch.no_grad()
def inference(self, info, dyn_state):
return self.forward(info, dyn_state)
def common_step(self, batch):
info, dyn_state, obs_tp, obs_circu = batch
_, _, valid_grid_2d, _, _ = info
obs_tp = obs_tp * valid_grid_2d
if self.hparams.G_D2O_class == "Func_C2P":
D2O_tp = self.Generator_D2O(info, dyn_state)
D2O_tp = D2O_tp * valid_grid_2d
return D2O_tp, obs_tp
elif self.hparams.G_D2O_class == "Generator_D2O":
D2O_circu, D2O_tp = self.Generator_D2O(info, dyn_state, None) # no z means deterministic
D2O_tp = D2O_tp * valid_grid_2d
return D2O_circu, D2O_tp, obs_circu, obs_tp
def common_log(self, stage, D2O_tp, obs_tp, valid_grid_2d):
# we dont need to check circu in THIS module
self.log(f'{stage}_tp_ssim', ssim(D2O_tp, obs_tp))
self.log(f'{stage}_tp_psnr', psnr(D2O_tp, obs_tp))
self.log(f'{stage}_tp_rmse', self.valid_rmse_score(D2O_tp, obs_tp, valid_grid_2d))
self.log(f'{stage}_tp_pcc', self.valid_pcc_score(D2O_tp, obs_tp, valid_grid_2d))
self.log(f'{stage}_tp_crps', self.valid_crps_score(D2O_tp, obs_tp, valid_grid_2d))
def training_step(self, batch, batch_idx):
if self.hparams.G_D2O_class == "Func_C2P":
D2O_tp, obs_tp = self.common_step(batch)
elif self.hparams.G_D2O_class == "Generator_D2O":
D2O_circu, D2O_tp, obs_circu, obs_tp = self.common_step(batch)
info, _, _, _ = batch
_, _, valid_grid_2d, _, _ = info
self.common_log('train', D2O_tp, obs_tp, valid_grid_2d)
# ID LOSS
tp_id_loss = self.pixelwise_score(D2O_tp, obs_tp)
if self.hparams.lambda_id == 'function':
info, _, _, _ = batch
_, _, _, _, aux = info
lead = aux[:, self.hparams.idx_of_lead]
tp_id_loss = tp_id_loss.mean(axis=(1, 2, 3))
tp_id_loss = L_ID_SCHEDULE(lead, thres=self.hparams.lead_thres, init_lambda=self.hparams.init_lambda_id) * tp_id_loss
else:
tp_id_loss = tp_id_loss * self.hparams.lambda_id
g_loss = tp_id_loss.mean()
return g_loss
def validation_step(self, batch, batch_idx):
if self.hparams.G_D2O_class == "Func_C2P":
D2O_tp, obs_tp = self.common_step(batch)
elif self.hparams.G_D2O_class == "Generator_D2O":
D2O_circu, D2O_tp, obs_circu, obs_tp = self.common_step(batch)
info, _, _, _ = batch
_, _, valid_grid_2d, _, _ = info
self.common_log('val', D2O_tp, obs_tp, valid_grid_2d)
_, dyn_state, _, _ = batch
dyn_tp = dyn_state[:, -1, :, :].unsqueeze(1)
dyn_tp = F.interpolate(dyn_tp, scale_factor=10)
dyn_tp = dyn_tp * valid_grid_2d
if batch_idx == 0 and self.hparams.log_images:
self.log_precip_map(['DYN tp', 'OBS tp', 'PRED tp'], [dyn_tp, obs_tp, D2O_tp])
def test_step(self, batch, batch_idx):
if self.hparams.G_D2O_class == "Func_C2P":
D2O_tp, obs_tp = self.common_step(batch)
elif self.hparams.G_D2O_class == "Generator_D2O":
D2O_circu, D2O_tp, obs_circu, obs_tp = self.common_step(batch)
info, _, _, _ = batch
_, _, valid_grid_2d, _, _ = info
self.common_log('test', D2O_tp, obs_tp, valid_grid_2d)
def configure_optimizers(self):
trainable_params = self.Generator_D2O.parameters()
if self.hparams.scheduler == 'ReduceLROnPlateau':
opt_G = self.get_optimizers(trainable_params)
sch_G = torch.optim.lr_scheduler.ReduceLROnPlateau(
opt_G, 'min', patience=2, threshold=1e-3,
threshold_mode='abs', min_lr=1e-5, verbose=True)
return {
'optimizer': opt_G,
'lr_scheduler': sch_G,
'monitor': 'val_tp_crps'
}
else:
opt_G = self.get_optimizers(trainable_params)
return opt_G
class AdversarialModule_v1(BaseModule):
'''
model state (circu, tp) -> obs state (circu, tp)
pairwise training with adversarial model
adv_loss, id_loss (like pix2pix), inv_loss (constrain circu)
v1: simply add adv_loss and etc
'''
def __init__(self,
G_D2O_config: DictConfig = None,
D_O_config: DictConfig = None,
nz: int = 8,
validation_ensemble: int = 1,
lambda_id: Union[float, int, str] = 1, # some fixed value or 'function'
lambda_id_circu_proportion: Union[float, int] = 0.1, # share schedule w/ lambda_id of tp
lead_thres: int = 3, # control lead weighting function
init_lambda_id: int = 10, # control lead weighting function
lambda_adv: Union[float, int] = 1,
lambda_inv: Union[float, int] = 0.1, # constrain circu only
delta_inv: float = 0,
*args, **kwargs
):
super(AdversarialModule_v1, self).__init__(
G_D2O_config=G_D2O_config, D_O_config=D_O_config, *args, **kwargs
)
self.save_hyperparameters()
print('haprams:', self.hparams)
G_D2O_config.nz = nz # itertively pass to D2O_Net
self.G = Generator_D2O(**G_D2O_config)
self.D = Discriminator_O(**D_O_config)
def forward(self, info, dyn_state):
B = dyn_state.shape[0]; L = self.hparams.nz
z = torch.randn(B, L, 1, 1).type_as(dyn_state) if L > 0 else None
return self.forward_Generator_D2O(self.G, info, dyn_state, z)
@torch.no_grad()
def inference(self, info, dyn_state):
return self.forward(info, dyn_state)
def forward_Generator_D2O(self, model, info, dyn_state, z):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
D2O_circu, D2O_tp = model(info, dyn_state, z)
_, _, valid_grid_2d, _, _ = info
return D2O_circu, D2O_tp * valid_grid_2d
def forward_Discriminator_O(self, model, info, obs_circu, obs_tp):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
_, _, valid_grid_2d, _, _ = info
obs_tp = obs_tp * valid_grid_2d
return model(info, obs_circu, obs_tp)
def common_log(self, stage, pred_tp, obs_tp, pred_circu, obs_circu, valid_grid_2d):
self.log(f'{stage}_tp_ssim', ssim(pred_tp, obs_tp))
self.log(f'{stage}_tp_psnr', psnr(pred_tp, obs_tp))
self.log(f'{stage}_tp_rmse', self.valid_rmse_score(pred_tp, obs_tp, valid_grid_2d))
self.log(f'{stage}_tp_pcc', self.valid_pcc_score(pred_tp, obs_tp, valid_grid_2d))
self.log(f'{stage}_tp_crps', self.valid_crps_score(pred_tp, obs_tp, valid_grid_2d))
# circu not on valid grid
self.log(f'{stage}_circu_rmse', torch.sqrt(F.mse_loss(pred_circu, obs_circu)))
def training_step(self, batch, batch_idx, optimizer_idx):
info, dyn_state, obs_tp, obs_circu = batch
B = dyn_state.shape[0]; L = self.hparams.nz
z = torch.randn(B, L, 1, 1).type_as(dyn_state) if L > 0 else None
_, _, valid_grid_2d, _, aux = info
obs_tp = obs_tp * valid_grid_2d
dyn_circu = dyn_state[:, :-1, :, :]
# train generator
if optimizer_idx == 0:
D2O_circu, D2O_tp = self.forward_Generator_D2O(self.G, info, dyn_state, z)
# --- ADV LOSS ---
fake_critic = self.forward_Discriminator_O(self.D, info, D2O_circu, D2O_tp)
adv_loss = self.get_adversarial_loss(fake_critic, True)
g_loss_adv = adv_loss * self.hparams.lambda_adv
# --- ID LOSS ---
# constrain tp & circu
id_loss = self.pixelwise_score(D2O_tp, obs_tp)
id_loss_circu = self.pixelwise_score(D2O_circu, obs_circu)
if self.hparams.lambda_id == 'function':
lead = aux[:, self.hparams.idx_of_lead]
id_loss = id_loss.mean(axis=(1, 2, 3))
id_loss = L_ID_SCHEDULE(lead, thres=self.hparams.lead_thres, init_lambda=self.hparams.init_lambda_id) * id_loss
id_loss_circu = id_loss_circu.mean(axis=(1, 2, 3))
id_loss_circu = L_ID_SCHEDULE(lead, thres=self.hparams.lead_thres, init_lambda=self.hparams.init_lambda_id) * id_loss_circu
else:
id_loss = id_loss * self.hparams.lambda_id
id_loss_circu = id_loss_circu * self.hparams.lambda_id
g_loss_id = id_loss.mean() + id_loss_circu.mean() * self.hparams.lambda_id_circu_proportion
# --- INV LOSS ---
# constrain pooled circu alone
D2O_circu_pool = F.avg_pool2d(D2O_circu, 4)
inv_loss = self.pixelwise_score(D2O_circu_pool, dyn_circu).mean()
inv_loss = F.relu(inv_loss - self.hparams.delta_inv)
g_loss_inv = inv_loss * self.hparams.lambda_inv
g_loss = g_loss_adv + g_loss_id + g_loss_inv
self.log('train_adv_loss', g_loss_adv)
self.log('train_id_loss', g_loss_id)
self.log('train_inv_loss', g_loss_inv)
self.log('train_g_loss', g_loss, prog_bar=True)
self.common_log('train', D2O_tp, obs_tp, D2O_circu, obs_circu, valid_grid_2d)
return g_loss
# train discriminator
if optimizer_idx == 1:
D2O_circu, D2O_tp = self.forward_Generator_D2O(self.G, info, dyn_state, z)
real_critic = self.forward_Discriminator_O(self.D, info, obs_circu, obs_tp)
fake_critic = self.forward_Discriminator_O(self.D, info, D2O_circu.detach(), D2O_tp.detach())
real_loss = self.get_adversarial_loss(real_critic, True)
fake_loss = self.get_adversarial_loss(fake_critic, False)
d_loss = real_loss + fake_loss
self.log('train_real_critic', torch.mean(real_critic))
self.log('train_fake_critic', torch.mean(fake_critic))
self.log('train_w_dist', torch.mean(real_critic) - torch.mean(fake_critic))
self.log('train_d_loss', d_loss, prog_bar=True)
# constraint on D
if self.hparams.weight_clipping or self.hparams.GAN_mode == 'WGAN':
self.weight_clipping(self.D, self.hparams.clip_value)
elif self.hparams.GAN_mode == 'WGAN-GP':
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
gradient_penalty = self.compute_gradient_penalty(self.D, info, (obs_circu, obs_tp), (D2O_circu, D2O_tp))
d_loss += self.hparams.lambda_gp * gradient_penalty
self.log('loss_gp', gradient_penalty)
return d_loss
def validation_step(self, batch, batch_idx):
info, dyn_state, obs_tp, obs_circu = batch
_, _, valid_grid_2d, _, aux = info
obs_tp = obs_tp * valid_grid_2d
B = dyn_state.shape[0]; L = self.hparams.nz; E = self.hparams.validation_ensemble
if E == 1:
D2O_circu, D2O_tp = self.forward(info, dyn_state)
self.common_log('val', D2O_tp, obs_tp, D2O_circu, obs_circu, valid_grid_2d)
D2O_circu_pool = F.avg_pool2d(D2O_circu, 4)
self.log('val_inv_rmse', torch.sqrt(F.mse_loss(D2O_circu_pool, dyn_state[:, :-1, :, :])))
elif E > 1:
# for each sample, generate E samples
# z = torch.randn(E, B, L).type_as(dyn_state).reshape(-1, L)
dyn_state = dyn_state.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *dyn_state.shape[1:])
info = [item.unsqueeze(0).expand(E, *([-1]*len(item.shape))).reshape(-1, *item.shape[1:]) for item in info]
D2O_circu, D2O_tp = self.forward(info, dyn_state)
D2O_circu_pool = F.avg_pool2d(D2O_circu, 4)
# prob skill for ensemble
obs_tp_expand = obs_tp.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *obs_tp.shape[1:])
obs_circu_expand = obs_circu.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *obs_circu.shape[1:])
valid_grid_2d_expand = valid_grid_2d.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *valid_grid_2d.shape[1:])
self.common_log('val', D2O_tp, obs_tp_expand, D2O_circu, obs_circu_expand, valid_grid_2d_expand)
D2O_tp = D2O_tp.reshape(E, B, *D2O_tp.shape[1:])
self.log('val_ensemble_crps', self.valid_crps_score(y_hat=D2O_tp, y=obs_tp, valid_grid_2d=valid_grid_2d))
# determ skill for ensemble mean
D2O_tp_mean = D2O_tp.mean(dim=0)
D2O_circu_mean = D2O_circu.mean(dim=0)
self.common_log('val_mean', D2O_tp_mean, obs_tp, D2O_circu_mean, obs_circu, valid_grid_2d)
if len(dyn_state.size()) == 4:
self.log('val_inv_rmse', torch.sqrt(F.mse_loss(D2O_circu_pool, dyn_state[:, :-1, :, :])))
elif len(dyn_state.size()) == 5:
self.log('val_inv_rmse', torch.sqrt(F.mse_loss(D2O_circu_pool, dyn_state[:, :, :-1, :, :])))
# retrieve one ensemble member for subsequent image logging
D2O_tp = D2O_tp[0]
dyn_state = dyn_state.reshape(E, B, *dyn_state.shape[1:])
dyn_state = dyn_state[0]
if batch_idx == 0 and self.hparams.log_images:
dyn_tp = dyn_state[:, -1, :, :].unsqueeze(1)
dyn_tp = F.interpolate(dyn_tp, scale_factor=10)
dyn_tp = dyn_tp * valid_grid_2d
self.log_precip_map(['DYN tp', 'OBS tp', 'PRED tp'], [dyn_tp, obs_tp, D2O_tp])
def configure_optimizers(self):
opt_G = self.get_optimizers(self.G.parameters())
opt_D = self.get_optimizers(self.D.parameters())
return (
{'optimizer': opt_G, 'frequency': self.hparams.n_opt_G},
{'optimizer': opt_D, 'frequency': self.hparams.n_opt_D}
)
class AdversarialModule_v3(AdversarialModule_v2):
'''
use cVAE-GAN to constrain circu
w/o O2D model
'''
def __init__(self,
E_D_config: DictConfig = None,
E_O_config: DictConfig = None,
lambda_kl: Union[float, int] = 0.01, # kl loss between Latent_D & Latent_O
*args, **kwargs
):
super(AdversarialModule_v3, self).__init__(
*args, **kwargs
)
self.save_hyperparameters()
E_D_config.latent_dim = self.hparams.nz
E_O_config.latent_dim = self.hparams.nz
self.E_D = Encoder_D(**E_D_config)
self.E_O = Encoder_O(**E_O_config)
def forward(self, info, dyn_state):
mu_D, logvar_D = self.forward_Encoder_D(self.E_D, info, dyn_state)
z_D = self.reparameterize(mu_D, logvar_D)
return self.forward_Generator_D2O(self.G, info, dyn_state, z_D)
def forward_Encoder_O(self, model, info, obs_circu, obs_tp):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
_, _, valid_grid_2d, _, _ = info
obs_tp = obs_tp * valid_grid_2d
return model(info, obs_circu, obs_tp)
def forward_Encoder_D(self, model, info, dyn_state):
if not self.hparams.embed_lead:
info = self.drop_lead_in_aux(info)
_, _, valid_grid_2d, _, _ = info
coarse_valid_grid_2d = self.get_coarse_bool(valid_grid_2d)
dyn_circu = dyn_state[:, :-1, :, :]
dyn_tp = dyn_state[:, -1, :, :].unsqueeze(1)
dyn_tp = dyn_tp * coarse_valid_grid_2d
dyn_state = torch.cat([dyn_circu, dyn_tp], dim=1)
return model(info, dyn_state)
def configure_optimizers(self):
opt_GE = self.get_optimizers(itertools.chain(self.G.parameters(), self.E_D.parameters(), self.E_O.parameters()))
opt_D = self.get_optimizers(self.D.parameters())
return (
{'optimizer': opt_GE, 'frequency': self.hparams.n_opt_G},
{'optimizer': opt_D, 'frequency': self.hparams.n_opt_D}
)
def training_step(self, batch, batch_idx, optimizer_idx):
info, dyn_state, obs_tp, obs_circu = batch
_, _, valid_grid_2d, _, aux = info
obs_tp = obs_tp * valid_grid_2d
B = dyn_state.shape[0]; L = self.hparams.nz; E = self.hparams.validation_ensemble
# latents: [E*B, L]
mu_D, logvar_D = self.forward_Encoder_D(self.E_D, info, dyn_state)
mu_O, logvar_O = self.forward_Encoder_O(self.E_O, info, obs_circu, obs_tp)
# z_D = self.reparameterize(mu_D, logvar_D, ensemble=E)
z_O = self.reparameterize(mu_O, logvar_O, ensemble=E)
# inputs: [E*B, ...]
dyn_state_expand = dyn_state.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *dyn_state.shape[1:])
info_expand = [item.unsqueeze(0).expand(E, *([-1]*len(item.shape))).reshape(-1, *item.shape[1:]) for item in info]
# prepare refs: [E*B, ...] for fields of obs space
obs_tp_expand = obs_tp.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *obs_tp.shape[1:])
obs_circu_expand = obs_circu.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *obs_circu.shape[1:])
valid_grid_2d_expand = valid_grid_2d.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *valid_grid_2d.shape[1:])
# forward
D2O_circu_expand, D2O_tp_expand = self.forward_Generator_D2O(self.G, info_expand, dyn_state_expand, z_O) # [E*B, ...]
# prepare refs: [B, ...] w/ valid_grid for fields of dyn space
dyn_circu = dyn_state[:, :-1, :, :]
dyn_tp = dyn_state[:, -1, :, :].unsqueeze(1)
coarse_valid_grid_2d = self.get_coarse_bool(valid_grid_2d)
dyn_tp = dyn_tp * coarse_valid_grid_2d
# prepare ensemble mean for fields of dyn space: [B, ...]
D2O_tp_em = D2O_tp_expand.reshape(E, B, *D2O_tp_expand.shape[1:]).mean(dim=0)
D2O_circu_em = D2O_circu_expand.reshape(E, B, *D2O_circu_expand.shape[1:]).mean(dim=0)
# train generator & encoder
if optimizer_idx == 0:
# --- ADV LOSS ---
# -- for every member --
fake_critic_O = self.forward_Discriminator_O(self.D, info_expand, D2O_circu_expand, D2O_tp_expand)
adv_loss = self.get_adversarial_loss(fake_critic_O, True)
g_loss_adv = adv_loss * self.hparams.lambda_adv
# --- ID LOSS ---
# -- for ensemble mean --
# constrain D2O & O2D
id_loss = self.pixelwise_score(D2O_tp_em, obs_tp).mean(axis=(1, 2, 3))
id_loss_circu = self.pixelwise_score(D2O_circu_em, obs_circu).mean(axis=(1, 2, 3))
if self.hparams.lambda_id == 'function':
lead = aux[:, self.hparams.idx_of_lead]
id_loss = L_ID_SCHEDULE(lead, thres=self.hparams.lead_thres, init_lambda=self.hparams.init_lambda_id) * id_loss
id_loss_circu = L_ID_SCHEDULE(lead, thres=self.hparams.lead_thres, init_lambda=self.hparams.init_lambda_id) * id_loss_circu
else:
id_loss = id_loss * self.hparams.lambda_id
id_loss_circu = id_loss_circu * self.hparams.lambda_id
g_loss_id = id_loss.mean() + id_loss_circu.mean() * self.hparams.lambda_id_circu_proportion
# --- INV LOSS ---
# -- for every member --
# constrain circu alone, with relax limit
D2O_circu_expand_pool = F.avg_pool2d(D2O_circu_expand, 4)
dyn_circu_expand = dyn_state_expand[:, :-1]
inv_loss_D2O = self.pixelwise_score(D2O_circu_expand_pool, dyn_circu_expand).mean()
inv_loss_D2O = F.relu(inv_loss_D2O - self.hparams.delta_inv)
g_loss_inv = inv_loss_D2O * self.hparams.lambda_inv
# --- KL LOSS ---
# -- for every member --
kl_loss = self.kl_divergence(mu_D, logvar_D, mu_O, logvar_O)
g_loss_kl = kl_loss * self.hparams.lambda_kl
g_loss = g_loss_adv + g_loss_id + g_loss_inv + g_loss_kl
self.log('train_adv_loss', g_loss_adv)
self.log('train_id_loss', g_loss_id)
self.log('train_inv_loss', g_loss_inv)
self.log('train_kl_loss', g_loss_kl)
self.common_log('train', D2O_tp_expand, obs_tp_expand, D2O_circu_expand, obs_circu_expand, valid_grid_2d_expand)
return g_loss
# train discriminator O
# -- fields on O space are probabilistic --
# feed every member to D_o
if optimizer_idx == 1:
real_critic_O = self.forward_Discriminator_O(self.D, info, obs_circu, obs_tp)
fake_critic_O = self.forward_Discriminator_O(self.D, info_expand, D2O_circu_expand.detach(), D2O_tp_expand.detach())
real_loss = self.get_adversarial_loss(real_critic_O, True)
fake_loss = self.get_adversarial_loss(fake_critic_O, False)
d_loss = real_loss + fake_loss
self.log('train_real_critic', torch.mean(real_critic_O))
self.log('train_fake_critic', torch.mean(fake_critic_O))
self.log('train_w_dist', torch.mean(real_critic_O) - torch.mean(fake_critic_O))
self.log('train_d_loss', d_loss, prog_bar=True)
# constraint on D_o
if self.hparams.weight_clipping or self.hparams.GAN_mode == 'WGAN':
self.weight_clipping(self.D, self.hparams.clip_value)
elif self.hparams.GAN_mode == 'WGAN-GP':
if not self.hparams.embed_lead:
info_expand = self.drop_lead_in_aux(info_expand)
gradient_penalty = self.compute_gradient_penalty(self.D, info_expand, (obs_circu_expand, obs_tp_expand), (D2O_circu_expand, D2O_tp_expand))
d_loss += self.hparams.lambda_gp * gradient_penalty
self.log('loss_gp_O', gradient_penalty)
return d_loss
def validation_step(self, batch, batch_idx):
info, dyn_state, obs_tp, obs_circu = batch
_, _, valid_grid_2d, _, _ = info
obs_tp = obs_tp * valid_grid_2d
# --- log sample for [ single ensemble member ] ---
mu_D, logvar_D = self.forward_Encoder_D(self.E_D, info, dyn_state)
z_D = self.reparameterize(mu_D, logvar_D)
mu_O, logvar_O = self.forward_Encoder_O(self.E_O, info, obs_circu, obs_tp)
z_O = self.reparameterize(mu_O, logvar_O)
# Dyn -> Obs
D2O_circu, D2O_tp = self.forward_Generator_D2O(self.G, info, dyn_state, z_O)
_, inference_tp = self.forward_Generator_D2O(self.G, info, dyn_state, z_D)
if batch_idx == 0 and self.hparams.log_images:
dyn_tp = dyn_state[:, -1, :, :].unsqueeze(1)
dyn_tp = F.interpolate(dyn_tp, scale_factor=10)
dyn_tp = dyn_tp * valid_grid_2d
self.log_precip_map(
['dyn tp', 'd2o tp', 'inference tp', 'obs tp'],
[dyn_tp, D2O_tp, inference_tp, obs_tp])
# --- log score for [ ensemble members ] ---
B = dyn_state.shape[0]; L = self.hparams.nz; E = self.hparams.validation_ensemble
if E == 1:
self.common_log('val', D2O_tp, obs_tp, D2O_circu, obs_circu, valid_grid_2d)
D2O_circu_pool = F.avg_pool2d(D2O_circu, 4)
self.log('val_inv_rmse', torch.sqrt(F.mse_loss(D2O_circu_pool, dyn_state[:, :-1, :, :])))
elif E > 1:
z_D = self.reparameterize(mu_D, logvar_D, ensemble=E) # [E*B, L]
dyn_state_expand = dyn_state.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *dyn_state.shape[1:])
info_expand = [item.unsqueeze(0).expand(E, *([-1]*len(item.shape))).reshape(-1, *item.shape[1:]) for item in info]
D2O_circu_expand, D2O_tp_expand = self.forward_Generator_D2O(self.G, info_expand, dyn_state_expand, z_D)
# prob skill for ensemble
obs_tp_expand = obs_tp.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *obs_tp.shape[1:])
obs_circu_expand = obs_circu.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *obs_circu.shape[1:])
valid_grid_2d_expand = valid_grid_2d.unsqueeze(0).expand(E, -1, -1, -1, -1).reshape(-1, *valid_grid_2d.shape[1:])
self.common_log('val', D2O_tp_expand, obs_tp_expand, D2O_circu_expand, obs_circu_expand, valid_grid_2d_expand)
D2O_tp_ensemble = D2O_tp_expand.reshape(E, B, *inference_tp.shape[1:])
D2O_circu_ensemble = D2O_circu_expand.reshape(E, B, *D2O_circu.shape[1:])
self.log('val_ensemble_crps', self.valid_crps_score(y_hat=D2O_tp_ensemble, y=obs_tp, valid_grid_2d=valid_grid_2d))
# determ skill for ensemble mean
D2O_tp_em = D2O_tp_ensemble.mean(dim=0)
D2O_circu_em = D2O_circu_ensemble.mean(dim=0)
self.common_log('val_mean', D2O_tp_em, obs_tp, D2O_circu_em, obs_circu, valid_grid_2d)
# inv skill for every member
D2O_circu_pool_expand = F.avg_pool2d(D2O_circu_expand, 4)
if len(dyn_state.size()) == 4:
self.log('val_inv_rmse', torch.sqrt(F.mse_loss(D2O_circu_pool_expand, dyn_state_expand[:, :-1, :, :])))
elif len(dyn_state.size()) == 5:
self.log('val_inv_rmse', torch.sqrt(F.mse_loss(D2O_circu_pool_expand, dyn_state_expand[:, :, :-1, :, :])))
class CycleGanModule(BaseModule):
'''
w/ 2 generators and 2 discriminators
ADV loss + ID loss + Cycle loss + INV loss
ID loss constrain circu & tp
INV loss constrain circu only, with fixed relax limit
D2O & O2D models are deterministic
'''
def __init__(self,
G_D2O_config: DictConfig = None,
D_O_config: DictConfig = None,
G_O2D_config: DictConfig = None,
D_D_config: DictConfig = None,
lambda_id: Union[float, int, str] = 1, # some fixed value or 'function'
lambda_id_circu_proportion: Union[float, int] = 1, # share schedule w/ lambda_id of tp
lead_thres: int = 3, # control lead weighting function
init_lambda_id: int = 10, # control lead weighting function
lambda_adv: Union[float, int] = 1,
lambda_cycle: Union[float, int] = 10,
lambda_inv: Union[float, int] = 0.1, # constrain circu only
delta_inv: float = 0.01,
n_opt_Dd: int = None,
n_opt_Do: int = None,
lambda_gp_d: float = None,
lambda_gp_o: float = None,
*args, **kwargs
):
super(CycleGanModule, self).__init__(
G_D2O_config=G_D2O_config, D_O_config=D_O_config,
G_O2D_config=G_O2D_config, D_D_config=D_D_config,
*args, **kwargs
)
self.save_hyperparameters()
print('haprams:', self.hparams)
if n_opt_Dd is None:
self.hparams.n_opt_Dd = self.hparams.n_opt_D
if n_opt_Do is None:
self.hparams.n_opt_Do = self.hparams.n_opt_G
if lambda_gp_d is None:
self.hparams.lambda_gp_d = self.hparams.lambda_gp
if lambda_gp_o is None: