Skip to main content

laplacekit-bayesopt

Bayesian optimization (BO) with Laplace-approximated Bayesian neural networks as surrogates, built on laplacekit.

Install

pip install laplacekit-bayesopt

For language models:

pip install "laplacekit-bayesopt[llm]"

Usage

The Quickstart has the full examples, with and without BoTorch, and the Reference explains every option. For the Laplace approximation itself, see laplacekit.org.

Continuous BO with BoTorch

The surrogate takes the place of a BoTorch model, and everything else in the loop stays the same:

import laplacekit_bayesopt as lkbo

# Load data etc. here
...

for _ in range(rounds):
    # Change your model
    model = lkbo.LaplaceBoTorchSurrogate(
        make_net(),
        train_X,
        train_Y,
        input_transform=Normalize(d=2),
        outcome_transform=Standardize(m=1),
    )

    # Everything else stays the same!
    acq = LogExpectedImprovement(model, best_f=train_Y.max())
    x, _ = optimize_acqf(acq, bounds=bounds, q=1, num_restarts=10, raw_samples=64)
    train_X = torch.cat([train_X, x])
    train_Y = torch.cat([train_Y, objective(x)])

Discrete BO with LLM surrogates

You can reproduce LAPEFT for molecular optimization over a discrete pool of candidates (Kristiadi et al., 2024) with a few lines of code:

import laplacekit_bayesopt as lkbo

# Load data etc. here
...

for i in range(3):
    model = lkbo.LaplaceDiscreteSurrogate(
        lkbo.LoRARegressor(),
        [smiles[j] for j in seen],
        y[seen],
    )

    pending = [j for j in range(len(smiles)) if j not in seen]
    pred = model.predict([smiles[j] for j in pending])
    ei = lkbo.discrete_acqf.expected_improvement(pred, best=y[seen].min(), maximize=False)
    seen.append(pending[ei.argmax()])

    print(f"round {i + 1}: best = {y[seen].min():.3f}")

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}
}

@inproceedings{kristiadi2024sober,
  title={A Sober Look at {LLMs} for Material Discovery: Are They Actually Good for {B}ayesian Optimization Over Molecules?},
  author={Kristiadi, Agustinus and Strieth-Kalthoff, Felix and Skreta, Marta and Poupart, Pascal and Aspuru-Guzik, Al{\'a}n and Pleiss, Geoff},
  booktitle={ICML},
  year={2024}
}