# ![](assets/logo.svg)![](assets/logo-dark.svg)laplace**kit**-bayesopt

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

- **With [BoTorch](https://botorch.org):** `LaplaceBoTorchSurrogate` is a drop-in alternative to Gaussian process (GP) surrogates such as `SingleTaskGP`, for single- and multi-objective BO. Your BoTorch loop stays the same.
- **Without BoTorch:** `LaplaceDiscreteSurrogate` scores a discrete set of candidates, which need not be vectors (texts, molecules, etc.), and the network can be a pretrained language model.

## Install

``` bash
pip install laplacekit-bayesopt
```

For language models:

``` bash
pip install "laplacekit-bayesopt[llm]"
```

## Usage

The [Quickstart](quickstart/basics.llms.md) has the full examples, with and without BoTorch, and the [Reference](reference/index.llms.md) explains every option. For the Laplace approximation itself, see [laplacekit.org](https://laplacekit.org).

### Continuous BO with BoTorch

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

``` python
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](https://arxiv.org/abs/2402.05015)) with a few lines of code:

``` python
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

``` bibtex
@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}
}
```
