2022-09-08 03:59:30 +00:00
|
|
|
import numpy as np
|
2022-09-11 20:58:14 +00:00
|
|
|
import torch
|
2022-09-08 03:59:30 +00:00
|
|
|
|
|
|
|
|
2022-09-17 21:02:27 +00:00
|
|
|
class DiagonalGaussianDistribution:
|
2022-09-08 03:59:30 +00:00
|
|
|
def __init__(self, parameters, deterministic=False):
|
|
|
|
self.parameters = parameters
|
|
|
|
self.mean, self.logvar = torch.chunk(parameters, 2, dim=1)
|
|
|
|
self.logvar = torch.clamp(self.logvar, -30.0, 20.0)
|
|
|
|
self.deterministic = deterministic
|
|
|
|
self.std = torch.exp(0.5 * self.logvar)
|
|
|
|
self.var = torch.exp(self.logvar)
|
|
|
|
if self.deterministic:
|
|
|
|
self.var = self.std = torch.zeros_like(self.mean).to(
|
|
|
|
device=self.parameters.device
|
|
|
|
)
|
|
|
|
|
|
|
|
def sample(self):
|
|
|
|
x = self.mean + self.std * torch.randn(self.mean.shape).to(
|
|
|
|
device=self.parameters.device
|
|
|
|
)
|
|
|
|
return x
|
|
|
|
|
|
|
|
def kl(self, other=None):
|
|
|
|
if self.deterministic:
|
|
|
|
return torch.Tensor([0.0])
|
2022-09-21 16:14:39 +00:00
|
|
|
|
|
|
|
if other is None:
|
|
|
|
return 0.5 * torch.sum(
|
|
|
|
torch.pow(self.mean, 2) + self.var - 1.0 - self.logvar,
|
|
|
|
dim=[1, 2, 3],
|
|
|
|
)
|
|
|
|
|
|
|
|
return 0.5 * torch.sum(
|
|
|
|
torch.pow(self.mean - other.mean, 2) / other.var
|
|
|
|
+ self.var / other.var
|
|
|
|
- 1.0
|
|
|
|
- self.logvar
|
|
|
|
+ other.logvar,
|
|
|
|
dim=[1, 2, 3],
|
|
|
|
)
|
|
|
|
|
|
|
|
def nll(self, sample, dims=None):
|
|
|
|
dims = dims if dims is None else [1, 2, 3]
|
2022-09-08 03:59:30 +00:00
|
|
|
if self.deterministic:
|
|
|
|
return torch.Tensor([0.0])
|
|
|
|
logtwopi = np.log(2.0 * np.pi)
|
|
|
|
return 0.5 * torch.sum(
|
|
|
|
logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var,
|
|
|
|
dim=dims,
|
|
|
|
)
|
|
|
|
|
|
|
|
def mode(self):
|
|
|
|
return self.mean
|