forked from danieltan07/dagmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
172 lines (125 loc) · 5.39 KB
/
model.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torchvision
from torch.autograd import Variable
import itertools
from utils import *
class Cholesky(torch.autograd.Function):
def forward(ctx, a):
l = torch.potrf(a, False)
ctx.save_for_backward(l)
return l
def backward(ctx, grad_output):
l, = ctx.saved_variables
linv = l.inverse()
inner = torch.tril(torch.mm(l.t(), grad_output)) * torch.tril(
1.0 - Variable(l.data.new(l.size(1)).fill_(0.5).diag()))
s = torch.mm(linv.t(), torch.mm(inner, linv))
return s
class DaGMM(nn.Module):
"""Residual Block."""
def __init__(self, n_gmm = 2, latent_dim=3):
super(DaGMM, self).__init__()
layers = []
layers += [nn.Linear(118,60)]
layers += [nn.Tanh()]
layers += [nn.Linear(60,30)]
layers += [nn.Tanh()]
layers += [nn.Linear(30,10)]
layers += [nn.Tanh()]
layers += [nn.Linear(10,1)]
self.encoder = nn.Sequential(*layers)
layers = []
layers += [nn.Linear(1,10)]
layers += [nn.Tanh()]
layers += [nn.Linear(10,30)]
layers += [nn.Tanh()]
layers += [nn.Linear(30,60)]
layers += [nn.Tanh()]
layers += [nn.Linear(60,118)]
self.decoder = nn.Sequential(*layers)
layers = []
layers += [nn.Linear(latent_dim,10)]
layers += [nn.Tanh()]
layers += [nn.Dropout(p=0.5)]
layers += [nn.Linear(10,n_gmm)]
layers += [nn.Softmax(dim=1)]
self.estimation = nn.Sequential(*layers)
self.register_buffer("phi", torch.zeros(n_gmm))
self.register_buffer("mu", torch.zeros(n_gmm,latent_dim))
self.register_buffer("cov", torch.zeros(n_gmm,latent_dim,latent_dim))
def relative_euclidean_distance(self, a, b):
return (a-b).norm(2, dim=1) / a.norm(2, dim=1)
def forward(self, x):
enc = self.encoder(x)
dec = self.decoder(enc)
rec_cosine = F.cosine_similarity(x, dec, dim=1)
rec_euclidean = self.relative_euclidean_distance(x, dec)
z = torch.cat([enc, rec_euclidean.unsqueeze(-1), rec_cosine.unsqueeze(-1)], dim=1)
gamma = self.estimation(z)
return enc, dec, z, gamma
def compute_gmm_params(self, z, gamma):
N = gamma.size(0)
# K
sum_gamma = torch.sum(gamma, dim=0)
# K
phi = (sum_gamma / N)
self.phi = phi.data
# K x D
mu = torch.sum(gamma.unsqueeze(-1) * z.unsqueeze(1), dim=0) / sum_gamma.unsqueeze(-1)
self.mu = mu.data
# z = N x D
# mu = K x D
# gamma N x K
# z_mu = N x K x D
z_mu = (z.unsqueeze(1)- mu.unsqueeze(0))
# z_mu_outer = N x K x D x D
z_mu_outer = z_mu.unsqueeze(-1) * z_mu.unsqueeze(-2)
# K x D x D
cov = torch.sum(gamma.unsqueeze(-1).unsqueeze(-1) * z_mu_outer, dim = 0) / sum_gamma.unsqueeze(-1).unsqueeze(-1)
self.cov = cov.data
return phi, mu, cov
def compute_energy(self, z, phi=None, mu=None, cov=None, size_average=True):
if phi is None:
phi = to_var(self.phi)
if mu is None:
mu = to_var(self.mu)
if cov is None:
cov = to_var(self.cov)
k, D, _ = cov.size()
z_mu = (z.unsqueeze(1)- mu.unsqueeze(0))
cov_inverse = []
det_cov = []
cov_diag = 0
eps = 1e-12
for i in range(k):
# K x D x D
cov_k = cov[i] + to_var(torch.eye(D)*eps)
cov_inverse.append(torch.inverse(cov_k).unsqueeze(0))
#det_cov.append(np.linalg.det(cov_k.data.cpu().numpy()* (2*np.pi)))
det_cov.append((Cholesky.apply(cov_k.cpu() * (2*np.pi)).diag().prod()).unsqueeze(0))
cov_diag = cov_diag + torch.sum(1 / cov_k.diag())
# K x D x D
cov_inverse = torch.cat(cov_inverse, dim=0)
# K
det_cov = torch.cat(det_cov).cuda()
#det_cov = to_var(torch.from_numpy(np.float32(np.array(det_cov))))
# N x K
exp_term_tmp = -0.5 * torch.sum(torch.sum(z_mu.unsqueeze(-1) * cov_inverse.unsqueeze(0), dim=-2) * z_mu, dim=-1)
# for stability (logsumexp)
max_val = torch.max((exp_term_tmp).clamp(min=0), dim=1, keepdim=True)[0]
exp_term = torch.exp(exp_term_tmp - max_val)
# sample_energy = -max_val.squeeze() - torch.log(torch.sum(phi.unsqueeze(0) * exp_term / (det_cov).unsqueeze(0), dim = 1) + eps)
sample_energy = -max_val.squeeze() - torch.log(torch.sum(phi.unsqueeze(0) * exp_term / (torch.sqrt(det_cov)).unsqueeze(0), dim = 1) + eps)
# sample_energy = -max_val.squeeze() - torch.log(torch.sum(phi.unsqueeze(0) * exp_term / (torch.sqrt((2*np.pi)**D * det_cov)).unsqueeze(0), dim = 1) + eps)
if size_average:
sample_energy = torch.mean(sample_energy)
return sample_energy, cov_diag
def loss_function(self, x, x_hat, z, gamma, lambda_energy, lambda_cov_diag):
recon_error = torch.mean((x - x_hat) ** 2)
phi, mu, cov = self.compute_gmm_params(z, gamma)
sample_energy, cov_diag = self.compute_energy(z, phi, mu, cov)
loss = recon_error + lambda_energy * sample_energy + lambda_cov_diag * cov_diag
return loss, sample_energy, recon_error, cov_diag