core.Dense
core.Dense(mat, eig=())A full precision matrix \(P\), for small blocks such as a last layer.
With eig set, logdet, solve, cov_sqrt_mv and inverse_diagonal read the eigendecomposition instead of factoring mat, so a prior-precision tune costs one eigh in all. scaled and add_isotropic keep it; add_diagonal and add_same drop it. Without it, they use the Cholesky factor \(P = U^\top U\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| mat | Tensor | The matrix \(P\), [P, P]. |
required |
| eig | tuple[] | tuple[Tensor, Tensor] | () or (evals [P], evecs [P, P]), the eigendecomposition \(P = Q \operatorname{diag}(e) Q^\top\) with eigenvalues \(e\) and eigenvectors \(Q\). with_eigen sets it. It holds a second [P, P] array (384 MB at 9,802 weights in float32). It is computed once from mat; replacing mat afterwards leaves it stale, and gradients through it go through eigh’s backward. Only detached posteriors should carry it. |
() |
Methods
| Name | Description |
|---|---|
| add_diagonal | \(P + \operatorname{diag}(d)\), dropping eig. |
| add_isotropic | \(P + \lambda I\), keeping eig with eigenvalues \(e + \lambda\). |
| add_same | \(P + P'\), dropping eig. |
| cov_sqrt_mv | \(L v\) with \(L = Q \operatorname{diag}(e)^{-1/2}\) from eig, else \(L = U^{-1}\). |
| diagonal | \(\operatorname{diag}(P)\). |
| inverse_diagonal | \(\operatorname{diag}(P^{-1})\): \(\sum_j Q_{ij}^2 / e_j\) from eig. |
| logdet | \(\sum_j \log e_j\) from eig, else \(2 \sum_i \log U_{ii}\). |
| mv | \(P v\). |
| scaled | \(s P\), keeping eig with eigenvalues \(s e\). |
| solve | \(Q \operatorname{diag}(e)^{-1} Q^\top v\) from eig. |
| with_eigen | The same matrix with its eigendecomposition cached in eig. |
add_diagonal
core.Dense.add_diagonal(d, /)\(P + \operatorname{diag}(d)\), dropping eig.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| d | Tensor | The diagonal \(d\), shaped like a vector of the operator. | required |
Returns
| Name | Type | Description |
|---|---|---|
| Self | An operator of the same type. |
add_isotropic
core.Dense.add_isotropic(precision, /)\(P + \lambda I\), keeping eig with eigenvalues \(e + \lambda\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| precision | Tensor | The precision \(\lambda\), a 0-dim tensor. | required |
Returns
| Name | Type | Description |
|---|---|---|
| Self | An operator of the same type. |
add_same
core.Dense.add_same(other, /)\(P + P'\), dropping eig.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| other | Operator[Tensor] | The operator \(P'\). | required |
Returns
| Name | Type | Description |
|---|---|---|
| Self | An operator of the same type. |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | other is not a Dense. |
cov_sqrt_mv
core.Dense.cov_sqrt_mv(v, /)\(L v\) with \(L = Q \operatorname{diag}(e)^{-1/2}\) from eig, else \(L = U^{-1}\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| v | Tensor | A vector [..., P]. |
required |
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | \(L v\), shaped like v. |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | \(P\) is not positive definite: its Cholesky factorization fails or, with eig, an eigenvalue is \(\le 0\) (not checked under torch.func). |
diagonal
core.Dense.diagonal()\(\operatorname{diag}(P)\).
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | A vector [P]. |
inverse_diagonal
core.Dense.inverse_diagonal()\(\operatorname{diag}(P^{-1})\): \(\sum_j Q_{ij}^2 / e_j\) from eig.
Without eig, it forms \(P^{-1} = U^{-1} U^{-\top}\) and takes its diagonal.
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | A vector [P]. |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | \(P\) is not positive definite: its Cholesky factorization fails or, with eig, an eigenvalue is \(\le 0\) (not checked under torch.func). |
logdet
core.Dense.logdet()\(\sum_j \log e_j\) from eig, else \(2 \sum_i \log U_{ii}\).
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | A 0-dim tensor. |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | \(P\) is not positive definite: its Cholesky factorization fails or, with eig, an eigenvalue is \(\le 0\) (not checked under torch.func). |
mv
core.Dense.mv(v, /)\(P v\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| v | Tensor | A vector [..., P]. |
required |
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | \(P v\), shaped like v. |
scaled
core.Dense.scaled(s, /)\(s P\), keeping eig with eigenvalues \(s e\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| s | Tensor | A 0-dim factor. | required |
Returns
| Name | Type | Description |
|---|---|---|
| Self | An operator of the same type. |
solve
core.Dense.solve(v, /)\(Q \operatorname{diag}(e)^{-1} Q^\top v\) from eig.
Without eig, two triangular solves with the Cholesky factor \(U\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| v | Tensor | A vector [..., P]. |
required |
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | \(P^{-1} v\), shaped like v. |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | \(P\) is not positive definite: its Cholesky factorization fails or, with eig, an eigenvalue is \(\le 0\) (not checked under torch.func). |
with_eigen
core.Dense.with_eigen()The same matrix with its eigendecomposition cached in eig.
The eigenvalues are kept as eigh returns them, unclamped: round-off on a rank-deficient GGN gives tiny values of either sign, and a clamp would hide a genuinely indefinite DenseHessian. The positivity check runs on the posterior precision, after the prior is added.
Returns
| Name | Type | Description |
|---|---|---|
| Self | A Dense with the same mat and eig from eigh of \(\tfrac{1}{2}(P + P^\top)\). |