# priors.Layerwise

``` python
priors.Layerwise(prior_precision=1.0, mean=None)
```

Gaussian prior with one precision per block, $\prod_b \mathcal{N}(\mu_b, \lambda_b^{-1} I)$.

Blocks group parameters by module prefix (`Layout.by_module`). Needs a per-block operator (`BlockDiag` or `Joint`).

`Layerwise()` starts every block at precision 1, and `lk.laplace` expands it to one precision per block (`expand`) before the tune.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| prior_precision | float \| Tensor \| Mapping\[str, float \| Tensor\] | The precisions $\lambda_b > 0$: a float or 0-dim tensor (the same for every block), or a mapping from block name to a float or 0-dim tensor, whose keys must be the blocks of the parameters. The marginal-likelihood tuner of `lk.laplace` tunes them by default. | `1.0` |
| mean | Params \| None | Prior mean $\mu$ as a parameter mapping. `None` means zero. | `None` |

## Methods

| Name | Description |
|----|----|
| [add_precision_to](#laplacekit.priors.Layerwise.add_precision_to) | `op` plus $\lambda_b I$ on each block $b$, through `op.add_per_block`. |
| [cov_mv](#laplacekit.priors.Layerwise.cov_mv) | Covariance times `v`: $v_b / \lambda_b$ on each block $b$. |
| [expand](#laplacekit.priors.Layerwise.expand) | The prior with one precision per block of `params`. |
| [location](#laplacekit.priors.Layerwise.location) | The mean, shaped like `like`. |
| [log_prob](#laplacekit.priors.Layerwise.log_prob) | Log density at `params`. |
| [tunable](#laplacekit.priors.Layerwise.tunable) | The precisions, by block, or the one shared by all blocks. |

### add_precision_to

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

`op` plus $\lambda_b I$ on each block $b$, through `op.add_per_block`.

#### 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 per-block operator (`BlockDiag` or `Joint`), or its block names differ from the keys of `prior_precision`. |

### cov_mv

``` python
priors.Layerwise.cov_mv(v, /)
```

Covariance times `v`: $v_b / \lambda_b$ on each block $b$.

#### Parameters

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

#### Returns

| Name | Type   | Description                      |
|------|--------|----------------------------------|
|      | Params | $\Sigma v$, a parameter mapping. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | The keys of `prior_precision` differ from the blocks of the parameters. |

### expand

``` python
priors.Layerwise.expand(params, /)
```

The prior with one precision per block of `params`.

#### Parameters

| Name   | Type   | Description                       | Default    |
|--------|--------|-----------------------------------|------------|
| params | Params | The parameters the prior is over. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Self | `self` when the precision is already a mapping, else a copy whose precision maps each block (`Layout.by_module`) to its own copy of the scalar. |

### location

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

The mean, shaped like `like`.

#### 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.Layerwise.log_prob(params, /)
```

Log density at `params`.

#### 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)$. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | The keys of `prior_precision` differ from the blocks of the parameters. |

### tunable

``` python
priors.Layerwise.tunable()
```

The precisions, by block, or the one shared by all blocks.

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Tensor \| Mapping\[str, Tensor\] | The prior’s precision, which is a leaf of the prior and positive, since the tune works on its log. |
