Skip to main content

core.KronEigenbasis

core.KronEigenbasis(
    evecs_a,
    evecs_g,
    evals,
    has_bias=static(),
    weight_shape=static(),
    factors=(),
)

A Kronecker-factored matrix held in its eigenbasis, for one layer’s block.

The matrix acts on the folded matrix \(M = [W \mid b]\) of shape [out, in (+1)]:

\[P \operatorname{vec}(M) = (Q_G \otimes Q_A) \operatorname{diag}(\operatorname{vec} S) (Q_G \otimes Q_A)^\top \operatorname{vec}(M),\]

with \(\operatorname{vec}\) row-major. That is \(M \mapsto Q_G \big(S \odot (Q_G^\top M Q_A)\big) Q_A^\top\). The flat vector is in layout order: the weight row-major (Conv2d weights as [out, in*kh*kw]), then the bias. The dense matrix is never formed.

Parameters

Name Type Description Default
evecs_a Tensor \(Q_A\), [in (+1), in (+1)]. required
evecs_g Tensor \(Q_G\), [out, out]. required
evals Tensor The eigenvalues \(S\), [out, in (+1)]. required
has_bias bool Whether the block has a bias, the last column of \(M\). static()
weight_shape tuple[int, …] The weight’s shape, (out, in, ...). static()
factors tuple[Tensor, …] () or (evals_a [in (+1)], evals_g [out], count []), the eigenvalues \(\lambda_A\) and \(\lambda_G\) of the two Kronecker factors and a count \(c\), with \(S = c \, \lambda_G \lambda_A^\top\). KFAC sets it; EKFAC (corrected eigenvalues) leaves it empty. scaled(s) keeps it and multiplies count by s, so in a posterior count is the fit’s scale, not always an example count. Adding a prior drops it. ()

Raises

Name Type Description
StructureError evecs_a, evecs_g or evals has the wrong shape for weight_shape and has_bias, or factors is neither () nor three tensors of the shapes above.

Methods

Name Description
add_diagonal Always raises: a diagonal prior has no Kronecker form, even a constant one.
add_isotropic \(P + \lambda I\): \(\lambda\) added to every eigenvalue, \(S + \lambda\); drops factors.
add_same Always raises: the sum of two Kronecker eigenbases has no cheap Kronecker form.
cov_sqrt_mv \(L v\) with \(L = (Q_G \otimes Q_A) \operatorname{diag}(\operatorname{vec} S)^{-1/2}\).
diagonal \((Q_G \odot Q_G) \, S \, (Q_A \odot Q_A)^\top\), unfolded.
inverse_diagonal \((Q_G \odot Q_G) \, (1 / S) \, (Q_A \odot Q_A)^\top\), unfolded.
logdet \(\sum_{ij} \log S_{ij}\).
mv \(Q_G \big(S \odot (Q_G^\top M Q_A)\big) Q_A^\top\) on the folded v.
scaled \(s P\): \(s S\), and count times \(s\) in factors.
solve \(Q_G \big((Q_G^\top M Q_A) / S\big) Q_A^\top\) on the folded v.

add_diagonal

core.KronEigenbasis.add_diagonal(d, /)

Always raises: a diagonal prior has no Kronecker form, even a constant one.

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.

Raises

Name Type Description
StructureError Always. The message points to a per-block scalar prior or a curvature without Kronecker structure.

add_isotropic

core.KronEigenbasis.add_isotropic(precision, /)

\(P + \lambda I\): \(\lambda\) added to every eigenvalue, \(S + \lambda\); drops factors.

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.KronEigenbasis.add_same(other, /)

Always raises: the sum of two Kronecker eigenbases has no cheap Kronecker form.

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 Always. For another type the message names both types. For a KronEigenbasis it points to a Diag curvature (or Dense on small blocks), or, for continual learning with KFAC, to lk.priors.KronSum.of(post), which sums the Kronecker factors (approximate).

cov_sqrt_mv

core.KronEigenbasis.cov_sqrt_mv(v, /)

\(L v\) with \(L = (Q_G \otimes Q_A) \operatorname{diag}(\operatorname{vec} S)^{-1/2}\).

On the folded v, that is \(Q_G (M / \sqrt{S}) Q_A^\top\).

Parameters

Name Type Description Default
v Tensor A vector [..., P]. required

Returns

Name Type Description
Tensor \(L v\), shaped like v.

diagonal

core.KronEigenbasis.diagonal()

\((Q_G \odot Q_G) \, S \, (Q_A \odot Q_A)^\top\), unfolded.

Returns

Name Type Description
Tensor A vector [P].

inverse_diagonal

core.KronEigenbasis.inverse_diagonal()

\((Q_G \odot Q_G) \, (1 / S) \, (Q_A \odot Q_A)^\top\), unfolded.

Returns

Name Type Description
Tensor A vector [P].

logdet

core.KronEigenbasis.logdet()

\(\sum_{ij} \log S_{ij}\).

Returns

Name Type Description
Tensor A 0-dim tensor.

mv

core.KronEigenbasis.mv(v, /)

\(Q_G \big(S \odot (Q_G^\top M Q_A)\big) Q_A^\top\) on the folded v.

Parameters

Name Type Description Default
v Tensor A vector [..., P]. required

Returns

Name Type Description
Tensor \(P v\), shaped like v.

scaled

core.KronEigenbasis.scaled(s, /)

\(s P\): \(s S\), and count times \(s\) in factors.

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.KronEigenbasis.solve(v, /)

\(Q_G \big((Q_G^\top M Q_A) / S\big) Q_A^\top\) on the folded v.

Parameters

Name Type Description Default
v Tensor A vector [..., P]. required

Returns

Name Type Description
Tensor \(P^{-1} v\), shaped like v.