Skip to main content

laplacekit-bayesopt

A laplacekit plugin for Bayesian optimization (BO) with BoTorch. Essentially, a drop-in alternative for Gaussian process (GP) surrogates. Here, the surrogate is a Laplace-approximated Bayesian neural network (BNN).

Install

pip install laplacekit-bayesopt

Usage

LaplaceBNNSurrogate is a BoTorch Model. Use it in a BoTorch loop in place of standard BoTorch models such as SingleTaskGP, etc.

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):
    # The only difference!
    model = LaplaceBNNSurrogate(
        make_net(),
        train_X,
        train_Y,
        input_transform=Normalize(d=2),
        outcome_transform=Standardize(m=1),
    )

    # Everything below stays the same!
    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 standard BoTorch models, we build a new model in each iteration. The posterior by default is the linearized Laplace approximation (which is theoretically a Gaussian process!) with KFAC.

Training

Since the surrogate is a neural network, it needs to be trained. A Trainer object does this for us, and there are two options (see the paper)

Trainer What it does
PosthocMarglik() (default) Adam on the mean squared error, then tunes the prior precision and the noise on the marginal likelihood. Cheaper, but theoretically less correct.
OnlineMarglik() Tunes the prior precision and the noise during training (Immer et al. 2021). More expensive, but theoretically better.

Various options are available---see docstrings. For the Laplace approximation itself, refer to https://laplacekit.org.

Example:

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)

Example Usages

See the examples/ directory.

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 1.0.1

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 1.0.1
File Size Uploaded Provenance Actions
laplacekit_bayesopt-1.0.1.tar.gz 14.2 kB With provenance Details for laplacekit_bayesopt-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 30.6 kB

Release files / laplacekit_bayesopt-1.0.1.tar.gz

Download URL laplacekit_bayesopt-1.0.1.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f2e477567152bdb1909d65f10e9a4c93bf614996a57efde8628e68c0999429a7
BLAKE2b-256 checksum
How to use checksums
7510abbca143f3b822fe016cd5f919eef0e8bd282910509cd6dd0c69cca4cec4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Release files / laplacekit_bayesopt-1.0.1-py3-none-any.whl

Download URL laplacekit_bayesopt-1.0.1-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e1133b466e713fa5e25dd9224aeb4b4b6e58e88f58b6397c62e72ddba03062e
BLAKE2b-256 checksum
How to use checksums
814e82013bed94e98ae706d1237b4759b6903eadf0f1ff0de801e367a99980b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Security for laplacekit-bayesopt 1.0.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for laplacekit_bayesopt-1.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

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