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

Document how to implement custom models #2306

Open
HaoLiu56 opened this issue Apr 23, 2024 · 6 comments
Open

Document how to implement custom models #2306

HaoLiu56 opened this issue Apr 23, 2024 · 6 comments

Comments

@HaoLiu56
Copy link

Issue description

Provide a short description.

I wonder if we could implement a custom model instead of GP class model.
Specifically, suppose we build a random forest model, and it also have mean and variance for data points. Is it possible to do Bayesian Optimization with this model under BoTorch framework?

Code example

For example:
model = CustomModel(train_X=init_x, train_Y=init_y )

System Info

Please provide information about your setup, including

  • BoTorch Version (0.10.0)
  • GPyTorch Version (1.11)
  • PyTorch Version (2.2.0.post100)
  • Computer OS: mac OS Sonoma
@Balandat
Copy link
Contributor

Yes, this is generally possible. BoTorch's Model class only requires you to define a posterior() method that returns a Posterior object, the only requirement of which is to implement an rsample() function for drawing posterior samples (the EnsemblePosterior class provides an implementation for that). Then the MC acquisition function machinery will work with such a setup (EnsemblePosterior also implements mean and variance properties, so some other analytic acquisition functions will also work).

That said, note that in order to use gradient-based optimization of the acquisition function (via the standard optimize_acqf() method) you will need to have the samples drawn from the posterior be differentiable w.r.t. to the input to the posterior() method (this is not the case for Random Forest models). In that case you'll have to perform the acquisition function optimization with gradient-free methods (e.g. CMA-ES or similar methods).

@HaoLiu56
Copy link
Author

Great! Could you include some tutorials notebook for this? I'm fairly new to BoTorch.

@esantorella esantorella changed the title Inplement custom model Document how to implement custom models May 25, 2024
@jakee417
Copy link
Contributor

@esantorella @Balandat here is an attempt at documenting a custom model that I'd like to submit a PR for: https://github.com/jakee417/botorch/blob/main/tutorials/custom_model.ipynb

One question I have is trying to get MCSampler to work with TorchPosterior. I am getting:

NotImplementedError: A registered `MCSampler` for posterior <botorch.posteriors.torch.TorchPosterior object at 0x1334cf410> is not found. You can implement and register one using `@GetSampler.register`.

But when I try:

from botorch.sampling import get_sampler
from botorch.sampling.base import MCSampler
from botorch.sampling.get_sampler import GetSampler


@GetSampler.register(TorchPosterior)
def _get_sampler_torch(
    posterior: TorchPosterior,
    sample_shape: torch.Size,
    *,
    seed: Optional[int] = None,
) -> MCSampler:
    return get_sampler(
        posterior=posterior,
        sample_shape=sample_shape,
        seed=seed,
    )

it doesn't seem to resolve the issue. Any tips here?

@saitcakmak
Copy link
Contributor

Hi @jakee417. The GetSampler case your register should return a sampler instance rather than calling back get_sampler. If _get_sampler_torch calls get_sampler, you end up in an infinite loop of get_sampler -> GetSampler -> _get_sampler_torch -> get_sampler -> ... calls when there is a TorchPosterior.

@jakee417
Copy link
Contributor

I tried following the example for TransformedPosterior, which I now see is a mistake. But, the infinite loop never even occurred due to the dispatching in _posterior_to_distribution_encoder (had it worked, I do see your point about the cyclic dependency). Instead, referencing the torch.distributions.Distribution seems to catch all of its subclasses:

from botorch.sampling.base import MCSampler
from botorch.sampling.get_sampler import GetSampler
from botorch.sampling.stochastic_samplers import StochasticSampler


@GetSampler.register(distributions.Distribution)
def _get_sampler_torch(
    posterior: TorchPosterior,
    sample_shape: torch.Size,
    *,
    seed: Optional[int] = None,
) -> MCSampler:
    return StochasticSampler(sample_shape=sample_shape, seed=seed)

In this case, the original error:

NotImplementedError: A registered `MCSampler` for posterior <botorch.posteriors.torch.TorchPosterior object at 0x1334cf410> is not found. You can implement and register one using `@GetSampler.register`.

Is a little misleading since you don't need to register botorch.posteriors.torch.TorchPosterior but instead the underlying distribution (again, due to the dispatching).

@saitcakmak
Copy link
Contributor

Ah yes, I forgot that we register them based on the distribution class for TorchPosterior. This is the right way to do it, but ideally we'd register them for the specific distribution class rather than the base class (in case there are custom behaviors for different distributions, like MVN). Left a comment on this on the PR as well. We can continue the discussion there.

@saitcakmak saitcakmak linked a pull request Aug 21, 2024 that will close this issue
facebook-github-bot pushed a commit that referenced this issue Aug 22, 2024
Summary:
<!--
Thank you for sending the PR! We appreciate you spending the time to make BoTorch better.

Help us understand your motivation by explaining why you decided to make this change.

You can learn more about contributing to BoTorch here: https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md
-->

## Motivation

Issue #2306

### Have you read the [Contributing Guidelines on pull requests](https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md#pull-requests)?

Yes. Added a tutorial which can be used for smoke tests.

Pull Request resolved: #2474

Test Plan:
Probabilistic linear regression, bayesian linear regression, and ensemble linear regression all yield optimization results close to (0, 0) which is groundtruth answer.

Random Forest doesn't seem to achieve groundtruth answer, likely due to its inability to incorporate gradient information into the optimization of the acquisition function.

## Related PRs

N/A

Reviewed By: esantorella

Differential Revision: D61612799

Pulled By: jakee417

fbshipit-source-id: 63d26c048dc4544cae37e89767e14caf732e7749
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants