Eigenvalue-Corrected Kronecker-Factored Approximate Curvature

This is meant to be a readable explanation of KFAC and EKFAC.

KFAC

Kronecker-Factored Approximate Curvature (KFAC), introduced by Martens and Grosse, is a method to approximate the Fisher Information Matrix (FIM). This is often useful for computing influence functions, where we need to invert a large Hessian. This is costly, so we seek to approximate the Hessian via the Gauss-Newton Hessian JTHyJJ^T H_y J.

Fact. The FIM equals the expected Hessian of the negative log-likelihood (where the expectation is taken over yp(yx;θ)y \sim p(y \mid x; \theta)). This expected Hessian coincides with the Gauss-Newton Hessian JTHyJJ^T H_y J.

So approximating the FIM Expdata(x),  yp(yx;θ)[(θlogp(yx;θ))(θlogp(yx;θ))T]\mathbb{E_{x \sim p_{data}(x), \; y \sim p(y \mid x; \theta)}}[(\nabla_{\theta} \log p(y \mid x; \theta))(\nabla_{\theta} \log p(y \mid x; \theta))^{T}] as an efficiently invertible matrix allows us to also efficiently invert the Hessian.


Approximating the FIM

Consider the gradient of the logprobs w.r.t. a certain layer ll's weights Wllogp(yx,θ)\nabla_{W_{l}} \log p(y \mid x, \theta). By the chain rule:

Wllogp(yx,θ)=Wlal1logp(yx,θ)WlWlal1=Wlal1logp(yx,θ)al1T\nabla_{W_{l}} \log p(y \mid x, \theta) = \nabla_{W_{l} a_{l-1}} \log p(y \mid x, \theta) \cdot \nabla_{W_{l}} W_{l} a_{l-1} \overset{*}{=} \nabla_{W_{l} a_{l-1}} \log p(y \mid x, \theta) \cdot a_{l-1}^{T}

where * follows from the fact that Wlal1=0\nabla_{W_{l}} a_{l-1} = 0

Now define θl=vec(Wl)\theta_{l} = \operatorname{vec}(W_l) and zl=Wlal1z_{l} = W_{l} a_{l-1}. So we have:

θllogp(yx,θ)=vec(Wllogp(yx;θ))\nabla_{\theta_{l}} \log p (y \mid x, \theta) = \operatorname{vec}(\nabla _{W_{l}} \log p(y \mid x; \theta))

Substituting,

θllogp(yx,θ)=vec(zllogp(yx,θ)al1T)\nabla_{\theta_{l}} \log p (y \mid x, \theta) = \operatorname{vec}(\nabla_{z_l} \log p(y \mid x, \theta) \cdot a_{l-1}^{T})

Lemma. vec(uvT)=vu\operatorname{vec}(u \cdot v^{T}) = v \otimes u when uRmu \in \mathbb{R}^m and vRnv \in \mathbb{R}^n.

This is easy to verify yourself! Applying the lemma,

θllogp(yx;θ)=al1zllogp(yx;θ)\nabla_{\theta_{l}} \log p(y \mid x; \theta) = a_{l-1} \otimes \nabla_{z_{l}} \log p(y \mid x; \theta)

As I mentioned previously, the Fisher Information Matrix for layer ll is given by:

Fl=E[(θllogp(yx;θ))(θllogp(yx;θ))T]F_l = \mathbb{E}[(\nabla_{\theta_{l}} \log p(y \mid x; \theta))(\nabla_{\theta_{l}} \log p(y \mid x; \theta))^{T}]

Substituting our expression for the gradient,

Fl=E[(al1zllogp(yx;θ))(al1zllogp(yx;θ))T]F_l = \mathbb{E}[(a_{l-1} \otimes \nabla_{z_{l}} \log p(y \mid x; \theta)) \cdot (a_{l-1} \otimes \nabla_{z_{l}} \log p(y \mid x; \theta))^T]

We can write:

Fl=E[al1al1T(zllogp(yx;θ))(zllogp(yx;θ))T]F_l = \mathbb{E}[a_{l-1}a_{l-1}^T \otimes (\nabla_{z_{l}} \log p(y \mid x; \theta)) (\nabla_{z_{l}}\log p(y \mid x; \theta))^T]

Here we used the mixed product property (AB)(CD)=ACBD(A \otimes B) \cdot (C \otimes D) = AC \otimes BD and that (uv)T=uTvT(u \otimes v)^T = u^T \otimes v^T.

To simplify this expression, we make the following assumption: al1a_{l-1} and zllogp(yx;θ)\nabla_{z_{l}} \log p(y \mid x; \theta) are independent, i.e. when taking the expected value over many examples, al1a_{l-1} doesn't provide much information about zllogp(yx;θ)\nabla_{z_{l}} \log p(y \mid x; \theta).

Remark. This assumption is not accurate in practice, and can cause trouble. This is why we need eigenvalue correction!

This assumption allows us to separate the expected value:

Fl=E[al1al1T]E[(zllogp(yx;θ))(zllogp(yx;θ))T]F_l = \mathbb{E}[a_{l-1}a_{l-1}^T] \otimes \mathbb{E}[(\nabla_{z_{l}} \log p(y \mid x; \theta)) (\nabla_{z_{l}}\log p(y \mid x; \theta))^T]

Designating the first matrix Al1A_{l-1} and the second BlB_{l}, we have:

Fl=Al1BlF_l = A_{l-1} \otimes B_{l}

Note that we have obtained the FIM only for a single layer ll. The FIM for the entire network is a block matrix:

F=[F11F12F13F21F22F23F31F32F33]F = \begin{bmatrix} F_{11} & F_{12} & F_{13} \\ F_{21} & F_{22} & F_{23} \\ F_{31} & F_{32} & F_{33}\end{bmatrix}

Here, we make another assumption: that FF is block-diagonal, i.e. Fij=0F_{ij} = 0 when iji \neq j. This means that for any two layers ll and kk, we are assuming the following:

E[(θllogp(yx;θ))(θklogp(yx;θ))T]=0\mathbb{E}[(\nabla_{\theta_{l}} \log p(y \mid x; \theta)) (\nabla_{\theta_{k}}\log p(y \mid x; \theta))^T] = 0

Then we can assemble FF as:

F=blockdiag(F1,F2,...,FL)F = \operatorname{blockdiag}(F_1, F_2, ..., F_L)

Eigenvalue-Corrected KFAC

(Spectral Theorem for Real Symmetric Matrices). Let VV be a real inner product space with dim(V)<\dim(V) < \infty, and S:VVS:V \to V be a symmetric linear map so that S=STS=S^T. Then SS is normal (SST=S2=STSSS^T = S^2 = S^T S) and admits an orthonormal basis of eigenvectors.

Remark. In this section, I write A=Al1A = A_{l-1} and B=BlB = B_l for simplicity's sake.

Notice that AA and BB are real symmetric matrices. Therefore, by the spectral theorem, they admit orthonormal bases of eigenvectors, which means we can diagonalize them:

A=PAΛAPATB=PBΛBPBTA = P_A \Lambda_A P_A^{T} \qquad B = P_B \Lambda_B P_B^{T}

where PAP_A and PBP_B are orthogonal, i.e. PAPAT=PATPA=IP_A \cdot P_A^T = P_A^T \cdot P_A = I i.e. PAT=PA1P_A^T = P_A^{-1}

Substituting,

Fl=(PAΛAPAT)(PBΛBPBT)F_l = (P_A \Lambda_A P_A^{T}) \otimes (P_B \Lambda_B P_B^{T})

Applying the same identities as before,

Fl=(PAPB)(ΛAΛB)(PATPBT)=(PAPB)(ΛAΛB)(PAPB)TF_l = (P_A \otimes P_B) (\Lambda_A \otimes \Lambda_B) (P_A^{T} \otimes P_B^{T}) = (P_A \otimes P_B) (\Lambda_A \otimes \Lambda_B) (P_A \otimes P_B)^{T}

This means that the matrix ΛAΛB\Lambda_A \otimes \Lambda_B contains the eigenvalues of FlF_l.

Problem. What happens if these eigenvalues are wrong because the independence assumption we made earlier was imperfect?

Solution. We compute the eigenvalues of FlF_l directly, i.e. a matrix Λ\Lambda which is not necessarily equal to ΛAΛB\Lambda_A \otimes \Lambda_B.

To do this, we project the gradient onto the columns of PAPBP_A \otimes P_B, i.e. our (assumed) eigenbasis for FlF_l and take the expected value of the squared projection.

First, recall the expression for the general quadratic form of a column vector xx: q(x)=xTMxq(x) = x^T M x. Now with M=FlM = F_l, we have:

xTFlx=E[xTθllogp(yx;θ)θllogp(yx;θ)Tx]=E[(xTθllogp(yx;θ))2]x^T F_lx = \mathbb{E}[x^T \nabla_{\theta_l} \log p(y \mid x; \theta) \nabla_{\theta_l} \log p(y \mid x; \theta)^T x] = \mathbb{E}[(x^T \nabla_{\theta_l} \log p(y \mid x; \theta))^2]

Note that for an eigenvector xx of MM such that x=1\lVert x \rVert = 1, xTMx=λxTx=λx^T M x = \lambda x^T x = \lambda. Since the columns of the matrix PAPBP_A \otimes P_B are orthonormal eigenvectors, we can assemble Λ\Lambda as follows:

Λii=E[([(PAPB)Tθllogp(yx;θ)]i)2]\Lambda_{ii} = \mathbb{E} \left[ \left ([(P_A \otimes P_B)^{T} \nabla_{\theta_l} \log p(y \mid x; \theta )]_{i} \right)^2 \right]

This allows us to keep our convenient assumption about the eigenbasis while correcting the inaccurate eigenvalues.