unitgridOpacitycanvasColororiginXoriginY

Nearest orthogonal matrix and Muon optimizer

1. Finding the minimizer

We want to find an orthogonal matrix OO that is closest to a matrix AA. If A=UΣVTA = U\Sigma V^T, then:

O∗=UVTO^* = UV^T
Proof that O* = UVᵀ∥A−O∥F2=tr((A−O)T(A−O))=tr(ATA)⏟∥A∥F2, no O− 2 tr(OTA) +tr(OTO)⏟tr(I) = n\begin{align} \|A - \textcolor{#1d4ed8}{O}\|_F^2 &= \text{tr}\big((A - \textcolor{#1d4ed8}{O})^T (A - \textcolor{#1d4ed8}{O})\big) \\ &= \underbrace{\text{tr}(A^TA)}_{\|A\|_F^2, \text{ no } \textcolor{#1d4ed8}{O}} - \ 2\,\text{tr}(\textcolor{#1d4ed8}{O}^TA) \ + \underbrace{\text{tr}(\textcolor{#1d4ed8}{O}^T\textcolor{#1d4ed8}{O})}_{\text{tr}(I) \, = \, n} \end{align} min⁡O∥A−O∥F  ⟺  max⁡Otr(OTA)\min_{\textcolor{#1d4ed8}{O}} \|A - \textcolor{#1d4ed8}{O}\|_F \iff \max_{\textcolor{#1d4ed8}{O}} \text{tr}(\textcolor{#1d4ed8}{O}^TA) tr(OTA)=tr(OTUΣVT⏟A)=tr(VTOTU⏟Z, orthogonal Σ)\text{tr}(\textcolor{#1d4ed8}{O}^TA) = \text{tr}(\textcolor{#1d4ed8}{O}^T \underbrace{U \Sigma V^T}_{A}) = \text{tr}(\underbrace{\textcolor{#be185d}{V^T \textcolor{#1d4ed8}{O}^T U}}_{\textcolor{#be185d}{Z}, \text{ orthogonal}} \ \Sigma) tr(ZΣ)=∑izii⏟≤1 σi⏟≥0  is largest when  Z=I  ⟺  VTOTU=I  ⟺  O∗=UVT\text{tr}(\textcolor{#be185d}{Z}\Sigma) = \sum_i \underbrace{\textcolor{#be185d}{z_{ii}}}_{\le 1} \ \underbrace{\sigma_i}_{\ge 0} \ \text{ is largest when } \ \textcolor{#be185d}{Z} = I \iff \textcolor{#be185d}{V^T \textcolor{#1d4ed8}{O}^T U} = I \iff \boxed{\textcolor{#1d4ed8}{O}^* = UV^T}

The difference is:

∥A−UVT∥F=∑i(σi−1)2\|A - UV^T\|_F = \sqrt{\sum_i (\sigma_i - 1)^2}

2. How AA and OO transform a circle

You can change the value of AA in the code below to whatever you want.

import numpy as np

# Change A, run this, then run the editor below
A = np.array([[1.6, 0.6],
              [0.4, 0.8]])

U, _, Vt = np.linalg.svd(A)
O = U @ Vt

pie.clear()
pie.scalar(90, out='unit')
DARK = '#3f3f46'

def shown_matrix(w_a, w_o, A, O):
    # The radio buttons move these weights between 0 and 1
    return w_a * A + w_o * O

w = pie.scalar([1, 0], out='w')
M = pie.lift(shown_matrix, pie.Array[pie.Scalar])(w[0], w[1], A, O)

# Drawing
origin = pie.point(0, 0, hidden=True)
pie.circle(origin, 1, stroke='rgba(63, 63, 70, 0.35)')
pie.la_circle_transform(M, 24)
pie.la_mat_to_ellipse(M, origin, stroke=DARK)

pie.ui.animated_radio(w, options=['A', 'O = UVᵀ'], label='show')
def bmatrix(M):
    rows = [' & '.join(f'{x:.2f}' for x in row) for row in M]
    return r'\begin{bmatrix}' + r' \\ '.join(rows) + r'\end{bmatrix}'

pie.ui.latex(pie.text(f'A = {bmatrix(A)}, \\quad O = UV^T = {bmatrix(O)}'))

3. Muon optimizer

Muon replaces the gradient of a matrix G=UΣVTG = U \Sigma V^T with its nearest orthogonal matrix UVTUV^T:

W←W−η UVT,G=UΣVTW \leftarrow W - \eta \, UV^T, \qquad G = U\Sigma V^T

However it only approximates UVTUV^T instead of calculating the exact value using SVD to save time.


4. Connection with Orthogonal Procrustes problem

The Procrustes problem relates to finding an orthogonal matrix OO so that OXOX gets as close as possible to YY (under Frobenius norm).

O∗=arg⁡min⁡OTO=I∥OX−Y∥FO^* = \arg\min_{\textcolor{#1d4ed8}{O}^T\textcolor{#1d4ed8}{O} = I} \|\textcolor{#1d4ed8}{O}X - Y\|_F

If YXT=UΣVTYX^T = U\Sigma V^T then

O∗=UVTO^* = UV^T
Proof that O* = UVᵀ∥OX−Y∥F2=tr((OX−Y)T(OX−Y))=tr(XTOTOX)⏟∥X∥F2, no O− 2 tr(YTOX) +tr(YTY)⏟∥Y∥F2, no O\begin{align} \|\textcolor{#1d4ed8}{O}X - Y\|_F^2 &= \text{tr}\big((\textcolor{#1d4ed8}{O}X - Y)^T (\textcolor{#1d4ed8}{O}X - Y)\big) \\ &= \underbrace{\text{tr}(X^T\textcolor{#1d4ed8}{O}^T\textcolor{#1d4ed8}{O}X)}_{\|X\|_F^2, \text{ no } \textcolor{#1d4ed8}{O}} - \ 2\,\text{tr}(Y^T\textcolor{#1d4ed8}{O}X) \ + \underbrace{\text{tr}(Y^TY)}_{\|Y\|_F^2, \text{ no } \textcolor{#1d4ed8}{O}} \end{align} min⁡O∥OX−Y∥F  ⟺  max⁡Otr(YTOX)\min_{\textcolor{#1d4ed8}{O}} \|\textcolor{#1d4ed8}{O}X - Y\|_F \iff \max_{\textcolor{#1d4ed8}{O}} \text{tr}(Y^T\textcolor{#1d4ed8}{O}X) tr(YTOX)=tr(OTYXT⏟UΣVT)=tr(VTOTU⏟Z, orthogonal Σ)\text{tr}(Y^T\textcolor{#1d4ed8}{O}X) = \text{tr}(\textcolor{#1d4ed8}{O}^T \underbrace{YX^T}_{U \Sigma V^T}) = \text{tr}(\underbrace{\textcolor{#be185d}{V^T \textcolor{#1d4ed8}{O}^T U}}_{\textcolor{#be185d}{Z}, \text{ orthogonal}} \ \Sigma) tr(ZΣ)=∑izii⏟≤1 σi⏟≥0  is largest when  Z=I  ⟺  VTOTU=I  ⟺  O∗=UVT\text{tr}(\textcolor{#be185d}{Z}\Sigma) = \sum_i \underbrace{\textcolor{#be185d}{z_{ii}}}_{\le 1} \ \underbrace{\sigma_i}_{\ge 0} \ \text{ is largest when } \ \textcolor{#be185d}{Z} = I \iff \textcolor{#be185d}{V^T \textcolor{#1d4ed8}{O}^T U} = I \iff \boxed{\textcolor{#1d4ed8}{O}^* = UV^T}

The nearest orthogonal matrix to AA is the Procrustes problem with X=IX = I and Y=AY = A:

∥O I−A∥F=∥A−O∥F,YXT=A IT=A\|\textcolor{#1d4ed8}{O}\,I - A\|_F = \|A - \textcolor{#1d4ed8}{O}\|_F, \qquad YX^T = A \, I^T = A