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 y∼p(y∣x;θ)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 Ex∼pdata(x),  y∼p(y∣x;θ)[(∇θlog⁡p(y∣x;θ))(∇θlog⁡p(y∣x;θ))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 ∇Wllog⁡p(y∣x,θ)\nabla_{W_{l}} \log p(y \mid x, \theta). By the chain rule:

∇Wllog⁡p(y∣x,θ)=∇Wlal−1log⁡p(y∣x,θ)⋅∇WlWlal−1\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} =∗∇Wlal−1log⁡p(y∣x,θ)⋅al−1T\overset{*}{=} \nabla_{W_{l} a_{l-1}} \log p(y \mid x, \theta) \cdot a_{l-1}^{T}

where ∗* follows from the fact that ∇Wlal−1=0\nabla_{W_{l}} a_{l-1} = 0

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

∇θllog⁡p(y∣x,θ)=vec⁡(∇Wllog⁡p(y∣x;θ))\nabla_{\theta_{l}} \log p (y \mid x, \theta) = \operatorname{vec}(\nabla _{W_{l}} \log p(y \mid x; \theta))

Substituting,

∇θllog⁡p(y∣x,θ)=vec⁡(∇zllog⁡p(y∣x,θ)⋅al−1T)\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⁡(u⋅vT)=v⊗u\operatorname{vec}(u \cdot v^{T}) = v \otimes u when u∈Rmu \in \mathbb{R}^m and v∈Rnv \in \mathbb{R}^n.

This is easy to verify yourself! Applying the lemma,

∇θllog⁡p(y∣x;θ)=al−1⊗∇zllog⁡p(y∣x;θ)\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[(∇θllog⁡p(y∣x;θ))(∇θllog⁡p(y∣x;θ))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[(al−1⊗∇zllog⁡p(y∣x;θ))⋅(al−1⊗∇zllog⁡p(y∣x;θ))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[al−1al−1T⊗(∇zllog⁡p(y∣x;θ))(∇zllog⁡p(y∣x;θ))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 (A⊗B)⋅(C⊗D)=AC⊗BD(A \otimes B) \cdot (C \otimes D) = AC \otimes BD and that (u⊗v)T=uT⊗vT(u \otimes v)^T = u^T \otimes v^T.

To simplify this expression, we make the following assumption: al−1a_{l-1} and ∇zllog⁡p(y∣x;θ)\nabla_{z_{l}} \log p(y \mid x; \theta) are independent, i.e. when taking the expected value over many examples, al−1a_{l-1} doesn't provide much information about ∇zllog⁡p(y∣x;θ)\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[al−1al−1T]⊗E[(∇zllog⁡p(y∣x;θ))(∇zllog⁡p(y∣x;θ))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 Al−1A_{l-1} and the second BlB_{l}, we have:

Fl=Al−1⊗BlF_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 i≠ji \neq j. This means that for any two layers ll and kk, we are assuming the following:

E[(∇θllog⁡p(y∣x;θ))(∇θklog⁡p(y∣x;θ))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:V→VS: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=Al−1A = 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. PA⋅PAT=PAT⋅PA=IP_A \cdot P_A^T = P_A^T \cdot P_A = I i.e. PAT=PA−1P_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=(PA⊗PB)(ΛA⊗ΛB)(PAT⊗PBT)=(PA⊗PB)(ΛA⊗ΛB)(PA⊗PB)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 PA⊗PBP_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∇θllog⁡p(y∣x;θ)∇θllog⁡p(y∣x;θ)Tx]=E[(xT∇θllog⁡p(y∣x;θ))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 PA⊗PBP_A \otimes P_B are orthonormal eigenvectors, we can assemble Λ\Lambda as follows:

Λii=E[([(PA⊗PB)T∇θllog⁡p(y∣x;θ)]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.