# OnlineMarglik

``` python
OnlineMarglik(
    epochs=1000,
    lr=0.1,
    marglik_frequency=50,
    batch_size=10,
    prior=lk.priors.Layerwise,
    hessian_approx=lk.KFAC(),
    noise=0.001,
)
```

Train with marginal-likelihood steps on the prior (Immer et al. 2021).

This runs `lk.marglik.train` with Adam and a cosine learning-rate schedule. Every `marglik_frequency` epochs, a Laplace fit of all trainable parameters gives the marginal likelihood, and the prior precision takes a step on it. The noise stays fixed at `noise`, or at the surrogate’s `train_Yvar`; with `noise=None` and no `train_Yvar`, it takes steps too.

With `noise=None` at small `n`, the marginal likelihood can prefer to explain the outcomes as noise: the prior precision grows, the weights shrink to zero, and the network ends up predicting a constant. On Hartmann6 and Branin with 8 or 25 points, that happened in 9 of 12 fits, and in none with the default fixed noise. `LaplaceBoTorchSurrogate` warns when it does.

``` python
trainer = OnlineMarglik(epochs=500, marglik_frequency=25)
model = LaplaceBoTorchSurrogate(make_net(), train_X, train_Y, trainer=trainer)
```

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| epochs | int | Passes over the data. | `1000` |
| lr | float | Adam’s learning rate at the start of the schedule. | `0.1` |
| marglik_frequency | int | Epochs between marginal-likelihood steps. | `50` |
| batch_size | int | Examples per training step. | `10` |
| prior | type\[\_BuiltinPrior\] \| \_BuiltinPrior | How the weights share the prior precision, a class or an instance with one shared starting precision, as in `lk.marglik.train`. | `lk.priors.Layerwise` |
| hessian_approx | HessianApprox\[Any\] \| None | The curvature method of each Laplace fit, KFAC by default. `None` lets `lk.laplace` pick one per block. | `lk.KFAC()` |
| noise | float \| None | The fixed noise variance, as a fraction of the variance of the outcomes the trainer gets (so with standardized outcomes, the noise variance itself). A `train_Yvar` given to the surrogate takes its place. `None` tunes the noise. | `0.001` |

## Raises

| Name | Type       | Description                                        |
|------|------------|----------------------------------------------------|
|      | ValueError | An option is not positive (`noise` may be `None`). |
