-
Notifications
You must be signed in to change notification settings - Fork 70
Refactor GP Surrogates #235
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
19 commits
Select commit
Hold shift + click to select a range
280ae4f
Turn gaussian_process.py into subpackage
AdrianSosic 92f512e
Introduce KernelFactory protocol
AdrianSosic 5a311e9
Make KernelFactory also take training data as inputs
AdrianSosic 1758806
Add convenience conversion from kernels to factories
AdrianSosic 8915eb3
Extend is_abstract method to handle protocol classes
AdrianSosic 31672a5
Update surrogate fixture to use new attribute name
AdrianSosic c56f009
Move kernel factory code to separate module
AdrianSosic 494b974
Draft preset mechanism
AdrianSosic e355469
Create preset subpackage
AdrianSosic 3c2b0f3
Rework documentation of default preset
AdrianSosic 39ce333
Fix references
AdrianSosic c6e14d0
Add missing attribute docstring
AdrianSosic dd4b79e
Rename boolean mordred variable to uses_descriptors
AdrianSosic 186dbf6
Add a to_factory method to the Kernel class
AdrianSosic 31aa08f
Rename DEFAULT preset to BAYBE
AdrianSosic c24ddc7
Make docstring more specific
AdrianSosic b50e860
Use is_protocol from typing_extensions
AdrianSosic 71b70f6
Expose kernel_factory attribute as kernel_or_factory
AdrianSosic 95b0f50
Update CHANGELOG.md
AdrianSosic 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,7 @@ | ||
| """Gaussian process surrogates.""" | ||
|
|
||
| from baybe.surrogates.gaussian_process.core import GaussianProcessSurrogate | ||
AVHopp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| __all__ = [ | ||
| "GaussianProcessSurrogate", | ||
| ] | ||
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,55 @@ | ||
| """Kernel factories for the Gaussian process surrogate.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Protocol, Union | ||
|
|
||
| from attrs import define, field | ||
| from attrs.validators import instance_of | ||
|
|
||
| from baybe.kernels.base import Kernel | ||
| from baybe.searchspace import SearchSpace | ||
| from baybe.serialization.core import ( | ||
| converter, | ||
| get_base_structure_hook, | ||
| unstructure_base, | ||
| ) | ||
| from baybe.serialization.mixin import SerialMixin | ||
|
|
||
| if TYPE_CHECKING: | ||
| from torch import Tensor | ||
|
|
||
|
|
||
| class KernelFactory(Protocol): | ||
| """A protocol defining the interface expected for kernel factories.""" | ||
|
|
||
| def __call__( | ||
| self, searchspace: SearchSpace, train_x: Tensor, train_y: Tensor | ||
| ) -> Kernel: | ||
| """Create a :class:`baybe.kernels.base.Kernel` for the given DOE context.""" | ||
| ... | ||
|
|
||
|
|
||
| # Register de-/serialization hooks | ||
| converter.register_structure_hook(KernelFactory, get_base_structure_hook(KernelFactory)) | ||
| converter.register_unstructure_hook(KernelFactory, unstructure_base) | ||
|
|
||
|
|
||
| @define(frozen=True) | ||
| class PlainKernelFactory(KernelFactory, SerialMixin): | ||
| """A trivial factory that returns a fixed pre-defined kernel upon request.""" | ||
|
|
||
| kernel: Kernel = field(validator=instance_of(Kernel)) | ||
| """The fixed kernel to be returned by the factory.""" | ||
|
|
||
| def __call__( # noqa: D102 | ||
| self, searchspace: SearchSpace, train_x: Tensor, train_y: Tensor | ||
| ) -> Kernel: | ||
| # See base class. | ||
|
|
||
| return self.kernel | ||
|
|
||
|
|
||
| def to_kernel_factory(x: Union[Kernel, KernelFactory], /) -> KernelFactory: | ||
AdrianSosic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Wrap a kernel into a plain kernel factory (with factory passthrough).""" | ||
| return x.to_factory() if isinstance(x, Kernel) else x | ||
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,11 @@ | ||
| """Gaussian process surrogate presets.""" | ||
|
|
||
| from baybe.surrogates.gaussian_process.presets.core import ( | ||
| GaussianProcessPreset, | ||
| make_gp_from_preset, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "make_gp_from_preset", | ||
| "GaussianProcessPreset", | ||
Scienfitz marked this conversation as resolved.
Show resolved
Hide 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,26 @@ | ||
| """Preset configurations for Gaussian process surrogates.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from enum import Enum | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from baybe.surrogates.gaussian_process.core import GaussianProcessSurrogate | ||
|
|
||
|
|
||
| class GaussianProcessPreset(Enum): | ||
| """Available Gaussian process surrogate presets.""" | ||
|
|
||
| BAYBE = "BAYBE" | ||
| """Recreates the default settings of the Gaussian process surrogate class.""" | ||
|
|
||
|
|
||
| def make_gp_from_preset(preset: GaussianProcessPreset) -> GaussianProcessSurrogate: | ||
| """Create a :class:`GaussianProcessSurrogate` from a :class:`GaussianProcessPreset.""" # noqa: E501 | ||
| if preset is GaussianProcessPreset.BAYBE: | ||
| return GaussianProcessSurrogate() | ||
|
|
||
| raise ValueError( | ||
| f"Unknown '{GaussianProcessPreset.__name__}' with name '{preset.name}'." | ||
| ) |
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,110 @@ | ||
| """Default preset for Gaussian process surrogates.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from attrs import define | ||
|
|
||
| from baybe.kernels.basic import MaternKernel, ScaleKernel | ||
| from baybe.priors.basic import GammaPrior | ||
| from baybe.surrogates.gaussian_process.kernel_factory import KernelFactory | ||
|
|
||
| if TYPE_CHECKING: | ||
| from torch import Tensor | ||
|
|
||
| from baybe.kernels.base import Kernel | ||
| from baybe.searchspace.core import SearchSpace | ||
AVHopp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @define | ||
| class DefaultKernelFactory(KernelFactory): | ||
AdrianSosic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """A factory providing the default kernel for Gaussian process surrogates. | ||
|
|
||
| The logic is adapted from EDBO (Experimental Design via Bayesian Optimization). | ||
|
|
||
| References: | ||
| * https://github.com/b-shields/edbo | ||
| * https://doi.org/10.1038/s41586-021-03213-y | ||
| """ | ||
|
|
||
| def __call__( # noqa: D102 | ||
| self, searchspace: SearchSpace, train_x: Tensor, train_y: Tensor | ||
| ) -> Kernel: | ||
| # See base class. | ||
|
|
||
| mordred = (searchspace.contains_mordred or searchspace.contains_rdkit) and ( | ||
AdrianSosic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| train_x.shape[-1] >= 50 | ||
| ) | ||
|
|
||
| # low D priors | ||
| if train_x.shape[-1] < 10: # <-- different condition compared to EDBO | ||
| lengthscale_prior = GammaPrior(1.2, 1.1) | ||
| lengthscale_initial_value = 0.2 | ||
| outputscale_prior = GammaPrior(5.0, 0.5) | ||
| outputscale_initial_value = 8.0 | ||
|
|
||
| # DFT optimized priors | ||
| elif mordred and train_x.shape[-1] < 100: | ||
| lengthscale_prior = GammaPrior(2.0, 0.2) | ||
| lengthscale_initial_value = 5.0 | ||
| outputscale_prior = GammaPrior(5.0, 0.5) | ||
| outputscale_initial_value = 8.0 | ||
|
|
||
| # Mordred optimized priors | ||
| elif mordred: | ||
| lengthscale_prior = GammaPrior(2.0, 0.1) | ||
| lengthscale_initial_value = 10.0 | ||
| outputscale_prior = GammaPrior(2.0, 0.1) | ||
| outputscale_initial_value = 10.0 | ||
|
|
||
| # OHE optimized priors | ||
| else: | ||
| lengthscale_prior = GammaPrior(3.0, 1.0) | ||
| lengthscale_initial_value = 2.0 | ||
| outputscale_prior = GammaPrior(5.0, 0.2) | ||
| outputscale_initial_value = 20.0 | ||
|
|
||
| return ScaleKernel( | ||
| MaternKernel( | ||
| nu=2.5, | ||
| lengthscale_prior=lengthscale_prior, | ||
| lengthscale_initial_value=lengthscale_initial_value, | ||
| ), | ||
| outputscale_prior=outputscale_prior, | ||
| outputscale_initial_value=outputscale_initial_value, | ||
| ) | ||
|
|
||
|
|
||
| def _default_noise_factory( | ||
AdrianSosic marked this conversation as resolved.
Show resolved
Hide resolved
Scienfitz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| searchspace: SearchSpace, train_x: Tensor, train_y: Tensor | ||
| ) -> tuple[GammaPrior, float]: | ||
| """Create the default noise settings for the Gaussian process surrogate. | ||
|
|
||
| The logic is adapted from EDBO (Experimental Design via Bayesian Optimization). | ||
|
|
||
| References: | ||
| * https://github.com/b-shields/edbo | ||
| * https://doi.org/10.1038/s41586-021-03213-y | ||
| """ | ||
| # TODO: Replace this function with a proper likelihood factory | ||
|
|
||
| uses_descriptors = ( | ||
| searchspace.contains_mordred or searchspace.contains_rdkit | ||
| ) and (train_x.shape[-1] >= 50) | ||
|
|
||
| # low D priors | ||
| if train_x.shape[-1] < 10: # <-- different condition compared to EDBO | ||
| return [GammaPrior(1.05, 0.5), 0.1] | ||
|
|
||
| # DFT optimized priors | ||
| elif uses_descriptors and train_x.shape[-1] < 100: | ||
| return [GammaPrior(1.5, 0.1), 5.0] | ||
|
|
||
| # Mordred optimized priors | ||
| elif uses_descriptors: | ||
| return [GammaPrior(1.5, 0.1), 5.0] | ||
|
|
||
| # OHE optimized priors | ||
| else: | ||
| return [GammaPrior(1.5, 0.1), 5.0] | ||
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.