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. At small n, it often ends with a network that predicts a constant.

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

Example:

from laplacekit_bayesopt import PosthocMarglik

trainer = PosthocMarglik(epochs=500, lr=0.01)

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.1.0

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

Built distribution (wheel)

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

Total release size: 33.6 kB

Release files / laplacekit_bayesopt-1.1.0.tar.gz

Download URL laplacekit_bayesopt-1.1.0.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
49fc71becfba7a1d396af5769d47a33a9599f26507c7aa81f364ae8c725ec8e9
BLAKE2b-256 checksum
How to use checksums
ed4b66226007da721e68ebb3ede873d27c9a590f7d7c0bd3ff7b780f45973b97
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.1.0-py3-none-any.whl

Download URL laplacekit_bayesopt-1.1.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09539e00dc05a8bbed0204d0784f1115d916582525bc3b513d89c5ec6b60afda
BLAKE2b-256 checksum
How to use checksums
da3ec170fa32f5aebdbc6c0c7a429736bd608e8dc20de391a1dba74323cbf4dc
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.1.0

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.1.0.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