-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion.py
More file actions
1175 lines (879 loc) · 34 KB
/
diffusion.py
File metadata and controls
1175 lines (879 loc) · 34 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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 23 18:59:12 2022
@author: huzongxiang
"""
import math
import numpy as np
from typing import List, Union, Sequence
from tensor import Tensor
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.constraints import NonNeg
from tensorflow.keras import layers
from egnn_diffusion import Egnn_diffusion
from schedule import Gamma, NoiseSchedule
from utils import (extract,
gravity_to_zero,
# assert_gravity_to_zero,
gaussian_kl,
gaussian_kl_subspace,
standard_cdf,
assert_err_values,
)
class VariationalGaussianDiffusion(layers.Layer):
def __init__(
self,
dynamics:Egnn_diffusion,
schedule:str="builtin",
batch_size:int=32,
node_dim:int=20,
x_dim:int=3,
timesteps:int=1000,
pattern:str="noise",
l2_loss:bool=True,
min_signal_rate:float=0.02,
max_signal_rate:float=0.95,
clip_noise:bool=True,
re_project:bool=True,
scaling:float=0.25,
epsilon:float=1e-4,
num_classes:int=20,
**kwargs,
):
super().__init__(**kwargs)
# assert pattern in ["denoising", "noise", "score"]
self.batch_size = batch_size
self.dynamics = dynamics
self.schedule = schedule
self.builtin_schedule = True
self.min_signal_rate = min_signal_rate
self.max_signal_rate = max_signal_rate
if self.schedule == "builtin":
self.builtin_schedule = True
self.min_signal_rate = min_signal_rate
self.max_signal_rate = max_signal_rate
elif schedule == "learned":
self.builtin_schedule = False
self.gamma = Gamma()
elif schedule == "predefined":
self.builtin_schedule = False
self.gamma = NoiseSchedule(timesteps=timesteps)
else:
raise ValueError(f"unknown schedule {schedule}, should be 'builtin', 'learned' and 'predefined'.")
self.node_dim = node_dim
self.x_dim = x_dim
self.timesteps = timesteps
self.pattern = pattern
self.l2_loss = l2_loss
self.clip_noise = clip_noise
self.re_project = re_project
self.scaling = scaling
self.epsilon = epsilon
self.num_class = num_classes
def cal_num_nodes_batch(self, graph_indices: Tensor) -> Tensor:
"""
Parameters
----------
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
num_nodes = tf.cast(tf.math.bincount(graph_indices), dtype=self.dtype)[:, None]
return num_nodes
def sample_s_t(self, graph_indices: Tensor) -> Sequence:
"""
Sample t from a uniform distribution.
Parameters
----------
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
start = 1
if self.trainable:
start = 0
i_t = tf.random.uniform((self.batch_size, 1), start, self.timesteps + 1, dtype=tf.int32)
i_t = tf.gather(i_t, graph_indices)
i_s = i_t - 1
s = tf.cast(i_s / self.timesteps, dtype=self.dtype)
t = tf.cast(i_t / self.timesteps, dtype=self.dtype)
return s, t
def snr(self, gamma: Tensor) -> Tensor:
"""
SNR(t) = exp(-γ(t))
Parameters
----------
gamma : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
return tf.math.exp(-gamma)
def alpha(self, gamma: Tensor, tensor: Tensor, tensor_: Union[Tensor, None]=None) -> Tensor:
"""
alpha = sqrt(sigmoid(-γ)) = sqrt[SNR(t)/(1 + SNR(t))]
Parameters
----------
gamma : Tensor
DESCRIPTION.
tensor : Tensor
DESCRIPTION.
tensor_ : Union[Tensor, None], optional
DESCRIPTION. The default is None.
Returns
-------
Tensor
DESCRIPTION.
"""
alpha = tf.math.sqrt(tf.math.sigmoid(-gamma))
if tensor_ is not None:
return extract(alpha, tensor), extract(alpha, tensor_)
return extract(alpha, tensor)
def sigma(self, gamma: Tensor, tensor: Tensor, tensor_: Union[Tensor, None]=None) -> Union[Tensor, Sequence]:
"""
sigma = sqrt(sigmoid(γ)) = sqrt[1/(1 + SNR(t))]
Parameters
----------
gamma : Tensor
DESCRIPTION.
tensor : Tensor
DESCRIPTION.
tensor_ : Union[Tensor, None], optional
DESCRIPTION. The default is None.
Returns
-------
Union[Tensor, Sequence]
DESCRIPTION.
"""
sigma = tf.math.sqrt(tf.math.sigmoid(gamma))
if tensor_ is not None:
return extract(sigma, tensor), extract(sigma, tensor_)
return extract(sigma, tensor)
def alpha_and_sigma(self, t: Tensor, tensor: Tensor) -> Tensor:
"""
Parameters
----------
gamma : Tensor
DESCRIPTION.
tensor : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
gamma = self.gamma(t)
return self.alpha(gamma, tensor), self.sigma(gamma, tensor)
def diffusion_schedule(self, t: Tensor, h: Tensor) -> Sequence:
"""
builtin diffusion schedule
diffusion times -> angles
Parameters
----------
t : Tensor
DESCRIPTION.
h : Tensor
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
start_angle = tf.acos(self.max_signal_rate)
end_angle = tf.acos(self.min_signal_rate)
diffusion_angles = start_angle + t * (end_angle - start_angle)
# angles -> signal and noise rates
signal_rates = tf.cos(diffusion_angles)
noise_rates = tf.sin(diffusion_angles)
# note that their squared sum is always: sin^2(x) + cos^2(x) = 1
return extract(signal_rates, h), extract(noise_rates, h)
def sample_eps_hx_t(self, h: Tensor, x: Tensor, graph_indices: Tensor) -> Sequence:
"""
Parameters
----------
h : Tensor
DESCRIPTION.
x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
# assert self.node_dim == tf.shape(h)[-1]
# assert self.x_dim == tf.shape(x)[-1]
eps_h = tf.random.normal(shape=tf.shape(h), mean=0.0, stddev=1.0)
eps_x_gravity = tf.random.normal(shape=tf.shape(x), mean=0.0, stddev=1.0)
eps_x = gravity_to_zero(eps_x_gravity, graph_indices)
# assert_gravity_to_zero(eps_x, graph_indices)
return eps_h, eps_x
def kl_prior(self, h: Tensor, x: Tensor, graph_indices: Tensor) -> Tensor:
"""
Computes the KL divergence between q(z1|hx) and the prior p(z1).
p(z1) = Normal(0, I), μ = 0, σ = I
q(z1|x_h) = Normal(z1|alpha_1*x_h, sigma_1_sq*I), μ = alpha_1*x_h, σ = sigma_1_sq*I
q(z1|x_h) = q(z1_x|x) * q(z1_h|h)
q(z1_x|x) should be invariant with respect to x
This term approximates zero.
Parameters
----------
h : Tensor
DESCRIPTION.
x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
# assert self.node_dim == tf.shape(h)[-1]
# assert self.x_dim == tf.shape(x)[-1]
ones = tf.ones(shape=(tf.shape(graph_indices)[0], 1))
if self.builtin_schedule:
alpha_1, sigma_1 = self.diffusion_schedule(ones, h)
else:
alpha_1, sigma_1 = self.alpha_and_sigma(ones, h)
mu_1_h = alpha_1 * h
mu_1_x = alpha_1 * x
zeros, ones = tf.zeros_like(mu_1_h), tf.ones_like(sigma_1)
kl_h = gaussian_kl(mu_1_h, sigma_1, zeros, ones)
kl_h = tf.math.segment_sum(kl_h, graph_indices)
num_nodes = self.cal_num_nodes_batch(graph_indices)
d_sub = (num_nodes - 1) * self.x_dim
d_sub = tf.gather(d_sub, graph_indices)
zeros, ones = tf.zeros_like(mu_1_x), tf.ones_like(sigma_1)
kl_x = gaussian_kl_subspace(mu_1_x, sigma_1, zeros, ones, d_sub)
kl_x = tf.math.segment_sum(kl_x, graph_indices)
return kl_h + kl_x
def neural(self, h: Tensor, x: Tensor, t: Tensor,
node_indices: Tensor, pair_indices: Tensor) -> Sequence:
"""
Equivariant graph neural network recovers the denoising h, x or predicts noise epsilon_h, epsilon_x.
Parameters
----------
h : Tensor
DESCRIPTION.
x : Tensor
DESCRIPTION.
t : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
h_theta, x_theta = self.dynamics([h, x, t, node_indices, pair_indices])
return h_theta, x_theta
def diffusion_loss_t(self, pred_h: Tensor, pred_x: Tensor, noisy_h: Tensor, noisy_x: Tensor,
t: Tensor, s: Tensor, graph_indices: Tensor) -> Tensor:
"""
Calculate t-th loss during diffusion.
denoising mode: L_t = [(SNR(s) − SNR(t)) ||hx − hx_θ(zt; t)||^2] / 2
noise mode: L_t = [(exp(γ(t) − γ(s)) − 1)||epsilon − epsilon_θ(zt; t)||^2] / 2
NOTICE: As above formula described, this term is always larger than zero !
Parameters
----------
h_theta : Tensor
DESCRIPTION.
x_theta : Tensor
DESCRIPTION.
h_ze : Tensor
DESCRIPTION.
x_ze : Tensor
DESCRIPTION.
gamma_t : Tensor
DESCRIPTION.
gamma_s : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Raises
------
ValueError
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
# assert tf.shape(h_theta)[-1] == tf.shape(h_ze)[-1] and tf.shape(x_theta)[-1] == tf.shape(x_ze)[-1]
noisy_hx = tf.concat([noisy_h, noisy_x], axis=-1)
pred_hx= tf.concat([pred_h, pred_x], axis=-1)
normal = tf.math.square(noisy_hx - pred_hx)
if self.trainable and self.l2_loss:
weight_snr_s_t = tf.ones_like(normal)
diffusion_loss = 0.5 * weight_snr_s_t * normal
diffusion_loss = tf.math.segment_mean(diffusion_loss, graph_indices)
return tf.reduce_mean(diffusion_loss, axis=-1)
gamma_s = self.gamma(s)
gamma_t = self.gamma(t)
if self.pattern == "denoising":
weight_snr_s_t = self.snr(gamma_s) - self.snr(gamma_t)
weight_snr_s_t = extract(weight_snr_s_t, normal)
elif self.pattern == "noise":
weight_snr_s_t = tf.math.expm1(gamma_t - gamma_s)
weight_snr_s_t = extract(weight_snr_s_t, normal)
else:
raise ValueError(self.pattern)
diffusion_loss = 0.5 * weight_snr_s_t * normal
diffusion_loss = tf.math.segment_sum(diffusion_loss, graph_indices)
return tf.reduce_sum(diffusion_loss, axis=-1)
def log_z_x(self, x: Tensor, graph_indices: Tensor) -> Tensor:
"""
Compute logZ
constant Z = [(sqrt(2*pi)*σ0/α0)]**((M-1)*n), M: num of nodes, n: dimension
logZ = (M-1)*n*[log(σ0/α0) + log(2*pi)/2]
σ_t^2/α_t^2 = 1/SNR(t) = exp(gamma_t)
log(σ_t/α_t) = 0.5 * gamma_t
Parameters
----------
x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
# assert self.x_dim == tf.shape(x)[-1]
num_nodes = self.cal_num_nodes_batch(graph_indices)
degrees_of_freedom = (num_nodes - 1) * self.x_dim
zeros = tf.zeros((tf.shape(num_nodes)[0], 1))
gamma_0 = self.gamma(zeros)
# log_0_x = log(σ_0/α_0) = 0.5 * gamma_0, it's far less than 0
log_0_x = 0.5 * gamma_0
return degrees_of_freedom * (log_0_x + 0.5 * tf.math.log(2 * math.pi))
def log_px_given_z0(self, pred_x: Tensor, noise_x: Tensor, graph_indices: Tensor) -> Tensor:
"""
Parameters
----------
x_theta : Tensor
DESCRIPTION.
x_ze : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
normal = tf.math.square(noise_x - pred_x)
reconstruction_loss = 0.5 * normal
reconstruction_loss = tf.math.segment_sum(reconstruction_loss, graph_indices)
if self.trainable and self.l2_loss:
return - tf.math.reduce_sum(reconstruction_loss, axis=-1)
log_z = self.log_z_x(pred_x, graph_indices)
return - tf.math.reduce_sum(reconstruction_loss + log_z, axis=-1)
def log_ph_given_z0(self, h: Tensor, z0_h: Tensor, sigma_0_h: Tensor, graph_indices: Tensor) -> Tensor:
"""
h: one_hot encoding in number of classes of h as [0, 0, 0, 1, 0 ...]
Parameters
----------
h : Tensor
DESCRIPTION.
z0_h : Tensor
DESCRIPTION.
sigma_0_h : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
h = h / self.scaling
z0_h = z0_h / self.scaling
sigma_0_h = sigma_0_h / self.scaling
ph_given_z0_unnormal = standard_cdf((1.5 - z0_h) / sigma_0_h) - standard_cdf((0.5 - z0_h) / sigma_0_h)
log_ph_given_z0_unnormal = tf.math.log(ph_given_z0_unnormal + self.epsilon)
assert_err_values(log_ph_given_z0_unnormal)
log_ph_given_z0_probabilities = tf.math.log_softmax(log_ph_given_z0_unnormal + self.epsilon, axis=-1)
assert_err_values(log_ph_given_z0_probabilities)
log_ph_given_z0 = tf.math.segment_sum(log_ph_given_z0_probabilities * h, graph_indices)
return tf.math.reduce_sum(log_ph_given_z0, axis=-1)
def noisy_loss(self, h: Tensor, x: Tensor,
pred_noisy_h: Tensor, pred_noisy_x: Tensor, graph_indices: Tensor) -> Tensor:
"""
Parameters
----------
h : Tensor
DESCRIPTION.
x : Tensor
DESCRIPTION.
pred_noisy_h : Tensor
DESCRIPTION.
pred_noisy_x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
noisy_hx = tf.concat([pred_noisy_h, pred_noisy_x], axis=-1)
hx = tf.concat([h, x], axis=-1)
loss_hx = tf.math.square(noisy_hx - hx)
noisy_loss = tf.math.segment_mean(loss_hx, graph_indices)
return tf.math.reduce_mean(noisy_loss, axis=-1)
def calculate_loss(self, h: Tensor, x: Tensor, node_indices: Tensor,
pair_indices: Tensor, graph_indices: Tensor) -> Tensor:
"""
-VLB = Dkl(q(z1|hx)||p(z1)) + Dkl(q(s|t,hx)||(p(s||t))) - log(p(hx||z0))
Parameters
----------
h : Tensor
DESCRIPTION.
x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
# 0 < s, t <= 1, diffusion loss
s, t = self.sample_s_t(graph_indices)
if self.builtin_schedule:
alpha_t, sigma_t = self.diffusion_schedule(t, h)
else:
alpha_t, sigma_t = self.alpha_and_sigma(t, h)
noise_h, noise_x = self.sample_eps_hx_t(h, x, graph_indices)
noisy_h = alpha_t * h + sigma_t * noise_h
noisy_x = alpha_t * x + sigma_t * noise_x
pred_noise_h, pred_noise_x = self.neural(noisy_h, noisy_x, t, node_indices, pair_indices)
diffusion_loss_t = self.diffusion_loss_t(pred_noise_h, pred_noise_x, noise_h, noise_x,
t, s, graph_indices)
if self.clip_noise:
pred_noise_h = tf.clip_by_value(pred_noise_h,
clip_value_min=(noisy_h - alpha_t) / sigma_t,
clip_value_max=(noisy_h + alpha_t) / sigma_t,
)
# pred_noise_x = tf.clip_by_value(pred_noise_x,
# clip_value_min=(noisy_x - alpha_t) / sigma_t,
# clip_value_max=(noisy_x + alpha_t) / sigma_t,
# )
pred_true_h = (noisy_h - sigma_t * pred_noise_h) / alpha_t
pred_true_x = (noisy_x - sigma_t * pred_noise_x) / alpha_t
pred_true_h = pred_true_h / self.scaling
# one-hot
pred_true_h = tf.one_hot(tf.argmax(pred_true_h, axis=-1), self.num_class)
noisy_loss = self.noisy_loss(h, x, pred_true_h, pred_true_x, graph_indices)
# t = 1, kl prior loss
if self.builtin_schedule:
kl_prior_loss = 0.0
else:
kl_prior_loss = self.kl_prior(h, x, graph_indices)
# t = 0, reconstruction loss
if self.trainable:
reconstruction_loss_x = - self.log_px_given_z0(pred_noise_x, noise_x, graph_indices)
reconstruction_loss_h = - self.log_ph_given_z0(h, noisy_h, sigma_t, graph_indices)
reconstruction_loss = reconstruction_loss_h + reconstruction_loss_x
is_t0 = tf.cast((t == 0), dtype=self.dtype)
reconstruction_loss = reconstruction_loss * is_t0
diffusion_loss_t = (1.0 - is_t0) * diffusion_loss_t
if self.trainable and self.l2_loss:
diffusion_loss = diffusion_loss_t + reconstruction_loss
else:
diffusion_loss = (self.timesteps + 1) * (diffusion_loss_t + reconstruction_loss)
loss = kl_prior_loss + diffusion_loss
return loss, noisy_loss
else:
t_0 = tf.zeros_like(graph_indices)[:, None]
alpha_0, sigma_0 = self.alpha_and_sigma(t_0, h)
noise_h0, noise_x0 = self.sample_eps_hx_t(h, x, graph_indices)
noisy_h0 = alpha_0 * h + sigma_0 * noise_h0
noisy_x0 = alpha_0 * x + sigma_0 * noise_x0
pred_h_0, pred_x_0 = self.neural(noisy_h0, noisy_x0, t_0, node_indices, pair_indices)
reconstruction_loss_x = - self.log_px_given_z0(pred_x_0, noise_x0, graph_indices)
reconstruction_loss_h = - self.log_ph_given_z0(h, noisy_h0, sigma_0, graph_indices)
reconstruction_loss = reconstruction_loss_h + reconstruction_loss_x
# print(kl_prior_loss, diffusion_loss_t, reconstruction_loss)
diffusion_loss = self.timesteps * diffusion_loss_t
loss = kl_prior_loss + diffusion_loss + reconstruction_loss
return loss, noisy_loss
def forward_paras(self, s: Tensor, t: Tensor, tensor: Tensor) -> Sequence:
"""
Compute parameters t given s in forward diffusion process.
alpha_t_given_s = alpha_t /_alpha_s
sigma_t_given_s_sq = -expm1(softplus(γ(s)) - softplus(γ(t)))
Parameters
----------
gamma_s : Tensor
DESCRIPTION.
gamma_t : Tensor
DESCRIPTION.
tensor : Tensor
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
gamma_s = self.gamma(s)
gamma_t = self.gamma(t)
sigma_t_given_s_2 = -tf.math.expm1(tf.math.softplus(gamma_s) - tf.math.softplus(gamma_t))
sigma_t_given_s_2 = extract(sigma_t_given_s_2, tensor)
sigma_t_given_s = tf.math.sqrt(sigma_t_given_s_2)
return sigma_t_given_s, sigma_t_given_s_2
def calc_mu_0(self, h_theta_0: Tensor, x_theta_0: Tensor, z0_h: Tensor, z0_x: Tensor,
alpha_0: Tensor, sigma_0: Tensor) -> Sequence:
"""
Parameters
----------
h_theta_0 : Tensor
DESCRIPTION.
x_theta_0 : Tensor
DESCRIPTION.
z0_h : Tensor
DESCRIPTION.
z0_x : Tensor
DESCRIPTION.
alpha_0_h : Tensor
DESCRIPTION.
sigma_0_h : Tensor
DESCRIPTION.
alpha_0_x : Tensor
DESCRIPTION.
sigma_0_x : Tensor
DESCRIPTION.
Raises
------
ValueError
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
if self.pattern == "denoising":
h = h_theta_0
x = x_theta_0
elif self.pattern == "noise":
eps0_h = h_theta_0
eps0_x = x_theta_0
h = 1. / alpha_0 * (z0_h - sigma_0 * eps0_h)
x = 1. / alpha_0 * (z0_x - sigma_0 * eps0_x)
elif self.pattern == "score":
score_0_h = h_theta_0
score_0_x = x_theta_0
h = 1. / alpha_0 * (z0_h + tf.math.square(sigma_0) * score_0_h)
x = 1. / alpha_0 * (z0_x + tf.math.square(sigma_0) * score_0_x)
else:
raise ValueError(self.pattern)
return h, x
def calc_mu_theta_t(self, t: Tensor, s: Tensor,
zt_h: Tensor, zt_x: Tensor,
node_indices: Tensor,pair_indices: Tensor) -> Sequence:
"""
Parameters
----------
t : Tensor
DESCRIPTION.
s : Tensor
DESCRIPTION.
zt_h : Tensor
DESCRIPTION.
zt_x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Raises
------
ValueError
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
z_t = tf.concat([zt_h, zt_x], axis=-1)
alpha_s, sigma_s = self.alpha_and_sigma(s, z_t)
alpha_t, sigma_t = self.alpha_and_sigma(t, z_t)
sigma_s_2 = tf.math.square(sigma_s)
sigma_t_2 = tf.math.square(sigma_t)
alpha_t_given_s = alpha_t / alpha_s
sigma_t_given_s, sigma_t_given_s_2 = \
self.forward_paras(s, t, z_t)
h_theta_t, x_theta_t = self.neural(zt_h, zt_x, t, node_indices, pair_indices)
if self.clip_noise:
h_theta_t = tf.clip_by_value(h_theta_t,
clip_value_min=(zt_h - alpha_t) / sigma_t,
clip_value_max=(zt_h + alpha_t) / sigma_t,
)
hx_theta_t = tf.concat([h_theta_t, x_theta_t], axis=-1)
if self.pattern == "denoising":
hx_t = hx_theta_t
mu_theta = (alpha_t_given_s * sigma_s_2 * z_t + alpha_s * sigma_t_given_s_2 * hx_t) / sigma_t_2
elif self.pattern == "noise":
eps_t = hx_theta_t
mu_theta = (z_t - sigma_t_given_s_2 / sigma_t * eps_t) / alpha_t_given_s
elif self.pattern == "score":
score_t = h_theta_t
mu_theta = (z_t + sigma_t_given_s_2 * score_t) / alpha_t_given_s
else:
raise ValueError(self.pattern)
sigma_theta = sigma_t_given_s * sigma_s / sigma_t
return mu_theta, sigma_theta
def sample_hx_given_z0(self, z0_h: Tensor, z0_x: Tensor, node_indices: Tensor,
pair_indices: Tensor, graph_indices: Tensor) -> Sequence:
"""
Sample hx from p(hx|z_0)
Parameters
----------
z0_h : Tensor
DESCRIPTION.
z0_x : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Sequence
DESCRIPTION.
"""
t_0 = tf.zeros_like(graph_indices, dtype=self.dtype)[:, None]
if self.builtin_schedule:
alpha_0, sigma_0 = self.diffusion_schedule(t_0, z0_h)
else:
alpha_0, sigma_0 = self.alpha_and_sigma(t_0, z0_h)
h_theta_0, x_theta_0 = self.neural(z0_h, z0_x, t_0, node_indices, pair_indices)
mu_0_h, mu_0_x = self.calc_mu_0(h_theta_0, x_theta_0, z0_h, z0_x, alpha_0, sigma_0)
eps0_h, eps0_x = self.sample_eps_hx_t(z0_h, z0_x, graph_indices)
h = mu_0_h + sigma_0 * eps0_h
x = mu_0_x + sigma_0 * eps0_x
# h is scaled to 0.25*h at initial, recaling to h
h = h / self.scaling
# one-hot
h = tf.one_hot(tf.argmax(h, axis=-1), self.num_class)
return h, x
def sample_zs_given_zt(self, t: Tensor, s: Tensor, z_t: Tensor,
node_indices: Tensor, pair_indices: Tensor, graph_indices: Tensor) -> Tensor:
"""
Parameters
----------
t : Tensor
DESCRIPTION.
s : Tensor
DESCRIPTION.
z_t : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
zt_h = z_t[:, : self.node_dim]
zt_x = z_t[:, self.node_dim:]
mu_theta, sigma_theta = self.calc_mu_theta_t(t, s, zt_h, zt_x, node_indices, pair_indices)
noise_h, noise_x = self.sample_eps_hx_t(zt_h, zt_x, graph_indices)
noise = tf.concat([noise_h, noise_x], axis=-1)
z_s = mu_theta + sigma_theta * noise
if self.re_project:
zs_h = z_s[:, : self.node_dim]
zs_x = z_s[:, self.node_dim:]
zs_x = gravity_to_zero(zs_x, graph_indices)
# assert_gravity_to_zero(zs_x, graph_indices)
z_s = tf.concat([zs_h, zs_x], axis=-1)
return z_s
def sample_z1(self, graph_indices: Tensor, n_nodes: Tensor) -> Tensor:
"""
Parameters
----------
graph_indices : Tensor
DESCRIPTION.
n_nodes : Tensor
DESCRIPTION.
Returns
-------
Tensor
DESCRIPTION.
"""
z1_h = tf.random.normal(shape=(n_nodes, self.node_dim), mean=0.0, stddev=1.0)
zx_gravity = tf.random.normal(shape=(n_nodes, self.x_dim), mean=0.0, stddev=1.0)
z1_x = gravity_to_zero(zx_gravity, graph_indices)
# assert_gravity_to_zero(z1_x, graph_indices)
z1 = tf.concat([z1_h, z1_x], axis=-1)
return z1
def sample_builtin(self, t: Tensor, s: Tensor, noisy_hx: Tensor,
node_indices: Tensor, pair_indices: Tensor, graph_indices: Tensor):
"""
Parameters
----------
t : Tensor
DESCRIPTION.
s : Tensor
DESCRIPTION.
noisy_hx : Tensor
DESCRIPTION.
node_indices : Tensor
DESCRIPTION.
pair_indices : Tensor
DESCRIPTION.
graph_indices : Tensor
DESCRIPTION.
Returns
-------
noisy_hxs : TYPE
DESCRIPTION.
"""
alpha_s, sigma_s = self.diffusion_schedule(s, noisy_hx)
alpha_t, sigma_t = self.diffusion_schedule(t, noisy_hx)
noisy_h = noisy_hx[:, : self.node_dim]
noisy_x = noisy_hx[:, self.node_dim:]
pred_noise_h, pred_noise_x = self.neural(noisy_h, noisy_x, t, node_indices, pair_indices)
if self.clip_noise:
pred_noise_h = tf.clip_by_value(pred_noise_h,
clip_value_min=(noisy_h - alpha_t) / sigma_t,
clip_value_max=(noisy_h + alpha_t) / sigma_t,
)
pred_h = (noisy_h - sigma_t * pred_noise_h) / alpha_t
pred_x = (noisy_x - sigma_t * pred_noise_x) / alpha_t