LaplaceBoTorchSurrogate
LaplaceBoTorchSurrogate(
net,
train_X,
train_Y,
train_Yvar=None,
*,
trainer=PosthocMarglik(),
input_transform=None,
outcome_transform=None,
)A Laplace-approximated neural network as a BoTorch model.
Build it from the data, as any BoTorch model, and use it wherever BoTorch takes a Model: analytic and Monte Carlo acquisition functions, optimize_acqf, and fantasy-based acquisition functions such as qKnowledgeGradient. The constructor trains the network and fits the Laplace approximation, so each round of Bayesian optimization builds a new model from all the data:
for _ in range(rounds):
model = LaplaceBoTorchSurrogate(
make_net(), train_X, train_Y, outcome_transform=Standardize(m=1)
)
acq = LogExpectedImprovement(model, best_f=train_Y.max())
x, _ = optimize_acqf(acq, bounds=bounds, q=1, num_restarts=10, raw_samples=512)
train_X, train_Y = torch.cat([train_X, x]), torch.cat([train_Y, objective(x)])The posterior is the linearized Laplace GP. For a batch of \(q\) points \(X\) and \(m\) outcomes, it is Gaussian with mean \(f(X; \mu)\) and covariance \(J \Sigma J^\top\), where \(J\) is the Jacobian of the \(q m\) outputs with respect to the weights. Batches are independent, so for marginals at many points pass them as X.unsqueeze(-2), a batch of one point each; a 2-dim X of \(N\) points gives one joint \(N m \times N m\) covariance.
As in BoTorch’s own models, the transforms learn their coefficients from the training data in the constructor and are then kept in eval mode. The network and the posterior work in the transformed spaces, and posterior returns outcomes in the original space. Training draws from torch’s global RNG, so torch.manual_seed makes it repeatable.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| net | nn.Module | A network with d inputs and m outputs. It is trained in place, so a network reused from the last round starts from its trained weights (a warm start); pass a freshly initialized one to start over. |
required |
| train_X | Tensor | The inputs, [n, d]. |
required |
| train_Y | Tensor | The outcomes, [n, m]. |
required |
| trainer | Trainer | Trains the network and fits the Laplace approximation. | PosthocMarglik() |
| train_Yvar | float | Tensor | None | The observation noise variance, as in SingleTaskGP: a float or a tensor [n, m] in the space of train_Y, kept fixed. It goes through the outcome transform with train_Y, and must then be one value. None leaves it to the trainer: PosthocMarglik and OnlineMarglik fix it at 1e-3 times the variance of the transformed outcomes, and with noise=None, tune it. |
None |
| input_transform | InputTransform | None | A BoTorch input transform, such as Normalize(d=d). |
None |
| outcome_transform | OutcomeTransform | None | A BoTorch outcome transform, such as Standardize(m=m). |
None |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | train_X is not [n, d], train_Y is not [n, m] with the same n, or train_Yvar is not positive and finite, or not one value after the outcome transform. |
|
| StructureError | The trainer returned something other than a weight posterior with an lk.Gaussian likelihood and a one-element sigma. |
Attributes
| Name | Description |
|---|---|
| batch_shape | The batch shape of the conditioning points, and empty without conditioning. |
| noise_variance | The noise variance of the transformed outcomes, learned or given, a 0-dim tensor. |
| num_outputs | The number of outcomes \(m\). |
Methods
| Name | Description |
|---|---|
| condition_on_observations | Condition the linearized GP on new observations, without training. |
| construct_inputs | The constructor’s arguments for a BoTorch dataset, as Model.construct_inputs. |
| posterior | The joint Gaussian over the outcomes at X. |
condition_on_observations
LaplaceBoTorchSurrogate.condition_on_observations(X, Y, noise=None, **kwargs)Condition the linearized GP on new observations, without training.
The result is a GP posterior given the new points: for a query \(x\), the mean moves by \(k(x, X) (K + N)^{-1} (Y - f(X; \mu))\) and the covariance shrinks by \(k(x, X) (K + N)^{-1} k(X, x)\), where \(k\) is the linearized kernel \(J(x) \Sigma J(x')^\top\), \(K = k(X, X)\) and \(N\) the noise. The network and the weight posterior stay as they are. This is what BoTorch’s fantasies use; to train on new data, build a new model.
Batch dims of X and Y broadcast, so a batch of fantasies gives a batched surrogate. Conditioning again adds the new points to the earlier ones.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| X | Tensor | The inputs, [*b, n, d], in the original space. |
required |
| Y | Tensor | The outcomes, [*b', n, m], in the original space, with *b' broadcasting to *b. |
required |
| noise | Tensor | None | The noise variances of Y, shaped like Y and in the transformed space. None uses noise_variance. |
None |
| **kwargs | Any | Ignored. | {} |
Returns
| Name | Type | Description |
|---|---|---|
| Self | A new model that shares this one’s posterior and training data. |
construct_inputs
LaplaceBoTorchSurrogate.construct_inputs(training_data, net=None, **kwargs)The constructor’s arguments for a BoTorch dataset, as Model.construct_inputs.
inputs = LaplaceBoTorchSurrogate.construct_inputs(dataset, net=net)
model = LaplaceBoTorchSurrogate(**inputs)Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| training_data | SupervisedDataset | A SupervisedDataset with X, Y and, optionally, Yvar. |
required |
| net | nn.Module | None | The network to train. | None |
| **kwargs | Any | Other constructor arguments, such as trainer or the transforms, passed on as they are. |
{} |
Returns
| Name | Type | Description |
|---|---|---|
| dict[str, Any] | train_X, train_Y, train_Yvar when the dataset has it, net and kwargs. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | net is missing. |
posterior
LaplaceBoTorchSurrogate.posterior(
X,
output_indices=None,
observation_noise=False,
posterior_transform=None,
**kwargs,
)The joint Gaussian over the outcomes at X.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| X | Tensor | The inputs, [*b, q, d], in the original space. Each batch is joint over its q points. A 2-dim X is one batch of q points. |
required |
| output_indices | list[int] | None | The outcomes to keep. None keeps all. |
None |
| observation_noise | bool | Tensor | True adds the noise variance (noise_variance) to the diagonal. A tensor [*, 1, m] or [*, q, m] adds those variances instead, given in the transformed space. False gives the latent outputs. |
False |
| posterior_transform | PosteriorTransform | None | Applied last, after the outcome transform. | None |
| **kwargs | Any | Ignored. | {} |
Returns
| Name | Type | Description |
|---|---|---|
| Posterior | A GPyTorchPosterior. Its distribution is a MultivariateNormal for one outcome and a MultitaskMultivariateNormal (point-major order) for several. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | X has fewer than 2 dims. |