Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

laplacekit-bayesopt

A laplacekit plugin that turns a network into a BoTorch surrogate, Bayesian optimization with Laplace-approximated neural networks.

Install

pip install laplacekit-bayesopt

Usage

LaplaceBNNSurrogate is a BoTorch Model. Use it in a BoTorch loop in place of SingleTaskGP:

import torch
from botorch.acquisition import LogExpectedImprovement
from botorch.models.transforms.input import Normalize
from botorch.models.transforms.outcome import Standardize
from botorch.optim import optimize_acqf
from torch import nn

from laplacekit_bayesopt import LaplaceBNNSurrogate


def make_net():
    return nn.Sequential(
        nn.Linear(2, 50), nn.ReLU(), nn.Linear(50, 50), nn.ReLU(), nn.Linear(50, 1)
    )


for _ in range(rounds):
    model = LaplaceBNNSurrogate(
        make_net(),
        train_X,
        train_Y,
        input_transform=Normalize(d=2),
        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=20)
    train_X, train_Y = torch.cat([train_X, x]), torch.cat([train_Y, objective(x)])

As with SingleTaskGP, each round builds a new model from all the data, and the data can go in by keyword (train_X=, train_Y=) or through LaplaceBNNSurrogate.construct_inputs(dataset, net=make_net()), as Ax builds models. The constructor trains the network it is given in place. A fresh network starts over; the network of the last round starts from its trained weights, a warm start.

The posterior is the linearized Laplace approximation, which is a Gaussian process:

  • model.posterior(X) is joint over the q points of each batch, so Monte Carlo acquisition functions (qLogNoisyExpectedImprovement, qLogExpectedHypervolumeImprovement, ...) see the correlations between points and outcomes.
  • model.condition_on_observations(X, Y) conditions the GP on new points without training. BoTorch's fantasies need this, so qKnowledgeGradient works.
  • BoTorch's PathwiseThompsonSampling works on it: importing the package registers the model with draw_matheron_paths. Each path is one function f(x; μ) + J(x) ε drawn from the posterior, smooth in x, so it works with optimize_acqf and optimize_acqf_discrete alike.

Training

A Trainer trains the network and fits the Laplace approximation:

Trainer What it does
PosthocMarglik() (default) Adam on the mean squared error, then tunes the prior precision and the noise on the marginal likelihood.
OnlineMarglik() Tunes the prior precision and the noise during training (Immer et al. 2021), through lk.marglik.train.

They are laplace-bayesopt's marglik_mode="posthoc" and "online". Both use KFAC over all weights by default. PosthocMarglik(subset=lk.subsets.last_layer) gives a last-layer Laplace BNN, and hessian_approx= takes another curvature:

import laplacekit as lk

from laplacekit_bayesopt import PosthocMarglik

trainer = PosthocMarglik(epochs=500, subset=lk.subsets.last_layer, hessian_approx=lk.DenseGGN())
model = LaplaceBNNSurrogate(make_net(), train_X, train_Y, trainer=trainer)

Training and path draws use torch's global RNG, so torch.manual_seed makes a run repeatable.

train_Yvar= fixes the observation noise variance instead of learning it, as in SingleTaskGP. It is in the space of train_Y and goes through the outcome transform; one value for all points and outcomes is supported. model.noise_variance is the noise, learned or given, and model.laplace_posterior the Laplace posterior. Any callable with the signature of Trainer.__call__ works as a trainer.

Examples

examples/ holds runnable scripts:

  • singletask.py: Branin with log expected improvement.
  • thompson.py: Branin over 8,192 Sobol candidates with Thompson sampling.
  • multitask.py: a composite objective over three outcomes, with Monte Carlo log expected improvement.
  • multiobjective.py: the Pareto front of Branin-Currin with log expected hypervolume improvement.

Development

make sync    # uv sync
make check   # ruff, ty, pytest
make examples

Citation

@inproceedings{kristiadi2023promises,
  title={Promises and Pitfalls of the Linearized {L}aplace in {B}ayesian Optimization},
  author={Kristiadi, Agustinus and Immer, Alexander and Eschenhagen, Runa and Fortuin, Vincent},
  booktitle={AABI},
  year={2023}
}

Metadata

Release files for laplacekit-bayesopt 0.1a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for laplacekit-bayesopt 0.1a0
File Size Uploaded Provenance Actions
laplacekit_bayesopt-0.1a0.tar.gz 14.6 kB Without provenance Details for laplacekit_bayesopt-0.1a0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for laplacekit-bayesopt 0.1a0
File Interpreter ABI Platform Actions
laplacekit_bayesopt-0.1a0-py3-none-any.whl Python 3 none any Details for laplacekit_bayesopt-0.1a0-py3-none-any.whl

Total release size: 31.5 kB

Release files / laplacekit_bayesopt-0.1a0.tar.gz

Download URL laplacekit_bayesopt-0.1a0.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e51da6a47e880a49a4d3e58fa52ebd5dc06b43ba996c56ceb8478f119af986e2
BLAKE2b-256 checksum
How to use checksums
b5899877a244ce9c121f490f92e6c92bab7bf5e6abe352ee1f6ba0469b6ed65a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.05","id":"yarara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / laplacekit_bayesopt-0.1a0-py3-none-any.whl

Download URL laplacekit_bayesopt-0.1a0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42c1075b1933fc2381f875e1c0ff9f03157a48c15e5dfe9ecebf4617bcb9b30b
BLAKE2b-256 checksum
How to use checksums
dcaebe1e62bc92e372f5239f1719398098f9638e22d04c2b2636d822e145f72d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.05","id":"yarara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for laplacekit-bayesopt 0.1a0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page