-
Notifications
You must be signed in to change notification settings - Fork 61
Introducing quad acquisition functions
#749
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
mmahsereci
merged 15 commits into
probabilistic-numerics:main
from
mmahsereci:mm-quad-acquisition
Dec 14, 2022
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
06d19da
acquisition functions for quad
mmahsereci fef2dff
adding tests for new policy
mmahsereci 1615657
tests for acqustion function
mmahsereci d2e1b68
implement uncertainty sampling
mmahsereci a43aa67
formatting
mmahsereci 3be7a01
fix pylint
mmahsereci dd49ce8
fix docstrings
mmahsereci 823c4cb
formatting
mmahsereci ff8d7fc
pylint
mmahsereci 249bfd0
Merge branch 'main' into mm-quad-acquisition
mmahsereci 84cacb9
pylint
mmahsereci 2578fb0
Merge branch 'main' into mm-quad-acquisition
mmahsereci b4a6410
small fix
mmahsereci 7f5f832
adressing PR comments
mmahsereci a389bf3
minor change
mmahsereci 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Acquisition Functions | ||
| --------------------- | ||
| .. automodapi:: probnum.quad.solvers.acquisition_functions | ||
| :no-heading: | ||
| :headings: "*" |
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
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
13 changes: 13 additions & 0 deletions
13
src/probnum/quad/solvers/acquisition_functions/__init__.py
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,13 @@ | ||
| """Acquisition functions for Bayesian quadrature.""" | ||
|
|
||
| from ._acquisition_function import AcquisitionFunction | ||
| from ._predictive_variance import WeightedPredictiveVariance | ||
|
|
||
| # Public classes and functions. Order is reflected in documentation. | ||
| __all__ = [ | ||
| "AcquisitionFunction", | ||
| "WeightedPredictiveVariance", | ||
| ] | ||
|
|
||
| # Set correct module paths. Corrects links and module paths in documentation. | ||
| WeightedPredictiveVariance.__module__ = "probnum.quad.solvers.acquisition_functions" |
43 changes: 43 additions & 0 deletions
43
src/probnum/quad/solvers/acquisition_functions/_acquisition_function.py
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,43 @@ | ||
| """Abstract base class for BQ acquisition functions.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import abc | ||
| from typing import Optional, Tuple | ||
|
|
||
| import numpy as np | ||
|
|
||
| from probnum.quad.solvers._bq_state import BQState | ||
|
|
||
|
|
||
| class AcquisitionFunction(abc.ABC): | ||
| """An abstract class for an acquisition function for Bayesian quadrature.""" | ||
|
|
||
| @property | ||
| @abc.abstractmethod | ||
| def has_gradients(self) -> bool: | ||
| """Whether the acquisition function exposes gradients.""" | ||
| raise NotImplementedError | ||
|
|
||
| @abc.abstractmethod | ||
| def __call__( | ||
| self, x: np.ndarray, bq_state: BQState | ||
| ) -> Tuple[np.ndarray, Optional[np.ndarray]]: | ||
| """Evaluates the acquisition function and optionally its gradients. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x | ||
| *shape=(batch_size, input_dim)* -- The nodes where the acquisition function | ||
| is being evaluated. | ||
| bq_state | ||
| State of the BQ belief. | ||
|
|
||
| Returns | ||
| ------- | ||
| acquisition_values : | ||
| *shape=(batch_size, )* -- The acquisition values at nodes ``x``. | ||
| acquisition_gradients : | ||
| *shape=(batch_size, input_dim)* -- The corresponding gradients (optional). | ||
| """ | ||
| raise NotImplementedError |
48 changes: 48 additions & 0 deletions
48
src/probnum/quad/solvers/acquisition_functions/_predictive_variance.py
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,48 @@ | ||
| """Uncertainty sampling for Bayesian Monte Carlo.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, Tuple | ||
|
|
||
| import numpy as np | ||
|
|
||
| from probnum.quad.solvers._bq_state import BQState | ||
| from probnum.quad.solvers.belief_updates import BQStandardBeliefUpdate | ||
|
|
||
| from ._acquisition_function import AcquisitionFunction | ||
|
|
||
| # pylint: disable=too-few-public-methods, fixme | ||
|
|
||
|
|
||
| class WeightedPredictiveVariance(AcquisitionFunction): | ||
| r"""The predictive variance acquisition function that yields uncertainty sampling. | ||
|
|
||
| The acquisition function is | ||
|
|
||
| .. math:: | ||
| a(x) = \operatorname{Var}(f(x)) p(x)^2 | ||
|
|
||
| where :math:`\operatorname{Var}(f(x))` is the predictive variance of the model and | ||
| :math:`p(x)` is the density of the integration measure :math:`\mu`. | ||
|
|
||
| """ | ||
|
|
||
| @property | ||
| def has_gradients(self) -> bool: | ||
| # Todo (#581): this needs to return True, once gradients are available | ||
| return False | ||
|
|
||
| def __call__( | ||
| self, | ||
| x: np.ndarray, | ||
| bq_state: BQState, | ||
| ) -> Tuple[np.ndarray, Optional[np.ndarray]]: | ||
| predictive_variance = bq_state.kernel(x, x) | ||
| if bq_state.fun_evals.shape != (0,): | ||
| kXx = bq_state.kernel.matrix(bq_state.nodes, x) | ||
| bq_weights = BQStandardBeliefUpdate.gram_cho_solve( | ||
| bq_state.gram_cho_factor, kXx | ||
| ) | ||
| predictive_variance -= np.sum(bq_weights * kXx, axis=0) | ||
| values = bq_state.scale_sq * predictive_variance * bq_state.measure(x) ** 2 | ||
| return values, None | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.