For L(W)=12∥Y−XW∥F2+λ2tr⁡(W⊤AW), where X∈Rn×d, W∈Rd×k, Y∈Rn×k and A∈Rd×d is symmetric, how do you derive ∇WL(W) using differentials and verify every matrix product by its shapes?

Linear algebra and optimisation, Matrix calculus
Level L4 Advanced. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can differentiate a matrix-valued objective cleanly using differentials and trace identities without transposing factors in the wrong order.

Key idea

Express both terms using traces, take the differential dL=tr⁡(G⊤dW) via the product and cyclic-trace rules, and match G=∇WL(W), whose dimensions must match W (d×k).

Steps

  1. Rewrite the Frobenius norm as a trace: ∥E∥F2=tr⁡(E⊤E) where E=XW−Y∈Rn×k, so L(W)=12tr⁡(E⊤E)+λ2tr⁡(W⊤AW).
  2. Differentiate the first term using dE=X dW: d(12tr⁡(E⊤E))=12tr⁡((dE)⊤E+E⊤dE)=tr⁡(E⊤dE)=tr⁡(E⊤X dW)=tr⁡((X⊤E)⊤dW).
  3. Differentiate the regulariser using symmetry A⊤=A and tr⁡(M⊤)=tr⁡(M): d(λ2tr⁡(W⊤AW))=λ2tr⁡((dW)⊤AW+W⊤A dW)=λtr⁡((AW)⊤dW).
  4. Read off the gradient from dL=tr⁡((∇WL)⊤dW): ∇WL(W)=X⊤(XW−Y)+λAW.
  5. Check shapes: X⊤ is (d×n) and (XW−Y) is (n×k), so their product is (d×k); A is (d×d) and W is (d×k), so AW is (d×k), matching W∈Rd×k.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Last checked on 4 December 2026.

Read this

Previous in Linear algebra and optimisation: If a data matrix X∈Rn×d has singular values σ1≥⋯≥σd≥0, how does adding λI (λ>0) change the eigenvalues and condition number of X⊤X, and why does this explain both numerical stability and Tikhonov regularisation?
Next in Linear algebra and optimisation: How do quasi-Newton methods (BFGS and L-BFGS) approximate the Hessian without computing second derivatives, what are the secant equation and the curvature condition sk⊤yk>0, and how does L-BFGS reduce memory from O(d2) to O(md)?