-
Notifications
You must be signed in to change notification settings - Fork 78
Stabilizing multivariate normal approximation #380
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
34c7f2a
Better parameterization of covariance matrices
han-ol 84ed002
Fix format string
han-ol fbc01f5
Test for invertibility of positive definite link output
han-ol eebf950
Allow estimation of univariate MVN
han-ol 42c6806
Remove commented lines
han-ol d57970a
Minor changes to comments and docstring for fill_triangular_matrix
han-ol ddfdbdc
Test coverage for unconditional MVNScore.sample
han-ol 2b38c21
Remove instability warning MultivariateNormalScore
han-ol 1405ee5
Remove commented numpy import
han-ol f1e1ba1
Fix dtype of dummy conditions if inference variables are available
han-ol 9d87656
Tuple conversion in case batch_shape is a list
han-ol 4bbbffa
Conversion to numpy before calling numpy operations
han-ol fe201aa
More detailed docs and renamed the transformation warning attribute
han-ol 02ea22c
Doc string detail
han-ol 9b46601
Remove untested comment for PointInferenceNetwork.sample()
han-ol 5cb8995
Relax type hints for ContinuousApproximator.log_prob
han-ol 303127d
Support log-prob in PointApproximator
han-ol 93e8833
Remove comment stating log prob was untested
han-ol 7bfacff
Fix typo
han-ol d87b0b9
Transformation warning using a class variable; docstring links
han-ol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import keras | ||
|
|
||
| from keras.saving import register_keras_serializable as serializable | ||
|
|
||
| from bayesflow.types import Tensor | ||
| from bayesflow.utils import keras_kwargs, fill_triangular_matrix | ||
|
|
||
|
|
||
| @serializable(package="bayesflow.links") | ||
| class PositiveDefinite(keras.Layer): | ||
| """Activation function to link from flat elements of a lower triangular matrix to a positive definite matrix.""" | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(**keras_kwargs(kwargs)) | ||
| self.built = True | ||
|
|
||
| def call(self, inputs: Tensor) -> Tensor: | ||
| # Build cholesky factor from inputs | ||
| L = fill_triangular_matrix(inputs, positive_diag=True) | ||
|
|
||
| # calculate positive definite matrix from cholesky factors | ||
| psd = keras.ops.matmul( | ||
| L, | ||
| keras.ops.moveaxis(L, -2, -1), # L transposed | ||
| ) | ||
| return psd | ||
|
|
||
| def compute_output_shape(self, input_shape): | ||
| m = input_shape[-1] | ||
| n = int((0.25 + 2.0 * m) ** 0.5 - 0.5) | ||
| return input_shape[:-1] + (n, n) | ||
|
|
||
| def compute_input_shape(self, output_shape): | ||
| """ | ||
| Returns the shape of parameterization of a cholesky factor triangular matrix. | ||
|
|
||
| There are m nonzero elements of a lower triangular nxn matrix with m = n * (n + 1) / 2. | ||
|
|
||
| Example | ||
| ------- | ||
| >>> PositiveDefinite().compute_output_shape((None, 3, 3)) | ||
| 6 | ||
| """ | ||
| n = output_shape[-1] | ||
| m = int(n * (n + 1) / 2) | ||
| return output_shape[:-2] + (m,) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add this function to the ContinuousApproximator, if it is identical between it and the Point Approximator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! This and similar refactoring of the ContinuousApproximator is a good idea (but I would keep them out of this PR).
There is also the option of moving the conversion to tensor into the adapter. Possibly with an optional bool flag convert_to_tensor that is by default False.