|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +r"""Abstract base module for all botorch acquisition functions.""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from abc import ABC, abstractmethod |
| 12 | + |
| 13 | +import torch |
| 14 | + |
| 15 | +from botorch.acquisition import AcquisitionFunction |
| 16 | +from botorch.models.model import Model |
| 17 | +from botorch.utils.transforms import t_batch_mode_transform |
| 18 | +from torch import Tensor |
| 19 | + |
| 20 | + |
| 21 | +class DiscretizedAcquistionFunction(AcquisitionFunction, ABC): |
| 22 | + r"""DiscretizedAcquistionFunction is an abstract base class for acquisition |
| 23 | + functions that are defined on discrete distributions. It wraps a model and |
| 24 | + implements a forward method that computes the acquisition function value at |
| 25 | + a given set of points. |
| 26 | + This class can be subclassed to define acquisiton functions for Riemann- |
| 27 | + distributed posteriors. |
| 28 | + The acquisition function must have the form $$acq(x) = \int p(y|x) ag(x)$$, |
| 29 | + where $$ag$$ is defined differently for each acquisition function. |
| 30 | + The ag_integrate method, which computes the integral of ag between two points, must be |
| 31 | + implemented by subclasses to define the specific acquisition functions. |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, model: Model) -> None: |
| 35 | + super().__init__(model=model) |
| 36 | + |
| 37 | + @t_batch_mode_transform(expected_q=1) |
| 38 | + def forward(self, X: Tensor) -> Tensor: |
| 39 | + r"""Evaluate the acquisition function on the candidate set X. |
| 40 | +
|
| 41 | + Args: |
| 42 | + X: A `(b) x q x d`-dim Tensor of `(b)` t-batches with `q` `d`-dim |
| 43 | + design points each. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + A `(b)`-dim Tensor of the acquisition function at the given |
| 47 | + design points `X`. |
| 48 | + """ |
| 49 | + self.to(device=X.device) |
| 50 | + |
| 51 | + discrete_posterior = self.model.posterior(X) |
| 52 | + result = discrete_posterior.integrate(self.ag_integrate) |
| 53 | + # remove q dimension |
| 54 | + return result.squeeze(-1) |
| 55 | + |
| 56 | + @abstractmethod |
| 57 | + def ag_integrate(self, lower_bound: Tensor, upper_bound: Tensor) -> Tensor: |
| 58 | + r""" |
| 59 | + This function calculates the integral that computes the acquisition function |
| 60 | + without the posterior factor from lower_bound to upper_bound. |
| 61 | + That is, our acquisition function is assumed to have the form |
| 62 | + \int ag(x) * p(x) dx, |
| 63 | + and this function calculates \int_{lower_bound}^{upper_bound} ag(x) dx. |
| 64 | + The `integrate` method of the posterior (`BoundedRiemannPosterior`) |
| 65 | + then computes the final acquisition value. |
| 66 | +
|
| 67 | + Args: |
| 68 | + lower_bound: lower bound of integral |
| 69 | + upper_bound: upper bound of integral |
| 70 | +
|
| 71 | + Returns: |
| 72 | + A `(b)`-dim Tensor of acquisition function derivatives at the given |
| 73 | + design points `X`. |
| 74 | + """ |
| 75 | + pass # pragma: no cover |
| 76 | + |
| 77 | + r"""DiscretizedExpectedImprovement is an acquisition function that computes |
| 78 | + the expected improvement over the current best observed value for a Riemann |
| 79 | + distribution.""" |
| 80 | + |
| 81 | + |
| 82 | +class DiscretizedExpectedImprovement(DiscretizedAcquistionFunction): |
| 83 | + r"""DiscretizedExpectedImprovement is an acquisition function that |
| 84 | + computes the expected improvement over the current best observed value |
| 85 | + for a Riemann distribution. |
| 86 | + """ |
| 87 | + |
| 88 | + def __init__(self, model: Model, best_f: Tensor) -> None: |
| 89 | + super().__init__(model) |
| 90 | + self.register_buffer("best_f", torch.as_tensor(best_f)) |
| 91 | + |
| 92 | + def ag_integrate(self, lower_bound: Tensor, upper_bound: Tensor) -> Tensor: |
| 93 | + r""" |
| 94 | + As Expected improvement has ag(y) = (y - best_f).clamp(min=0), and |
| 95 | + is defined as \int ag(y) * p(y) dy, we can calculate the integral |
| 96 | + of ag(y) like so: |
| 97 | + We just calculate ag(y) at beginning and end, and since the function has |
| 98 | + a gradient of 1 or 0, we can just take the average of the two. |
| 99 | +
|
| 100 | + Args: |
| 101 | + lower_bound: lower bound of integral |
| 102 | + upper_bound: upper bound of integral |
| 103 | +
|
| 104 | + Returns: |
| 105 | + A `(b)`-dim Tensor of acquisition function derivatives at the given |
| 106 | + design points `X`. |
| 107 | + """ |
| 108 | + max_lower_bound_and_f = torch.max(self.best_f, lower_bound) |
| 109 | + bucket_average = (upper_bound + max_lower_bound_and_f) / 2 |
| 110 | + improvement = bucket_average - self.best_f |
| 111 | + |
| 112 | + return improvement.clamp_min(0) |
| 113 | + |
| 114 | + |
| 115 | +class DiscretizedProbabilityOfImprovement(DiscretizedAcquistionFunction): |
| 116 | + r"""DiscretizedProbabilityOfImprovement is an acquisition function that |
| 117 | + computes the probability of improvement over the current best observed value |
| 118 | + for a Riemann distribution. |
| 119 | + """ |
| 120 | + |
| 121 | + def __init__(self, model: Model, best_f: Tensor) -> None: |
| 122 | + super().__init__(model) |
| 123 | + self.register_buffer("best_f", torch.as_tensor(best_f)) |
| 124 | + |
| 125 | + def ag_integrate(self, lower_bound: Tensor, upper_bound: Tensor) -> Tensor: |
| 126 | + r""" |
| 127 | + PI is defined as \int ag(y) * p(y) dy, where ag(y) = I(y - best_f) |
| 128 | + and I being the indicator function. |
| 129 | +
|
| 130 | + So all we need to do is calculate the portion between the bounds |
| 131 | + that is larger than best_f. |
| 132 | + We do this by comparing how much higher the upper bound is than best_f, |
| 133 | + compared to the size of the bucket. |
| 134 | + Then we clamp at one if best_f is below lower_bound and at zero if |
| 135 | + best_f is above upper_bound. |
| 136 | +
|
| 137 | + Args: |
| 138 | + lower_bound: lower bound of integral |
| 139 | + upper_bound: upper bound of integral |
| 140 | +
|
| 141 | + Returns: |
| 142 | + A `(b)`-dim Tensor of acquisition function derivatives at the given |
| 143 | + design points `X`. |
| 144 | + """ |
| 145 | + proportion = (upper_bound - self.best_f) / (upper_bound - lower_bound) |
| 146 | + return proportion.clamp(0, 1) |
0 commit comments