-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalizingFlowDensity.py
More file actions
46 lines (36 loc) · 1.63 KB
/
Copy pathNormalizingFlowDensity.py
File metadata and controls
46 lines (36 loc) · 1.63 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
import torch
from pyro.distributions.transforms.planar import Planar
from pyro.distributions.transforms.radial import Radial
from pyro.distributions.transforms.affine_autoregressive import AffineAutoregressive, affine_autoregressive
from torch import nn
import torch.distributions as tdist
class NormalizingFlowDensity(nn.Module):
def __init__(self, dim, flow_length, flow_type='planar_flow'):
super(NormalizingFlowDensity, self).__init__()
self.dim = dim
self.flow_length = flow_length
self.flow_type = flow_type
self.mean = nn.Parameter(torch.zeros(self.dim), requires_grad=False)
self.cov = nn.Parameter(torch.eye(self.dim), requires_grad=False)
if self.flow_type == 'radial_flow':
self.transforms = nn.Sequential(*(
Radial(dim) for _ in range(flow_length)
))
elif self.flow_type == 'iaf_flow':
self.transforms = nn.Sequential(*(
affine_autoregressive(dim, hidden_dims=[128, 128]) for _ in range(flow_length)
))
else:
raise NotImplementedError
def forward(self, z):
sum_log_jacobians = 0
for transform in self.transforms:
z_next = transform(z)
sum_log_jacobians = sum_log_jacobians + transform.log_abs_det_jacobian(z, z_next)
z = z_next
return z, sum_log_jacobians
def log_prob(self, x):
z, sum_log_jacobians = self.forward(x)
log_prob_z = tdist.MultivariateNormal(self.mean, self.cov).log_prob(z)
log_prob_x = log_prob_z + sum_log_jacobians # [batch_size]
return log_prob_x