-
Notifications
You must be signed in to change notification settings - Fork 79
Custom kernel priors #219
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
Custom kernel priors #219
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,5 @@ | ||
| """Available priors.""" | ||
|
|
||
| from baybe.kernels.priors.basic import GammaPrior | ||
|
|
||
| __all__ = ["GammaPrior"] |
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,35 @@ | ||
| """Base class for all priors.""" | ||
|
|
||
| from abc import ABC | ||
|
|
||
| from attrs import define | ||
|
|
||
| from baybe.serialization.core import ( | ||
| converter, | ||
| get_base_structure_hook, | ||
| unstructure_base, | ||
| ) | ||
| from baybe.serialization.mixin import SerialMixin | ||
| from baybe.utils.basic import filter_attributes | ||
|
|
||
|
|
||
| @define(frozen=True) | ||
| class Prior(ABC, SerialMixin): | ||
| """Abstract base class for all priors.""" | ||
|
|
||
| def to_gpytorch(self, *args, **kwargs): | ||
| """Create the gpytorch representation of the prior.""" | ||
| import gpytorch.priors | ||
|
|
||
| prior_cls = getattr(gpytorch.priors, self.__class__.__name__) | ||
| fields_dict = filter_attributes(object=self, callable_=prior_cls.__init__) | ||
|
|
||
| # Update kwargs to contain class-specific attributes | ||
|
AVHopp marked this conversation as resolved.
|
||
| kwargs.update(fields_dict) | ||
|
|
||
| return prior_cls(*args, **kwargs) | ||
|
|
||
|
|
||
| # Register de-/serialization hooks | ||
| converter.register_structure_hook(Prior, get_base_structure_hook(Prior)) | ||
| converter.register_unstructure_hook(Prior, unstructure_base) | ||
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,16 @@ | ||
| """Priors that can be used for kernels.""" | ||
| from attrs import define, field | ||
| from attrs.validators import gt | ||
|
|
||
| from baybe.kernels.priors.base import Prior | ||
|
|
||
|
|
||
| @define(frozen=True) | ||
| class GammaPrior(Prior): | ||
| """A Gamma prior parameterized by concentration and rate.""" | ||
|
|
||
| concentration: float = field(converter=float, validator=gt(0.0)) | ||
| """The concentration.""" | ||
|
|
||
| rate: float = field(converter=float, validator=gt(0.0)) | ||
| """The rate.""" |
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
|
AVHopp marked this conversation as resolved.
|
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,19 @@ | ||
| """Hypothesis strategies for priors.""" | ||
|
AVHopp marked this conversation as resolved.
|
||
|
|
||
| import hypothesis.strategies as st | ||
|
|
||
| from baybe.kernels.priors import GammaPrior | ||
|
|
||
| gamma_priors = st.builds( | ||
| GammaPrior, | ||
| st.floats(min_value=0, exclude_min=True), | ||
| st.floats(min_value=0, exclude_min=True), | ||
| ) | ||
| """A strategy that generates Gamma priors.""" | ||
|
|
||
| priors = st.one_of( | ||
| [ | ||
| gamma_priors, | ||
| ] | ||
| ) | ||
| """A strategy that generates priors.""" | ||
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 @@ | ||
| """Test serialization of priors.""" | ||
|
|
||
| from hypothesis import given | ||
|
|
||
| from baybe.kernels.priors.base import Prior | ||
| from tests.hypothesis_strategies.priors import priors | ||
|
|
||
|
|
||
| @given(priors) | ||
| def test_prior_kernel_roundtrip(prior: Prior): | ||
| string = prior.to_json() | ||
| prior2 = Prior.from_json(string) | ||
| assert prior == prior2, (prior, prior2) |
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.