Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20221224] replace with pytorch cholesky #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,8 @@
from torch.autograd import Variable
import itertools
from utils import *
import math

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):
Expand Down Expand Up @@ -130,9 +118,8 @@ def compute_energy(self, z, phi=None, mu=None, cov=None, size_average=True):
# 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))

det_cov.append(torch.linalg.cholesky(cov_k * 2 * math.pi).diag().prod().unsqueeze(0))
cov_diag = cov_diag + torch.sum(1 / cov_k.diag())

# K x D x D
Expand Down