-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyNormal.py
More file actions
58 lines (45 loc) · 1.87 KB
/
MyNormal.py
File metadata and controls
58 lines (45 loc) · 1.87 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
"""Implementations of Normal distributions."""
import numpy as np
import torch
from torch import nn
from nflows.distributions.base import Distribution
from nflows.utils import torchutils
class MyDiagonalNormal(Distribution):
"""A diagonal multivariate Normal with trainable parameters."""
def __init__(self, shape, input_par, reparametrization):
"""Constructor.
Args:
shape: list, tuple or torch.Size, the shape of the input variables.
context_encoder: callable or None, encodes the context to the distribution parameters.
If None, defaults to the identity function.
"""
super().__init__()
self._shape = torch.Size(shape)
self.mean_ = nn.Parameter(torch.zeros(shape).reshape(1, -1))
self.log_std_ = nn.Parameter(torch.zeros(shape).reshape(1, -1))
self.register_buffer("_log_z",
torch.tensor(0.5 * np.prod(shape) * np.log(2 * np.pi),
dtype=torch.float64),
persistent=False)
def _log_prob(self, inputs, context):
if inputs.shape[1:] != self._shape:
raise ValueError(
"Expected input of shape {}, got {}".format(
self._shape, inputs.shape[1:]
)
)
# Compute parameters.
means = self.mean_
log_stds = self.log_std_
# Compute log prob.
norm_inputs = (inputs - means) * torch.exp(-log_stds)
log_prob = -0.5 * torchutils.sum_except_batch(
norm_inputs ** 2, num_batch_dims=1
)
log_prob -= torchutils.sum_except_batch(log_stds, num_batch_dims=1)
log_prob -= self._log_z
return log_prob
def _sample(self, num_samples, context):
raise NotImplementedError()
def _mean(self, context):
return self.mean