-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathdistributions.py
57 lines (44 loc) · 1.82 KB
/
distributions.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
import torch
from equivariant_diffusion.utils import \
center_gravity_zero_gaussian_log_likelihood_with_mask, \
standard_gaussian_log_likelihood_with_mask, \
center_gravity_zero_gaussian_log_likelihood, \
sample_center_gravity_zero_gaussian_with_mask, \
sample_center_gravity_zero_gaussian, \
sample_gaussian_with_mask
class PositionFeaturePrior(torch.nn.Module):
def __init__(self, n_dim, in_node_nf):
super().__init__()
self.n_dim = n_dim
self.in_node_nf = in_node_nf
def forward(self, z_x, z_h, node_mask=None):
assert len(z_x.size()) == 3
assert len(node_mask.size()) == 3
assert node_mask.size()[:2] == z_x.size()[:2]
assert (z_x * (1 - node_mask)).sum() < 1e-8 and \
(z_h * (1 - node_mask)).sum() < 1e-8, \
'These variables should be properly masked.'
log_pz_x = center_gravity_zero_gaussian_log_likelihood_with_mask(
z_x, node_mask
)
log_pz_h = standard_gaussian_log_likelihood_with_mask(
z_h, node_mask
)
log_pz = log_pz_x + log_pz_h
return log_pz
def sample(self, n_samples, n_nodes, node_mask):
z_x = sample_center_gravity_zero_gaussian_with_mask(
size=(n_samples, n_nodes, self.n_dim), device=node_mask.device,
node_mask=node_mask)
z_h = sample_gaussian_with_mask(
size=(n_samples, n_nodes, self.in_node_nf), device=node_mask.device,
node_mask=node_mask)
return z_x, z_h
class PositionPrior(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return center_gravity_zero_gaussian_log_likelihood(x)
def sample(self, size, device):
samples = sample_center_gravity_zero_gaussian(size, device)
return samples