# DenseHessian

``` python
DenseHessian(joint=False)
```

Exact Hessian of the negative log-likelihood at the batch’s targets.

$$C_n = -\nabla_\theta^2 \log p(y_n \mid f(x_n; \theta)),$$

with masked positions left out, so $\bar{C} = \frac{1}{W} \sum_n w_n C_n$.

Per block by default: each block’s Hessian, with the other blocks held at their values. Built with `jacrev(jacrev(...))`, which vmaps a double backward over $P$ basis vectors, for $P$ parameters in the block (memory about $P$ times the activations), so it needs vjp_twice and vmap from the probe. That refuses some models `jacrev` could handle; the GGN curvatures still fit them. The probe checks the double backward in the cotangent only, so a custom backward that detaches its saved inputs passes it and gives a wrong Hessian.

The Hessian can be indefinite away from a minimum of the loss. The posterior precision can then be indefinite too, and its Cholesky raises `StructureError`. A larger prior precision makes it positive definite; the GGN and Fisher curvatures are always PSD.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| joint | bool | Keep one matrix over all blocks, in layout order, as a `Joint`. $P$ is then the total parameter count. | `False` |

## Attributes

| Name | Description |
|----|----|
| [requires](#laplacekit.DenseHessian.requires) | Needs vjp, vjp_twice and vmap, and targets. |

## Methods

| Name | Description |
|----|----|
| [finalize](#laplacekit.DenseHessian.finalize) | $\bar{C}$ as `Dense` operators per block, or one `Joint` with `joint=True`. |
| [init](#laplacekit.DenseHessian.init) | Zero matrices per block, or one over all blocks with `joint=True`. |
| [template](#laplacekit.DenseHessian.template) | Zero `Dense` operators, per block or one `Joint`. |
| [update](#laplacekit.DenseHessian.update) | Add $\sum_n w_n C_n$, the Hessian of the batch’s weighted negative log-likelihood. |

### finalize

``` python
DenseHessian.finalize(stat, /)
```

$\bar{C}$ as `Dense` operators per block, or one `Joint` with `joint=True`.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| stat | \_DenseStat | The statistic over all the data, after `all_reduce`. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | BlockDiag\[core.Dense\] \| Joint\[core.Dense\] | The per-example average $\bar{C} = \sum_n w_n C_n / \sum_n w_n$. The posterior multiplies it by `scale`. |

### init

``` python
DenseHessian.init(params, ctx, /)
```

Zero matrices per block, or one over all blocks with `joint=True`.

#### Parameters

| Name   | Type       | Description                           | Default    |
|--------|------------|---------------------------------------|------------|
| params | Params     | The params the curvature is taken at. | *required* |
| ctx    | FitContext | The fit’s context.                    | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | \_DenseStat | A statistic $e$ with $e + s = s$ for every statistic $s$ of this fit. |

### template

``` python
DenseHessian.template(params, ctx, /)
```

Zero `Dense` operators, per block or one `Joint`.

#### Parameters

| Name   | Type       | Description                           | Default    |
|--------|------------|---------------------------------------|------------|
| params | Params     | The params the curvature is taken at. | *required* |
| ctx    | FitContext | The fit’s context.                    | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | BlockDiag\[core.Dense\] \| Joint\[core.Dense\] | A zero operator with the operator types, blocks, static fields, leaf shapes and dtypes that `finalize` returns. |

### update

``` python
DenseHessian.update(stat, params, batch, generator, ctx, /)
```

Add $\sum_n w_n C_n$, the Hessian of the batch’s weighted negative log-likelihood.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| stat | \_DenseStat | The statistic so far. | *required* |
| params | Params | The params the curvature is taken at. | *required* |
| batch | Batch | The batch to add. | *required* |
| generator | torch.Generator | The batch’s generator, for sampled methods. | *required* |
| ctx | FitContext | The fit’s context. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | \_DenseStat | `stat` with the batch added. Updating with batches $a$, then $b$, equals adding the statistics of $a$ and of $b$. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | CapabilityError | The batch has no targets. |
|  | ValueError | `batch.weights` has a negative entry. |
|  | StructureError | The likelihood refuses the outputs or targets, or its mask is not bool or not shaped like its `log_prob`. |
