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

Add epsilon after precision-factor-multiplication #22

Merged
merged 1 commit into from
Aug 24, 2022
Merged
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
8 changes: 6 additions & 2 deletions pyknos/mdn/mdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
raise NotImplementedError

# Constant for numerical stability.
self._epsilon = 1e-2
self._epsilon = 1e-4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a test in sbi to make sure this really fixes the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'll create an issue


# Initialize mixture coefficients and precision factors sensibly.
if custom_initialization:
Expand Down Expand Up @@ -116,7 +116,7 @@ def get_mixture_components(

# Elements of diagonal of precision factor must be positive
# (recall precision factor A such that SIGMA^-1 = A^T A).
diagonal = F.softplus(unconstrained_diagonal) + self._epsilon
diagonal = F.softplus(unconstrained_diagonal)

# Create empty precision factor matrix, and fill with appropriate quantities.
precision_factors = torch.zeros(
Expand All @@ -139,6 +139,10 @@ def get_mixture_components(
precisions = torch.matmul(
torch.transpose(precision_factors, 2, 3), precision_factors
)
# Add epsilon to diagnonal for numerical stability.
precisions[
..., torch.arange(self._features), torch.arange(self._features)
] += self._epsilon

# The sum of the log diagonal of A is used in the likelihood calculation.
sumlogdiag = torch.sum(torch.log(diagonal), dim=-1)
Expand Down