# DiagGGN

``` python
DiagGGN(fisher='exact', mc_samples=None, observed=False)
```

Diagonal of the GGN, $C_n = \operatorname{diag}(J_n^\top H_n J_n)$.

`fisher` picks $H_n$ (see `Fisher`):

- `"exact"`: the closed form, through $E$ VJPs per example (needs vjp and vmap). It forms the $E$ per-example gradients, so a batch of $N$ examples costs about $N E$ backward passes of one example and holds $N P$ numbers per output entry. On a large network that is far slower than `KFAC()` or `"mc"`, which cost a few backward passes of the whole batch: an MLP with 235,000 parameters and $E = 10$ takes about a minute on 5,000 examples on a CPU, against under a second for `KFAC()`.
- `"mc"`: the Monte Carlo Fisher from plain backward passes: for each of the $S$ draws $\tilde{y}_n \sim p(y \mid f_n)$, one backward of $\sum_n \sqrt{w_n} \log p(\tilde{y}_n \mid f_n)$, squared. Needs nothing from the model, so it works on any model (a language model with fused attention, say).
- `"empirical"`: the squared per-example gradients at the batch’s targets.

`observed=True` takes the Hessian of $-\log p(y \mid f)$ at the batch’s targets instead of the Fisher, which can be indefinite (a heteroscedastic Gaussian).

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| fisher | Fisher | Which Fisher, `"exact"`, `"mc"` or `"empirical"`. | `'exact'` |
| mc_samples | int \| None | Draws $S$ for `fisher="mc"`, at least 1 (1 when `None`). Refused with another `fisher`. | `None` |
| observed | bool | Use the observed output Hessian at the targets. Needs `fisher="exact"`. | `False` |

## Raises

| Name | Type | Description |
|----|----|----|
|  | ValueError | `fisher` is unknown, `mc_samples` comes without `fisher="mc"` or is below 1, or `observed` comes without `fisher="exact"`. |
