# priors.KronSum

``` python
priors.KronSum(summed, base, mean)
```

A chain of KFAC posteriors as the prior of a KFAC fit, by summing Kronecker factors.

The prior is $\mathcal{N}(\mu, (\Lambda_0 + S)^{-1})$, with $\Lambda_0$ the precision of `base` and $S$ the summed curvature.

Build it with `KronSum.of(post)`. Approximate (Ritter et al. 2018): the sum of two Kronecker products has no Kronecker form, so per layer the sum of an older and a newer curvature is $G \otimes A$ with

$$G = c_o G_o + c_n G_n, \qquad A = \frac{c_o A_o + c_n A_n}{c_o + c_n}.$$

Here $c$ is each fit’s scale times the likelihood’s curvature factor ($1 / \sigma^2$ for a `Gaussian`); with one $\sigma$ for every fit the weights on $A$ are the example counts. $A$ and $G$ are the per-example factors. This is laplace-torch’s rule for sequential fits.

The chain is held flat. So `KronSum.of` of a posterior fitted with a `KronSum` prior extends the sum, and forming a precision decomposes each factor once, however long the chain.

Both curvatures must be `BlockDiag` of `KronEigenbasis` with Kronecker factors (KFAC, not EKFAC) and the same blocks; others raise `StructureError`, for `post` in `of`, for the new fit when its precision is formed (in `lk.laplace`, before the data pass when the method has a template). Not tuned by default and not pushable to function space.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| summed | BlockDiag\[KronEigenbasis\] | $S$, the sum of every earlier fit’s scaled curvature. | *required* |
| base | Prior | The first prior in the chain that is not a `KronSum`, added once, after the sum. | *required* |
| mean | Params | The last posterior’s mean $\mu$. | *required* |

## Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `summed` is not a `BlockDiag` of `KronEigenbasis` with Kronecker factors. |

## Methods

| Name | Description |
|----|----|
| [add_precision_to](#laplacekit.priors.KronSum.add_precision_to) | The Kronecker sum of $S$ and `op`, plus the precision $\Lambda_0$ of `base`. |
| [location](#laplacekit.priors.KronSum.location) | The last posterior’s mean. |
| [log_prob](#laplacekit.priors.KronSum.log_prob) | $\log \mathcal{N}(\theta; \mu, (\Lambda_0 + S)^{-1})$, in the dtype of $S$. |
| [of](#laplacekit.priors.KronSum.of) | The prior for the fit after `post`: its scaled curvature added to the chain. |

### add_precision_to

``` python
priors.KronSum.add_precision_to(op, /)
```

The Kronecker sum of $S$ and `op`, plus the precision $\Lambda_0$ of `base`.

#### Parameters

| Name | Type | Description           | Default    |
|------|------|-----------------------|------------|
| op   | Op   | A precision operator. | *required* |

#### Returns

| Name | Type | Description                                            |
|------|------|--------------------------------------------------------|
|      | Op   | An operator of the same type, $\text{op} + \Lambda_0$. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `op` is not a `BlockDiag` of `KronEigenbasis` with Kronecker factors, a block differs from those of $S$ (its name, weight shape or bias), or `base` can’t add its precision. |
|  | ValueError | A block has zero scale in both $S$ and `op`. |

### location

``` python
priors.KronSum.location(like, /)
```

The last posterior’s mean.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| like | Params | Parameter mapping whose names, shapes, dtypes and devices the mean must have. | *required* |

#### Returns

| Name | Type   | Description                    |
|------|--------|--------------------------------|
|      | Params | The mean, a parameter mapping. |

### log_prob

``` python
priors.KronSum.log_prob(params, /)
```

$\log \mathcal{N}(\theta; \mu, (\Lambda_0 + S)^{-1})$, in the dtype of $S$.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| params | Params | Parameter mapping with the posterior’s keys and shapes. | *required* |

#### Returns

| Name | Type   | Description                       |
|------|--------|-----------------------------------|
|      | Tensor | A 0-dim tensor, $\log q(\theta)$. |

### of

``` python
priors.KronSum.of(post, /)
```

The prior for the fit after `post`: its scaled curvature added to the chain.

With $C$ the curvature of `post` and $c$ its scale times the likelihood’s curvature factor, the new $S$ is $cC$ when the prior of `post` is not a `KronSum`, and the Kronecker sum of the prior’s $S$ and $cC$ (as in the class description) when it is.

#### Parameters

| Name | Type                         | Description       | Default    |
|------|------------------------------|-------------------|------------|
| post | LaplacePosterior\[Any, Any\] | A KFAC posterior. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Self | The `KronSum`, with `post.mean` as its mean. Its `base` is the prior of `post`, or that prior’s `base` when it is a `KronSum`. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | The curvature of `post` is not a `BlockDiag` of `KronEigenbasis` with Kronecker factors, or a block differs from the chain’s (its name, weight shape or bias). |
|  | ValueError | A block has zero scale in both the chain and `post`. |
