-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnp_utils.py
More file actions
57 lines (39 loc) · 1.59 KB
/
Copy pathdnp_utils.py
File metadata and controls
57 lines (39 loc) · 1.59 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
import torch
import math
import torch.nn.functional as F
import numpy as np
from torch.distributions import Bernoulli
from itertools import product
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
float_tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
def logitexp(logp):
# https: // github.com / pytorch / pytorch / issues / 4007
pos = torch.clamp(logp, min=-0.69314718056)
neg = torch.clamp(logp, max=-0.69314718056)
neg_val = neg - torch.log(1 - torch.exp(neg))
pos_val = -torch.log(torch.clamp(torch.expm1(-pos), min=1e-20))
return pos_val + neg_val
def one_hot(x, n_classes=10):
x_onehot = float_tensor(x.size(0), n_classes).zero_()
x_onehot.scatter_(1, x[:, None], 1)
return x_onehot
class Normal(object):
def __init__(self, means, logscales, **kwargs):
self.means = means
self.logscales = logscales
def log_prob(self, value):
log_prob = torch.pow(value - self.means, 2)
log_prob *= - (1 / (2. * self.logscales.mul(2.).exp()))
log_prob -= self.logscales + .5 * math.log(2. * math.pi)
return log_prob
def sample(self, **kwargs):
eps = torch.normal(float_tensor(self.means.size()).zero_(), float_tensor(self.means.size()).fill_(1))
return self.means + self.logscales.exp() * eps
def rsample(self, **kwargs):
return self.sample(**kwargs)
class Flatten(torch.nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
assert len(x.shape) > 1
return x.view(x.shape[0], -1)