Ten problems on differentiating through a matrix inverse and a determinant: the differential of A⁻¹, the trace trick that reads a gradient off a differential, Jacobi's formula for det and log det, the chain rule through a parametrised matrix, backprop through a linear solve and through an inverse, the Sherman–Morrison and determinant lemmas for a rank-one update, log det through a Cholesky factor, the Gaussian log-likelihood gradient with respect to that factor, and log det through the eigenvalues, with worked solutions and the mistakes that put the factors in the wrong order.
Before you start
A matrix inverse and a determinant appear wherever a Gaussian does: the log-likelihood of a covariance, the marginal likelihood of a Gaussian process, the entropy of a VAE's posterior, a Kalman update. Differentiating through them by entries is hopeless, but there is a method that makes every one of these a few lines: write the differential of the expression, move everything into a trace, and read the gradient off the trace. These ten problems build that method from the differential of A−1 and Jacobi's formula for ,detA, then use it for what a framework actually has to do, which is backpropagate through a linear solve and an inverse, and for the parametrisations that appear in practice: a rank-one update, a Cholesky factor, and the eigenvalues. The five mistakes at the end each give an answer of the right shape: the inverse differentiated like a reciprocal, a transpose lost in the trace trick, det confused with ,logdet, a Cholesky gradient that lands above the diagonal, and a chain rule through LL⊤ that counts L once.
Gradients follow the conventions of the matrix-calculus page: for a scalar f(X) with ,X∈Rm×n,∇Xf has the shape of X and .(∇Xf)ij=∂f/∂Xij.
The Frobenius inner product of two matrices of the same shape is .⟨A,B⟩=tr(A⊤B)=∑ijAijBij. The trace is linear, ,tr(A⊤)=tr(A), and it is cyclic: tr(ABC)=tr(CAB)=tr(BCA) whenever the products are defined. Cyclic is not commutative: tr(ABC)=tr(ACB) in general.
The differential dX of a matrix variable is an arbitrary small change of the same shape. For a scalar ,f, the differential df is linear in ,dX, and if df=⟨C,dX⟩ for every dX then :∇Xf=C: this is the trace trick, and it is proved in Problem 2. Derivatives of products follow the product rule, ,d(AB)=(dA)B+AdB, in that order, because matrices do not commute.
The derivative of f along a direction E is dtdf(A+tE) at ;t=0; for a scalar f it equals .⟨∇Af,E⟩.
A is a square invertible matrix, A−⊤ is ,(A−1)⊤=(A⊤)−1, and logdetA is taken where .detA>0.I is the identity.
L is a scalar loss, and the upstream gradient of an intermediate quantity is written with g or :G: for ,x=A−1b,;g=∇xL; for ,Y=A−1,.G=∇YL.
A Cholesky factor L (the letter is reused, as it is everywhere) is lower triangular with positive diagonal and ;Σ=LL⊤; its free parameters are the n(n+1)/2 entries on and below the diagonal. tril(M) keeps the entries of M on and below the diagonal and zeroes the rest.
The multivariate-Gaussian page showed ∇ΣlogdetΣ=Σ−1 for symmetric Σ and, for the Gaussian log-likelihood ℓ(Σ)=−2NlogdetΣ−21tr(Σ−1S)+const with scatter matrix ,S, that .∇Σℓ=−2NΣ−1+21Σ−1SΣ−1. Problems 3 and 9 use these.
Show that ,d(A−1)=−A−1(dA)A−1, and write the derivative of A−1 along a direction .E.
··
Prove the trace trick: if df=tr(C⊤dX) for every ,dX, then .∇Xf=C. Then use it on f(X)=a⊤X−1b with X invertible and ,a,b fixed vectors.
··
Show that ,dtddet(I+tM)t=0=trM, and derive Jacobi's formula .d(detA)=det(A)tr(A−1dA). Deduce ∇AdetA and .∇AlogdetA.
··
Let A(θ) be an invertible matrix depending on a scalar ,θ, with derivative .A′(θ). Show that dθdlogdetA(θ)=tr(A−1A′) and .dθdA(θ)−1=−A−1A′A−1. Evaluate both for A(θ)=B+θI with B symmetric positive definite and eigenvalues ,λi, and .θ>0.
···
A layer solves x=A−1b (as solve(A, b), without forming ).A−1). With upstream gradient ,g=∇xL, compute ∇bL and ,∇AL, and say what the backward pass costs.
··
A layer computes Y=A−1 explicitly. With upstream gradient ,G=∇YL, compute ∇AL in terms of Y and .G.
··
Rank-one update. For invertible A and vectors ,u,v with ,1+v⊤A−1u=0, show the Sherman–Morrison formula (A+uv⊤)−1=A−1−1+v⊤A−1uA−1uv⊤A−1 and the matrix determinant lemma .det(A+uv⊤)=det(A)(1+v⊤A−1u). Then, with 1+v⊤A−1u>0 and ,detA>0, compute .∇ulogdet(A+uv⊤).
···
Let Σ=LL⊤ with L a Cholesky factor. Show that ,logdetΣ=2∑ilogLii, and compute the gradient of logdetΣ with respect to the free entries of ,L, once directly and once through .∇ΣlogdetΣ=Σ−1.
···
The Gaussian log-likelihood of Before you start, ,ℓ(Σ)=−2NlogdetΣ−21tr(Σ−1S)+const, is optimised through .Σ=LL⊤. Starting from ,∇Σℓ, compute the gradient with respect to the free entries of ,L, simplify it using ,M=L−1SL−⊤, and show that it vanishes at .Σ=S/N.
··
Let A be symmetric with distinct eigenvalues λi and orthonormal eigenvectors ,qi, and let E be symmetric. Show that along A+tE each eigenvalue has derivative dtdλi=qi⊤Eqi at ,t=0, and use it to recover dtdlogdet(A+tE)=tr(A−1E) for positive definite .A.
Answers
;d(A−1)=−A−1(dA)A−1; along ,E,dtd(A+tE)−1t=0=−A−1EA−1
;logdetΣ=2∑ilogLii; the gradient over the free entries of L is 2/Lii on the diagonal and 0 below it, which is tril(2Σ−1L)=tril(2L−⊤)
∇Lℓ=tril(2∇ΣℓL)=tril(L−⊤(M−NI)) with ;M=L−1SL−⊤; it is 0 at Σ=S/N
,dtdλi=qi⊤Eqi, and dtdlogdet(A+tE)=∑iqi⊤Eqi/λi=tr(A−1E)
Worked solutions
Problem 1
Show that ,d(A−1)=−A−1(dA)A−1, and write the derivative of A−1 along a direction .E.
A−1A=I for every invertible .A.The definition of the inverse, and the identity does not change, so its differential is .0.
.d(A−1)A+A−1dA=0.Product rule on the left side, factors kept in order; dI=0 on the right.
.d(A−1)A=−A−1dA.Move the second term across.
.d(A−1)=−A−1(dA)A−1.Multiply both sides by A−1 on the right. It has to go on the right, where the A is; multiplying on the left would not cancel it.
;d(A−1)=−A−1(dA)A−1; along ,E,dtd(A+tE)−1t=0=−A−1EA−1For a 1×1 matrix this is .d(1/a)=−da/a2. The matrix version keeps the two factors of A−1 apart with dA between them, and Problem 2 onwards never needs more than this line and the trace trick.
Problem 2
Prove the trace trick: if df=tr(C⊤dX) for every ,dX, then .∇Xf=C. Then use it on f(X)=a⊤X−1b with X invertible and ,a,b fixed vectors.
.tr(C⊤dX)=∑ijCijdXij.Entry (j,j) of C⊤dX is ;∑iCijdXij; the trace sums over .j.
.df=∑ij∂Xij∂fdXij.The differential of a scalar function of many variables is the sum of its partial derivatives times the changes in the variables.
Both are linear in the entries of dX and agree for every ,dX, so .∂f/∂Xij=Cij.Take dX with a single nonzero entry at :(i,j): each side reduces to that one coefficient. That is .∇Xf=C.
.df=a⊤d(X−1)b=−a⊤X−1(dX)X−1b.a and b are constant, and Problem 1 gives the differential of the inverse.
.=−tr(a⊤X−1(dX)X−1b)=−tr(X−1ba⊤X−1dX).A scalar is its own trace, and the cyclic property moves the vector factor X−1b from the end to the front, keeping the order ,X−1b, then ,a⊤X−1, then .dX.
,C⊤=−X−1ba⊤X−1, so .C=−(X−1ba⊤X−1)⊤=−X−⊤ab⊤X−⊤.Match step 5 to the form ,tr(C⊤dX), then transpose: the order reverses, ,(ba⊤)⊤=ab⊤, and .(X−1)⊤=X−⊤.
∇Xf=C whenever ;df=tr(C⊤dX);∇X(a⊤X−1b)=−X−⊤ab⊤X−⊤,n×n, the shape of .X. The Gaussian page's ∇Σ(v⊤Σ−1v)=−Σ−1vv⊤Σ−1 is the case a=b=v with X symmetric, so that the transposes disappear.
Problem 3
Show that ,dtddet(I+tM)t=0=trM, and derive Jacobi's formula .d(detA)=det(A)tr(A−1dA). Deduce ∇AdetA and .∇AlogdetA.
Let μ1,…,μn be the eigenvalues of ,M, with multiplicity and possibly complex. Then I+tM has eigenvalues .1+tμi.If Mw=μw then ,(I+tM)w=(1+tμ)w, and the characteristic polynomial of I+tM is that of M shifted, so multiplicities carry over.
.det(I+tM)=∏i(1+tμi)=1+t∑iμi+O(t2).The determinant is the product of the eigenvalues; expand and collect the terms with one factor of .t.
,∑iμi=trM, so .dtddet(I+tM)t=0=trM.The trace is the sum of the eigenvalues; the derivative at 0 is the coefficient of .t.
.det(A+tE)=det(A(I+tA−1E))=det(A)det(I+tA−1E).Factor A out on the left, then .det(XY)=detXdetY.
.dtddet(A+tE)t=0=det(A)tr(A−1E).Step 3 with .M=A−1E. Since E is any direction, this is .d(detA)=det(A)tr(A−1dA).
,det(A)tr(A−1dA)=tr((det(A)A−⊤)⊤dA), so .∇AdetA=det(A)A−⊤.Match to :tr(C⊤dA):,C⊤=det(A)A−1, so C=det(A)A−⊤ (Problem 2).
,dlogdetA=detAd(detA)=tr(A−1dA), so .∇AlogdetA=A−⊤.Chain rule through ;log; the detA cancels, which is why logdet is the quantity that gets differentiated in practice.
;dtddet(I+tM)0=trM;;d(detA)=det(A)tr(A−1dA);∇AdetA=det(A)A−⊤ and ∇AlogdetA=A−⊤For symmetric ,Σ,,Σ−⊤=Σ−1, which is the Gaussian page's result. det(A)A−⊤ is the transpose of the adjugate, whose (i,j) entry is the cofactor of :Aij: differentiating the cofactor expansion along row i gives the same thing entry by entry.
Problem 4
Let A(θ) be an invertible matrix depending on a scalar ,θ, with derivative .A′(θ). Show that dθdlogdetA(θ)=tr(A−1A′) and .dθdA(θ)−1=−A−1A′A−1. Evaluate both for A(θ)=B+θI with B symmetric positive definite and eigenvalues ,λi, and .θ>0.
.dA=A′(θ)dθ.A depends on the single variable ,θ, so its differential is its derivative times .dθ.
.dlogdetA=tr(A−1dA)=tr(A−1A′)dθ.Problem 3, step 7, with step 1 substituted; dθ is a scalar and comes out of the trace.
.d(A−1)=−A−1(dA)A−1=−A−1A′A−1dθ.Problem 1 with step 1 substituted.
For :A=B+θI:,A′=I, so dθdlogdetA=tr(A−1) and .dθdA−1=−A−2.,dθd(B+θI)=I, and .A−1IA−1=A−2. Here the factors do commute, because one of them is the identity.
B=QΛQ⊤ gives A=Q(Λ+θI)Q⊤ and .A−1=Q(Λ+θI)−1Q⊤.B is symmetric, so it has an orthonormal eigenbasis; QQ⊤=I lets θI be written as ,Q(θI)Q⊤, and the inverse of QDQ⊤ with Q orthogonal is .QD−1Q⊤.
.tr(A−1)=tr((Λ+θI)−1)=∑iλi+θ1.Cyclic trace: ,tr(QDQ⊤)=tr(DQ⊤Q)=trD, and D is diagonal with entries .1/(λi+θ).
,dθdlogdetA=tr(A−1A′),;dθdA−1=−A−1A′A−1; for :B+θI:∑i1/(λi+θ) and −(B+θI)−2B+θI is the ridge matrix X⊤X+λI with ,θ=λ, and the marginal likelihood of a Gaussian process has exactly this logdet with θ the noise variance (the Gaussian-process page). The sum is positive and decreasing in :θ:logdet grows with the ridge, fastest when some λi is small.
Problem 5
A layer solves x=A−1b (as solve(A, b), without forming ).A−1). With upstream gradient ,g=∇xL, compute ∇bL and ,∇AL, and say what the backward pass costs.
.dx=d(A−1)b+A−1db=−A−1(dA)A−1b+A−1db.Product rule on ,A−1b, then Problem 1 for the first term.
.=−A−1(dA)x+A−1db.,A−1b=x, the forward output, which is saved.
.dL=g⊤dx=−g⊤A−1(dA)x+g⊤A−1db.L depends on A and b only through ,x, and .dL=∑i(∂L/∂xi)dxi=g⊤dx.
Let ,y=A−⊤g, so .g⊤A−1=y⊤..y⊤=(A−⊤g)⊤=g⊤A−1.y is one solve with :A⊤:solve(A.T, g).
,g⊤A−1db=y⊤db, so .∇bL=y=A−⊤g.Matching dL=⟨∇bL,db⟩ with .dA=0.
.−g⊤A−1(dA)x=−y⊤(dA)x=−tr(y⊤dAx)=−tr(xy⊤dA).A scalar is its own trace; the cyclic property moves x to the front.
.∇AL=−(xy⊤)⊤=−yx⊤.Trace trick with .C⊤=−xy⊤.
∇bL=y=A−⊤g and ∇AL=−yx⊤=−(A−⊤g)x⊤∇AL is n×n and rank one: an outer product of the solve with A⊤ and the forward output. The backward pass costs one more solve (or one more triangular pair if the factorisation of A is kept) and an outer product; A−1 is never formed, forward or backward. Check against Problem 2: L=a⊤x has ,g=a, and .−A−⊤a(A−1b)⊤=−A−⊤ab⊤A−⊤.
Problem 6
A layer computes Y=A−1 explicitly. With upstream gradient ,G=∇YL, compute ∇AL in terms of Y and .G.
.dL=⟨G,dY⟩=tr(G⊤dY).L depends on A only through ,Y, and the differential of a scalar function of a matrix is the Frobenius inner product of its gradient with the change.
.dY=−A−1(dA)A−1=−Y(dA)Y.Problem 1, with A−1=Y saved from the forward pass.
.dL=−tr(G⊤YdAY)=−tr(YG⊤YdA).Cyclic: the trailing Y moves to the front.
.∇AL=−(YG⊤Y)⊤=−Y⊤GY⊤.Trace trick with ;C⊤=−YG⊤Y; transposing reverses the order and .(G⊤)⊤=G.
∇AL=−A−⊤GA−⊤=−Y⊤GY⊤,n×n, the shape of .A. For L=⟨G,Y⟩ with ,G=ab⊤,L=a⊤Yb=a⊤A−1b and this gives ,−A−⊤ab⊤A−⊤, Problem 2 again. The two transposes are the whole content; when A is symmetric and G is not, −YGY is still wrong, because G sits between them.
Problem 7
Rank-one update. For invertible A and vectors ,u,v with ,1+v⊤A−1u=0, show the Sherman–Morrison formula (A+uv⊤)−1=A−1−1+v⊤A−1uA−1uv⊤A−1 and the matrix determinant lemma .det(A+uv⊤)=det(A)(1+v⊤A−1u). Then, with 1+v⊤A−1u>0 and ,detA>0, compute .∇ulogdet(A+uv⊤).
Write ,s=1+v⊤A−1u, a scalar.
.(A+uv⊤)(A−1−sA−1uv⊤A−1)=I+uv⊤A−1−suv⊤A−1−su(v⊤A−1u)v⊤A−1.Multiply out the four terms; ,AA−1=I, and in the last term the scalar v⊤A−1u has been pulled out of the middle of .uv⊤A−1uv⊤A−1.
.=I+uv⊤A−1(1−s1−sv⊤A−1u)=I+uv⊤A−1⋅ss−1−v⊤A−1u=I.The last three terms share the factor ;uv⊤A−1; the bracket's numerator is .s−1−(s−1)=0. A right inverse of a square matrix is its inverse.
det(I+wv⊤)=1+v⊤w for any vectors ,w,.v.I+wv⊤ sends w to ,(1+v⊤w)w, so w is an eigenvector with eigenvalue 1+v⊤w (when );w=0); every z with ,v⊤z=0, an (n−1)-dimensional space, is an eigenvector with eigenvalue .1. When v⊤w=0 these n eigenvectors are independent and the determinant is the product of the eigenvalues; when v⊤w=0 both sides equal 1 by continuity in .w.
.det(A+uv⊤)=det(A(I+A−1uv⊤))=det(A)det(I+A−1uv⊤)=det(A)(1+v⊤A−1u).Factor out ,A, split the determinant, and apply step 3 with .w=A−1u.
.logdet(A+uv⊤)=logdetA+log(1+v⊤A−1u).Log of step 4; both factors are positive by assumption.
.∇u(v⊤A−1u)=(v⊤A−1)⊤=A−⊤v.v⊤A−1u=c⊤u with ,c=(v⊤A−1)⊤, and ∇u(c⊤u)=c (the matrix-calculus page).
;(A+uv⊤)−1=A−1−1+v⊤A−1uA−1uv⊤A−1;;det(A+uv⊤)=det(A)(1+v⊤A−1u);∇ulogdet(A+uv⊤)=1+v⊤A−1uA−⊤vThe first two turn an O(n3) inverse and determinant into O(n2) work once A−1 (or a factorisation) is known: adding one example to a kernel matrix, one observation to a Kalman covariance, or a rank-one step in a quasi-Newton method. The gradient agrees with Problem 3 through the chain rule: ∇Alogdet at A+uv⊤ is ,(A+uv⊤)−⊤, and u enters through .uv⊤.
Problem 8
Let Σ=LL⊤ with L a Cholesky factor. Show that ,logdetΣ=2∑ilogLii, and compute the gradient of logdetΣ with respect to the free entries of ,L, once directly and once through .∇ΣlogdetΣ=Σ−1.
.detΣ=detLdetL⊤=(detL)2=∏iLii2.,det(XY)=detXdetY,,detL⊤=detL, and the determinant of a triangular matrix is the product of its diagonal.
.logdetΣ=2∑ilogLii.Log of step 1; each Lii>0 so the logs are defined. This is how a library computes logdet of a covariance: no determinant is ever formed.
Directly: .∂Lij∂2∑klogLkk={2/Lii0i=ji>j.Only the diagonal entries appear in the formula, so the below-diagonal entries have zero derivative, and .dxdlogx=1/x.
Through :Σ:.dΣ=(dL)L⊤+L(dL)⊤.Product rule on ,LL⊤, in order.
.dlogdetΣ=tr(Σ−1dΣ)=tr(Σ−1(dL)L⊤)+tr(Σ−1L(dL)⊤).Problem 3, step 7, with dΣ from step 4 and the trace split over the sum.
tr(Σ−1(dL)L⊤)=tr(L⊤Σ−1dL) and .tr(Σ−1L(dL)⊤)=tr(dLL⊤Σ−1)=tr(L⊤Σ−1dL).Cyclic trace for the first; for the second, transpose inside the trace (which leaves it unchanged, using )Σ−⊤=Σ−1) and then cycle. The two terms are equal.
,dlogdetΣ=2tr((Σ−1L)⊤dL), so over all entries the gradient is .2Σ−1L=2L−⊤L−1L=2L−⊤.Trace trick with ,C⊤=2L⊤Σ−1, so ;C=2Σ−1L; then .Σ−1=(LL⊤)−1=L−⊤L−1.
L−⊤ is upper triangular with diagonal ,1/Lii, so .tril(2L−⊤)=diag(2/L11,…,2/Lnn).The inverse of a lower-triangular matrix is lower triangular with reciprocal diagonal, and its transpose is upper triangular. Only the entries where L has free parameters count: dL is zero above the diagonal, so those entries of C multiply nothing.
;logdetΣ=2∑ilogLii; the gradient over the free entries of L is 2/Lii on the diagonal and 0 below it, which is tril(2Σ−1L)=tril(2L−⊤)Both routes agree, and the second is the one a framework's autodiff takes: it computes the full 2Σ−1L and the restriction to the parametrised entries discards the upper triangle. A gradient step on the off-diagonal entries of L that came from logdet alone would be a bug; those entries only move through the other terms of a likelihood (Problem 9).
Problem 9
The Gaussian log-likelihood of Before you start, ,ℓ(Σ)=−2NlogdetΣ−21tr(Σ−1S)+const, is optimised through .Σ=LL⊤. Starting from ,∇Σℓ, compute the gradient with respect to the free entries of ,L, simplify it using ,M=L−1SL−⊤, and show that it vanishes at .Σ=S/N.
Write ,C=∇Σℓ=−2NΣ−1+21Σ−1SΣ−1, which is symmetric because Σ and S are.
.dℓ=⟨C,dΣ⟩=⟨C,(dL)L⊤⟩+⟨C,L(dL)⊤⟩.ℓ depends on L only through ,Σ, with dΣ from Problem 8, step 4, and the inner product is linear.
.⟨C,(dL)L⊤⟩=tr(C⊤(dL)L⊤)=tr(L⊤C⊤dL)=⟨CL,dL⟩.Cyclic trace, then .L⊤C⊤=(CL)⊤.
.⟨C,L(dL)⊤⟩=tr(C⊤L(dL)⊤)=tr(dLL⊤C)=tr(L⊤CdL)=⟨C⊤L,dL⟩.Transpose inside the trace, then cycle; .L⊤C=(C⊤L)⊤.
,dℓ=⟨(C+C⊤)L,dL⟩=⟨2CL,dL⟩, so over the free entries .∇Lℓ=tril(2CL).Trace trick; C is symmetric; and as in Problem 8 only the lower triangle is parametrised.
.2CL=−NΣ−1L+Σ−1SΣ−1L=−NL−⊤+L−⊤L−1SL−⊤.Σ−1=L−⊤L−1 and ,L−1L=I, applied to each term: ,Σ−1L=L−⊤, and .Σ−1SΣ−1L=L−⊤L−1SL−⊤.
2CL=L−⊤(M−NI) with .M=L−1SL−⊤.Factor L−⊤ out on the left of both terms.
At :Σ=S/N:,LL⊤=S/N, so M=L−1(NLL⊤)L−⊤=NI and .2CL=0.L−1L=I and .L⊤L−⊤=I. Then the lower triangle of 0 is .0.
∇Lℓ=tril(2∇ΣℓL)=tril(L−⊤(M−NI)) with ;M=L−1SL−⊤; it is 0 at Σ=S/NM is the scatter matrix of the whitened data ,L−1(xn−μ), and the gradient says: move L until the whitened scatter is N times the identity, that is, until Σ explains the data's spread exactly. The maximum-likelihood covariance from the Gaussian page is unchanged by the reparametrisation, as it must be; what changes is the gradient the optimiser sees, which here needs two triangular solves and no inverse.
Problem 10
Let A be symmetric with distinct eigenvalues λi and orthonormal eigenvectors ,qi, and let E be symmetric. Show that along A+tE each eigenvalue has derivative dtdλi=qi⊤Eqi at ,t=0, and use it to recover dtdlogdet(A+tE)=tr(A−1E) for positive definite .A.
Take as known that for distinct eigenvalues, λi(t) and a unit eigenvector qi(t) of A+tE can be chosen to vary differentiably in .t.
.(A+tE)qi(t)=λi(t)qi(t).The eigenvector equation at every .t.
Eqi+Aqi′=λi′qi+λiqi′ at .t=0.Differentiate both sides with respect to t by the product rule; .dtd(A+tE)=E. Primes are t-derivatives at .0.
.qi⊤Eqi+qi⊤Aqi′=λi′+λiqi⊤qi′.Multiply on the left by ;qi⊤;.qi⊤qi=1.
.qi⊤Aqi′=(Aqi)⊤qi′=λiqi⊤qi′.A is symmetric, so ,qi⊤A=(Aqi)⊤, and .Aqi=λiqi.
.λi′=qi⊤Eqi.The two qi′ terms in step 3 are equal by step 4 and cancel. The eigenvector's derivative never needed to be found.
.dtdlogdet(A+tE)=∑iλiλi′=∑iλiqi⊤Eqi.logdet=∑ilogλi for positive eigenvalues, and .dtdlogλi=λi′/λi.
.∑iλiqi⊤Eqi=∑itr(λiqiqi⊤E)=tr((∑iλiqiqi⊤)E)=tr(A−1E).qi⊤Eqi=tr(qiqi⊤E) by the cyclic property; the trace is linear; and ∑iqiqi⊤/λi=QΛ−1Q⊤=A−1 is the eigendecomposition of the inverse.
,dtdλi=qi⊤Eqi, and dtdlogdet(A+tE)=∑iqi⊤Eqi/λi=tr(A−1E)Jacobi's formula from Problem 3, reached through the spectrum. The per-eigenvalue form says what the trace hides: a perturbation moves logdet most through the smallest eigenvalues, with weight 1/λi along ,qi, which is why a nearly singular covariance makes its logdet gradient large along the nearly flat direction.
Where this goes wrong
1. Differentiating the inverse like a reciprocal
The scalar rule d(1/a)=−da/a2 is well worn, and A−1 looks like .1/A.
A−1A=IRight so far: Problem 1, step 1.
“A−1 is ,1/A, so its derivative is −1/A2 times .dA.”The analogy that causes the mistake: in the scalar rule the two factors of 1/a sit together because numbers commute; matrices do not.
d(A−1)=−A−2dAProblem 1 gives ,−A−1(dA)A−1, with dA between the two inverses. The two agree only when dA commutes with ,A, for instance A=B+θI differentiated in θ (Problem 4), which is exactly the case in which the habit is formed. In Problem 5 the wrong form gives ∇AL=−A−2⊤gb⊤ in place of :−A−⊤gx⊤: the right shape, and a gradient check fails on every entry.
2. Trace trick without the transpose
Once df is inside a trace, the matrix next to dX looks like the gradient.
df=−tr(X−1ba⊤X−1dX) for f=a⊤X−1bRight so far: Problem 2, step 5.
“The gradient is whatever multiplies dX inside the trace.”The shortcut that causes the mistake: the trace trick reads ,tr(C⊤dX), so what multiplies dX is ,C⊤, not .C.
∇X(a⊤X−1b)=−X−1ba⊤X−1The gradient is the transpose, −X−⊤ab⊤X−⊤ (Problem 2). Both are ,n×n, so no shape check objects, and for symmetric X with a=b they coincide, which is the case the Gaussian page uses. Test the general case on one entry: with ,X=I,,∂f/∂Xij=−aibj, the (i,j) entry of ,−ab⊤, not of .−ba⊤.
3. The gradient of det taken to be the gradient of log det
A−⊤ is the answer everyone remembers, and the log is easy to drop or to add.
d(detA)=det(A)tr(A−1dA)Right so far: Jacobi's formula, Problem 3.
“So the gradient of the determinant is the inverse transpose.”The slip that causes the mistake: the factor detA in front of the trace is treated as part of the bookkeeping, when it is the difference between det and .logdet.
∇AdetA=A−⊤That is ;∇AlogdetA; the determinant's gradient is det(A)A−⊤ (Problem 3). With A=2I in n=4 dimensions, detA=16 and ∂detA/∂A11=8 (the cofactor), while A−⊤ has entry 21 there. The error is a factor of detA on every entry, which in a likelihood is the difference between an update that scales with the volume of the covariance and one that does not.
4. Cholesky gradient left above the diagonal
Autodiff produces 2Σ−1L for the full matrix, and it is tempting to use all of it.
dlogdetΣ=⟨2Σ−1L,dL⟩ over all n2 entries of LRight so far: Problem 8, step 7.
“L is a matrix parameter, so update every entry of L with this gradient.”The shortcut that causes the mistake: 2Σ−1L=2L−⊤ is upper triangular, and its nonzero off-diagonal entries sit exactly where L has no parameters.
,L←L−η⋅2L−⊤, so the entries above the diagonal of L become nonzeroL is no longer lower triangular, logdet(LL⊤)=2∑ilogLii no longer holds, and the next Cholesky-based solve is wrong. The gradient over the free entries is ,tril(2L−⊤), which is only the diagonal (Problem 8); a parametrisation that stores the lower triangle as a vector, or masks the gradient with ,tril, never sees the problem.
5. Chain rule through LLᵀ counting L once
Σ=LL⊤ is a product, but it is a product of a matrix with itself.
,∇Σℓ=C, symmetricRight so far: the Gaussian page.
“Σ=LL⊤ is linear in L with L⊤ fixed, so by the chain rule .∇Lℓ=CL.”The shortcut that causes the mistake: dΣ=(dL)L⊤+L(dL)⊤ has two terms, and the second is dropped because L⊤ is treated as a different variable.
∇Lℓ=tril(CL)The two terms are equal (Problem 9, steps 2 and 3), so the gradient is :tril(2CL): the error is a factor of 2 on every entry. The stationary point is unchanged, which is why training can still reach the maximum-likelihood covariance, only with half the step the learning rate promises; the same slip in logdetΣ alone gives 1/Lii in place of 2/Lii (Problem 8).