Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

InfoNCE linear models, gradients, the matrix XXX and differentiable SVDs

Definition
DimensionalCollapse_Basic

by Xeroth · Nov 24, 2026 · Mathlib 0df444a (Lean v4.33.1)

contrastive-learninggradient-flowinfonceself-supervised-learningsingular-values

Objects of Sections 3–5 and Appendices A–B of Jing, Vincent, LeCun and Tian (ICLR 2022).

  1. Data and embeddings. There are NNN pairs of inputs (xi,xi′)(x_i,x_i')(xi​,xi′​), xi,xi′∈Rnx_i,x_i'\in\mathbb{R}^nxi​,xi′​∈Rn, where xi′x_i'xi′​ is an augmented view of xix_ixi​. A linear model W∈Rd×nW\in\mathbb{R}^{d\times n}W∈Rd×n gives embeddings zi=Wxiz_i=Wx_izi​=Wxi​, zi′=Wxi′z_i'=Wx_i'zi′​=Wxi′​. The two-layer model has W1,W2∈Rd×dW_1,W_2\in\mathbb{R}^{d\times d}W1​,W2​∈Rd×d (inputs in Rd\mathbb{R}^dRd) and z=W2W1xz=W_2W_1xz=W2​W1​x. We write ∣v∣2=∑cvc2|v|^2=\sum_c v_c^2∣v∣2=∑c​vc2​.
  2. InfoNCE (eq. 2). With Zi=∑j≠ie−∣zi−zj∣2/2+e−∣zi−zi′∣2/2Z_i=\sum_{j\neq i}e^{-|z_i-z_j|^2/2}+e^{-|z_i-z_i'|^2/2}Zi​=∑j=i​e−∣zi​−zj​∣2/2+e−∣zi​−zi′​∣2/2,
L=−∑i=1Nlog⁡e−∣zi−zi′∣2/2Zi.L=-\sum_{i=1}^N\log\frac{e^{-|z_i-z_i'|^2/2}}{Z_i}.L=−i=1∑N​logZi​e−∣zi​−zi′​∣2/2​.

The softmax weights are αij=e−∣zi−zj∣2/2/Zi\alpha_{ij}=e^{-|z_i-z_j|^2/2}/Z_iαij​=e−∣zi​−zj​∣2/2/Zi​ for j≠ij\neq ij=i and αii=e−∣zi−zi′∣2/2/Zi\alpha_{ii}=e^{-|z_i-z_i'|^2/2}/Z_iαii​=e−∣zi​−zi′​∣2/2/Zi​, so ∑jαij=1\sum_j\alpha_{ij}=1∑j​αij​=1. 3. Gradients (Lemma 1, eq. 3). gzig_{z_i}gzi​​ and gzi′g_{z_i'}gzi′​​ are the gradients of LLL with respect to ziz_izi​ and zi′z_i'zi′​, and G=∑i(gzixi⊤+gzi′xi′⊤)∈Rd×nG=\sum_i(g_{z_i}x_i^\top+g_{z_i'}x_i'^\top)\in\mathbb{R}^{d\times n}G=∑i​(gzi​​xi⊤​+gzi′​​xi′⊤​)∈Rd×n, evaluated at z=Wxz=Wxz=Wx. The gradient ∂F/∂W\partial F/\partial W∂F/∂W of a function of a matrix is the matrix of its partial derivatives ∂F/∂Wab\partial F/\partial W_{ab}∂F/∂Wab​. 4. The matrix XXX (eq. 6, in the form of Appendix B.2).

X=∑i(∑j≠iαij(xi′−xj)+∑j≠iαji(xi−xj))xi⊤−∑i(1−αii)(xi′−xi)xi′⊤,X=\sum_i\Big(\sum_{j\neq i}\alpha_{ij}(x_i'-x_j)+\sum_{j\neq i}\alpha_{ji}(x_i-x_j)\Big)x_i^\top-\sum_i(1-\alpha_{ii})(x_i'-x_i)x_i'^\top,X=i∑​(j=i∑​αij​(xi′​−xj​)+j=i∑​αji​(xi​−xj​))xi⊤​−i∑​(1−αii​)(xi′​−xi​)xi′⊤​,

with α\alphaα computed from z=Wxz=Wxz=Wx, z′=Wx′z'=Wx'z′=Wx′. The weighted covariances of Lemma 2 are Σ^0=∑i,jαij(xi−xj)(xi−xj)⊤\hat\Sigma_0=\sum_{i,j}\alpha_{ij}(x_i-x_j)(x_i-x_j)^\topΣ^0​=∑i,j​αij​(xi​−xj​)(xi​−xj​)⊤ and Σ^1=∑i(1−αii)(xi′−xi)(xi′−xi)⊤\hat\Sigma_1=\sum_i(1-\alpha_{ii})(x_i'-x_i)(x_i'-x_i)^\topΣ^1​=∑i​(1−αii​)(xi′​−xi​)(xi′​−xi​)⊤. 5. Gradient flows. The linear loss is L(W)L(W)L(W) with z=Wxz=Wxz=Wx; the two-layer loss is L(W1,W2)L(W_1,W_2)L(W1​,W2​) with z=W2W1xz=W_2W_1xz=W2​W1​x. The two-layer weights follow the gradient flow at time ttt if W˙1(t)=−∂L/∂W1\dot W_1(t)=-\partial L/\partial W_1W˙1​(t)=−∂L/∂W1​ and W˙2(t)=−∂L/∂W2\dot W_2(t)=-\partial L/\partial W_2W˙2​(t)=−∂L/∂W2​ at (W1(t),W2(t))(W_1(t),W_2(t))(W1​(t),W2​(t)). 6. Differentiable SVD (Section 5.3, Appendix A). W(s)=U(s) diag(σ(s)) V(s)⊤W(s)=U(s)\,\mathrm{diag}(\sigma(s))\,V(s)^\topW(s)=U(s)diag(σ(s))V(s)⊤ with U(s),V(s)U(s),V(s)U(s),V(s) orthogonal and σ(s)≥0\sigma(s)\ge0σ(s)≥0 for all sss near ttt, and U,V,σU,V,\sigmaU,V,σ differentiable at ttt. The columns uk,vku^k,v^kuk,vk of U,VU,VU,V are the singular vectors of σk\sigma^kσk. 7. Spectra. σmin⁡(A)\sigma_{\min}(A)σmin​(A) is the smallest of the nnn singular values of A∈Rm×nA\in\mathbb{R}^{m\times n}A∈Rm×n (zero when m<nm<nm<n). The embedding covariance (eq. 1) is C=1N∑i(zi−zˉ)(zi−zˉ)⊤C=\frac1N\sum_i(z_i-\bar z)(z_i-\bar z)^\topC=N1​∑i​(zi​−zˉ)(zi​−zˉ)⊤ with zˉ=1N∑izi\bar z=\frac1N\sum_i z_izˉ=N1​∑i​zi​, zi=Wxiz_i=Wx_izi​=Wxi​.

Formalization Note The gradients are defined as vectors and matrices of partial derivatives (deriv along coordinate directions), which are the true gradients because the loss is smooth. The matrix XXX uses the sign convention of Appendix B.2: eq. (6) as printed on p. 4 has an extra minus sign on its first sum, and with that sign neither eq. (5) nor Lemma 2 holds. Singular values are Mathlib's LinearMap.singularValues, sorted in decreasing order.

Definition code
import Mathlib

/-!
Jing, Vincent, LeCun and Tian, *Understanding Dimensional Collapse in Contrastive Self-supervised
Learning*, ICLR 2022, arXiv:2110.09348v3: the InfoNCE loss of eq. (2), the linear and two-layer
linear models of Sections 4.1 and 5.1, the gradients and the matrix `X` of eqs. (3)–(9)
(Sections 4.2 and 5.2, Appendix B.2), the embedding covariance of eq. (1), and the differentiable
singular value decompositions of Section 5.3 and Appendix A.
-/

open Matrix Filter Topology

noncomputable section

namespace DimensionalCollapse

/-- Squared Euclidean norm `|v|² = ∑_c v_c²` of a vector `v ∈ ℝ^d`. -/
def sqNorm {d : ℕ} (v : Fin d → ℝ) : ℝ := ∑ c, v c ^ 2

/-- Section 4.2, below eq. (4): `Z_i = ∑_{j ≠ i} exp(-|z_i - z_j|²/2) + exp(-|z_i - z_i'|²/2)`,
for embeddings `z_i, z_i'` (`i = 1, …, N`) of the two branches. -/
def partitionZ {N d : ℕ} (z z' : Fin N → Fin d → ℝ) (i : Fin N) : ℝ :=
  (∑ j ∈ Finset.univ.erase i, Real.exp (-sqNorm (z i - z j) / 2)) +
    Real.exp (-sqNorm (z i - z' i) / 2)

/-- Eq. (2), Section 4.1: the InfoNCE loss
`L = -∑_i log (exp(-|z_i - z_i'|²/2) / Z_i)`, whose negatives for `z_i` are the `z_j`, `j ≠ i`. -/
def infoNCE {N d : ℕ} (z z' : Fin N → Fin d → ℝ) : ℝ :=
  -∑ i, Real.log (Real.exp (-sqNorm (z i - z' i) / 2) / partitionZ z z' i)

/-- Section 4.2, below eq. (4): the softmax weights `α_ij = exp(-|z_i - z_j|²/2) / Z_i` for
`j ≠ i` and `α_ii = exp(-|z_i - z_i'|²/2) / Z_i`, so that `∑_j α_ij = 1`. -/
def alpha {N d : ℕ} (z z' : Fin N → Fin d → ℝ) (i j : Fin N) : ℝ :=
  (if j = i then Real.exp (-sqNorm (z i - z' i) / 2) else Real.exp (-sqNorm (z i - z j) / 2)) /
    partitionZ z z' i

/-- Sections 4.1 and 5.1: the embeddings `z_i = W x_i` of inputs `x_i ∈ ℝ^n` under a linear
map `W ∈ ℝ^{d×n}` (for the two-layer model, `W = W₂ W₁`). -/
def embed {N n d : ℕ} (W : Matrix (Fin d) (Fin n) ℝ) (x : Fin N → Fin n → ℝ) :
    Fin N → Fin d → ℝ :=
  fun i => W *ᵥ x i

/-- Lemma 1, Section 4.2: `g_{z_i}`, the gradient of the InfoNCE loss with respect to the
embedding vector `z_i` (its vector of partial derivatives; the loss is smooth). -/
def gradZ {N d : ℕ} (z z' : Fin N → Fin d → ℝ) (i : Fin N) : Fin d → ℝ :=
  fun c => deriv (fun s : ℝ => infoNCE (Function.update z i (z i + s • Pi.single c 1)) z') 0

/-- Lemma 1, Section 4.2: `g_{z_i'}`, the gradient of the InfoNCE loss with respect to the
embedding vector `z_i'` (its vector of partial derivatives). -/
def gradZ' {N d : ℕ} (z z' : Fin N → Fin d → ℝ) (i : Fin N) : Fin d → ℝ :=
  fun c => deriv (fun s : ℝ => infoNCE z (Function.update z' i (z' i + s • Pi.single c 1))) 0

/-- Lemma 1, eq. (3): `G = ∑_i (g_{z_i} x_iᵀ + g_{z_i'} x_i'ᵀ)`, with the gradients evaluated at
the embeddings `z_i = W x_i`, `z_i' = W x_i'`. -/
def Gmat {N n d : ℕ} (x x' : Fin N → Fin n → ℝ) (W : Matrix (Fin d) (Fin n) ℝ) :
    Matrix (Fin d) (Fin n) ℝ :=
  Matrix.of fun a b => ∑ i, (gradZ (embed W x) (embed W x') i a * x i b +
    gradZ' (embed W x) (embed W x') i a * x' i b)

/-- Eq. (6) in the form used in Appendix B.2 (the form for which eq. (5), `G = -W X`, holds):
`X = ∑_i (∑_{j≠i} α_ij (x_i' - x_j) + ∑_{j≠i} α_ji (x_i - x_j)) x_iᵀ
  - ∑_i (1 - α_ii)(x_i' - x_i) x_i'ᵀ`, with `α` computed from the embeddings `W x_i`, `W x_i'`. -/
def Xmat {N n d : ℕ} (x x' : Fin N → Fin n → ℝ) (W : Matrix (Fin d) (Fin n) ℝ) :
    Matrix (Fin n) (Fin n) ℝ :=
  Matrix.of fun a b =>
    (∑ i, ((∑ j ∈ Finset.univ.erase i, alpha (embed W x) (embed W x') i j * (x' i a - x j a)) +
        (∑ j ∈ Finset.univ.erase i, alpha (embed W x) (embed W x') j i * (x i a - x j a))) * x i b) -
      ∑ i, (1 - alpha (embed W x) (embed W x') i i) * (x' i a - x i a) * x' i b

/-- Lemma 2, eq. (7): the weighted data covariance `Σ̂₀ = ∑_{i,j} α_ij (x_i - x_j)(x_i - x_j)ᵀ`. -/
def Sigma0 {N n d : ℕ} (x x' : Fin N → Fin n → ℝ) (W : Matrix (Fin d) (Fin n) ℝ) :
    Matrix (Fin n) (Fin n) ℝ :=
  Matrix.of fun a b =>
    ∑ i, ∑ j, alpha (embed W x) (embed W x') i j * (x i a - x j a) * (x i b - x j b)

/-- Lemma 2, eq. (7): the weighted augmentation covariance
`Σ̂₁ = ∑_i (1 - α_ii)(x_i' - x_i)(x_i' - x_i)ᵀ`. -/
def Sigma1 {N n d : ℕ} (x x' : Fin N → Fin n → ℝ) (W : Matrix (Fin d) (Fin n) ℝ) :
    Matrix (Fin n) (Fin n) ℝ :=
  Matrix.of fun a b =>
    ∑ i, (1 - alpha (embed W x) (embed W x') i i) * (x' i a - x i a) * (x' i b - x i b)

/-- The gradient `∂F/∂W` of a function of a matrix, as the matrix of its partial derivatives
`∂F/∂W_ab` (used for the gradient flows of Lemmas 1 and 3). -/
def matGrad {m n : ℕ} (F : Matrix (Fin m) (Fin n) ℝ → ℝ) (W : Matrix (Fin m) (Fin n) ℝ) :
    Matrix (Fin m) (Fin n) ℝ :=
  Matrix.of fun a b => deriv (fun s : ℝ => F (W + s • Matrix.single a b 1)) 0

/-- Section 4.1: the InfoNCE loss of the linear model `z = W x` on the pairs `(x_i, x_i')`. -/
def linLoss {N n d : ℕ} (x x' : Fin N → Fin n → ℝ) (W : Matrix (Fin d) (Fin n) ℝ) : ℝ :=
  infoNCE (embed W x) (embed W x')

/-- Section 5.1: the InfoNCE loss of the two-layer linear model `z = W₂ W₁ x`,
`W₁, W₂ ∈ ℝ^{d×d}`. -/
def twoLayerLoss {N d : ℕ} (x x' : Fin N → Fin d → ℝ) (W1 W2 : Matrix (Fin d) (Fin d) ℝ) : ℝ :=
  infoNCE (embed (W2 * W1) x) (embed (W2 * W1) x')

/-- Sections 5.1–5.2: at time `t`, the weights `W₁(·), W₂(·)` follow the gradient flow of the
two-layer InfoNCE loss, `Ẇ₁ = -∂L/∂W₁`, `Ẇ₂ = -∂L/∂W₂` (entrywise derivatives). -/
def IsTwoLayerFlowAt {N d : ℕ} (x x' : Fin N → Fin d → ℝ)
    (W1 W2 : ℝ → Matrix (Fin d) (Fin d) ℝ) (t : ℝ) : Prop :=
  (∀ a b, HasDerivAt (fun s => W1 s a b)
      (-(matGrad (fun M => twoLayerLoss x x' M (W2 t)) (W1 t)) a b) t) ∧
    (∀ a b, HasDerivAt (fun s => W2 s a b)
      (-(matGrad (fun M => twoLayerLoss x x' (W1 t) M) (W2 t)) a b) t)

/-- Section 5.3 and Appendix A (Lemma 4): `W(s) = U(s) diag(σ(s)) V(s)ᵀ` is a singular value
decomposition (`U, V` orthogonal, `σ ≥ 0`) for all `s` near `t`, and `U`, `V`, `σ` are
differentiable at `t`. -/
def IsDiffSVDAt {d : ℕ} (W U V : ℝ → Matrix (Fin d) (Fin d) ℝ) (sig : ℝ → Fin d → ℝ)
    (t : ℝ) : Prop :=
  (∀ᶠ s in 𝓝 t, U s ∈ Matrix.orthogonalGroup (Fin d) ℝ ∧
      V s ∈ Matrix.orthogonalGroup (Fin d) ℝ ∧ (∀ k, 0 ≤ sig s k) ∧
      W s = U s * Matrix.diagonal (sig s) * (V s)ᵀ) ∧
    (∀ a b, DifferentiableAt ℝ (fun s => U s a b) t) ∧
    (∀ a b, DifferentiableAt ℝ (fun s => V s a b) t) ∧
    (∀ k, DifferentiableAt ℝ (fun s => sig s k) t)

/-- Theorem 1 ("vanishing singular values"): the smallest of the `n` singular values of
`W ∈ ℝ^{d×n}` (Mathlib's singular values, in decreasing order and indexed from `0`; it is `0`
when `d < n`). -/
def minSingVal {m n : ℕ} (A : Matrix (Fin m) (Fin n) ℝ) : ℝ :=
  (Matrix.toEuclideanLin A).singularValues (n - 1)

/-- Eq. (1) and Corollary 1: the covariance matrix of the embeddings `z_i = W x_i`,
`C = (1/N) ∑_i (z_i - z̄)(z_i - z̄)ᵀ` with `z̄ = (1/N) ∑_i z_i`. -/
def embCov {N n d : ℕ} (x : Fin N → Fin n → ℝ) (W : Matrix (Fin d) (Fin n) ℝ) :
    Matrix (Fin d) (Fin d) ℝ :=
  Matrix.of fun a b =>
    (N : ℝ)⁻¹ * ∑ i, (embed W x i a - (N : ℝ)⁻¹ * ∑ j, embed W x j a) *
      (embed W x i b - (N : ℝ)⁻¹ * ∑ j, embed W x j b)

end DimensionalCollapse
Source
Jing, Vincent, LeCun and Tian, Understanding Dimensional Collapse in Contrastive Self-supervised Learning, ICLR 2022, arXiv:2110.09348v3, Sections 3, 4.1–4.2, 5.1–5.3, Appendix A and Appendix B.2, eqs. (1)–(9), pp. 3–6, 11–14
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

sqNorm. For a natural number ddd and a vector v=(v1,…,vd)∈Rdv=(v_1,\dots,v_d)\in\mathbb{R}^dv=(v1​,…,vd​)∈Rd, this is the squared Euclidean norm

∣v∣2=∑c=1dvc2.|v|^2=\sum_{c=1}^{d} v_c^2 .∣v∣2=c=1∑d​vc2​.

When d=0d=0d=0 the sum is empty and the value is 000.


partitionZ. Fix N,d∈NN,d\in\mathbb{N}N,d∈N and two families of vectors z=(z1,…,zN)z=(z_1,\dots,z_N)z=(z1​,…,zN​) and z′=(z1′,…,zN′)z'=(z'_1,\dots,z'_N)z′=(z1′​,…,zN′​) in Rd\mathbb{R}^dRd, together with an index i∈{1,…,N}i\in\{1,\dots,N\}i∈{1,…,N}. The definition is

Zi(z,z′)=∑j=1j≠iNexp⁡ ⁣(−12∣zi−zj∣2)+exp⁡ ⁣(−12∣zi−zi′∣2),Z_i(z,z')=\sum_{\substack{j=1\\ j\neq i}}^{N}\exp\!\Big(-\tfrac{1}{2}|z_i-z_j|^2\Big)+\exp\!\Big(-\tfrac{1}{2}|z_i-z'_i|^2\Big),Zi​(z,z′)=j=1j=i​∑N​exp(−21​∣zi​−zj​∣2)+exp(−21​∣zi​−zi′​∣2),

where ∣⋅∣2|\cdot|^2∣⋅∣2 is the squared Euclidean norm.

  • The sum over j≠ij\neq ij=i uses only vectors of the first family zzz. The only member of z′z'z′ that appears is zi′z'_izi′​.
  • Every term is strictly positive, so Zi>0Z_i>0Zi​>0 always.
  • When N=1N=1N=1, the first sum is empty and Zi=exp⁡(−12∣z1−z1′∣2)Z_i=\exp(-\tfrac12|z_1-z'_1|^2)Zi​=exp(−21​∣z1​−z1′​∣2).

infoNCE. For N,d∈NN,d\in\mathbb{N}N,d∈N and families z=(zi)i=1Nz=(z_i)_{i=1}^Nz=(zi​)i=1N​, z′=(zi′)i=1Nz'=(z'_i)_{i=1}^Nz′=(zi′​)i=1N​ in Rd\mathbb{R}^dRd, the loss is

L(z,z′)=−∑i=1Nlog⁡exp⁡ ⁣(−12∣zi−zi′∣2)Zi(z,z′).L(z,z')=-\sum_{i=1}^{N}\log\frac{\exp\!\big(-\tfrac12|z_i-z'_i|^2\big)}{Z_i(z,z')}.L(z,z′)=−i=1∑N​logZi​(z,z′)exp(−21​∣zi​−zi′​∣2)​.

Here ZiZ_iZi​ is the quantity above:

Zi=∑j≠iexp⁡(−12∣zi−zj∣2)+exp⁡(−12∣zi−zi′∣2).Z_i=\sum_{j\neq i}\exp(-\tfrac12|z_i-z_j|^2)+\exp(-\tfrac12|z_i-z'_i|^2).Zi​=j=i∑​exp(−21​∣zi​−zj​∣2)+exp(−21​∣zi​−zi′​∣2).

Some consequences of the definition:

  • The argument of log⁡\loglog is always a positive number in (0,1](0,1](0,1], so no junk value of the logarithm or of division arises.
  • Equivalently, L=∑i(12∣zi−zi′∣2+log⁡Zi)L=\sum_i\big(\tfrac12|z_i-z'_i|^2+\log Z_i\big)L=∑i​(21​∣zi​−zi′​∣2+logZi​).
  • There is no temperature parameter.
  • The "negatives" in ZiZ_iZi​ are only the zjz_jzj​ with j≠ij\ne ij=i, never the zj′z'_jzj′​.
  • For N=0N=0N=0, L=0L=0L=0.

alpha. For N,d∈NN,d\in\mathbb{N}N,d∈N, families z,z′z,z'z,z′ of NNN vectors in Rd\mathbb{R}^dRd, and indices i,j∈{1,…,N}i,j\in\{1,\dots,N\}i,j∈{1,…,N}, the weights are

αij(z,z′)={exp⁡(−12∣zi−zi′∣2)Zi(z,z′),j=i,exp⁡(−12∣zi−zj∣2)Zi(z,z′),j≠i,\alpha_{ij}(z,z')=\begin{cases}\dfrac{\exp(-\tfrac12|z_i-z'_i|^2)}{Z_i(z,z')}, & j=i,\\[2ex] \dfrac{\exp(-\tfrac12|z_i-z_j|^2)}{Z_i(z,z')}, & j\neq i,\end{cases}αij​(z,z′)=⎩⎨⎧​Zi​(z,z′)exp(−21​∣zi​−zi′​∣2)​,Zi​(z,z′)exp(−21​∣zi​−zj​∣2)​,​j=i,j=i,​

with ZiZ_iZi​ as defined above.

  • The normalisation always uses ZiZ_iZi​, the partition function of the first index. Hence αij\alpha_{ij}αij​ and αji\alpha_{ji}αji​ generally differ.
  • By the definition of ZiZ_iZi​, each row satisfies ∑jαij=1\sum_{j}\alpha_{ij}=1∑j​αij​=1, and every αij∈(0,1]\alpha_{ij}\in(0,1]αij​∈(0,1].
  • This normalisation is a consequence, not part of the definition.

embed. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, a real d×nd\times nd×n matrix WWW, and inputs x=(x1,…,xN)x=(x_1,\dots,x_N)x=(x1​,…,xN​) with xi∈Rnx_i\in\mathbb{R}^nxi​∈Rn, this is the family of embeddings

zi=Wxi∈Rd,i=1,…,N,z_i=Wx_i\in\mathbb{R}^d,\qquad i=1,\dots,N,zi​=Wxi​∈Rd,i=1,…,N,

where WxiWx_iWxi​ is the ordinary matrix–vector product.


gradZ. For N,d∈NN,d\in\mathbb{N}N,d∈N, families z,z′z,z'z,z′ of NNN vectors in Rd\mathbb{R}^dRd, and an index iii, this is the vector gzi∈Rdg_{z_i}\in\mathbb{R}^dgzi​​∈Rd whose ccc-th component (c=1,…,dc=1,\dots,dc=1,…,d) is

(gzi)c=dds∣s=0 L(z1,…,zi−1, zi+s ec, zi+1,…,zN; z′).(g_{z_i})_c=\frac{d}{ds}\Big|_{s=0}\,L\big(z_1,\dots,z_{i-1},\,z_i+s\,e_c,\,z_{i+1},\dots,z_N;\ z'\big).(gzi​​)c​=dsd​​s=0​L(z1​,…,zi−1​,zi​+sec​,zi+1​,…,zN​; z′).

Here LLL is the InfoNCE loss above and ece_cec​ is the ccc-th standard basis vector of Rd\mathbb{R}^dRd.

  • So it is the vector of partial derivatives of LLL with respect to the coordinates of the single vector ziz_izi​, with z′z'z′ and all other zjz_jzj​ held fixed.
  • Note that ziz_izi​ appears both in the iii-th summand of LLL and in the terms exp⁡(−12∣zj−zi∣2)\exp(-\tfrac12|z_j-z_i|^2)exp(−21​∣zj​−zi​∣2) of the other ZjZ_jZj​.
  • Convention: if the one-variable function were not differentiable at 000, the value would be defined to be 000.

gradZ'. For N,d∈NN,d\in\mathbb{N}N,d∈N, families z,z′z,z'z,z′ of NNN vectors in Rd\mathbb{R}^dRd, and an index iii, this is the vector gzi′∈Rdg_{z'_i}\in\mathbb{R}^dgzi′​​∈Rd with components

(gzi′)c=dds∣s=0 L(z; z1′,…,zi−1′, zi′+s ec, zi+1′,…,zN′),c=1,…,d.(g_{z'_i})_c=\frac{d}{ds}\Big|_{s=0}\,L\big(z;\ z'_1,\dots,z'_{i-1},\,z'_i+s\,e_c,\,z'_{i+1},\dots,z'_N\big),\qquad c=1,\dots,d.(gzi′​​)c​=dsd​​s=0​L(z; z1′​,…,zi−1′​,zi′​+sec​,zi+1′​,…,zN′​),c=1,…,d.
  • This is the vector of partial derivatives of the InfoNCE loss LLL with respect to the coordinates of zi′z'_izi′​, with zzz and all other zj′z'_jzj′​ fixed.
  • zi′z'_izi′​ enters LLL only through the iii-th summand, both in its numerator and in ZiZ_iZi​.
  • The same convention applies: the value would be 000 at a point of non-differentiability.

Gmat. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, input families x=(xi)x=(x_i)x=(xi​) and x′=(xi′)x'=(x'_i)x′=(xi′​) in Rn\mathbb{R}^nRn, and a real d×nd\times nd×n matrix WWW, this is the d×nd\times nd×n matrix

G=∑i=1N(gzi xi⊤+gzi′ xi′⊤),i.e.Gab=∑i=1N((gzi)a(xi)b+(gzi′)a(xi′)b).G=\sum_{i=1}^{N}\Big(g_{z_i}\,x_i^{\top}+g_{z'_i}\,x_i'^{\top}\Big), \qquad\text{i.e.}\qquad G_{ab}=\sum_{i=1}^N\Big((g_{z_i})_a(x_i)_b+(g_{z'_i})_a(x'_i)_b\Big).G=i=1∑N​(gzi​​xi⊤​+gzi′​​xi′⊤​),i.e.Gab​=i=1∑N​((gzi​​)a​(xi​)b​+(gzi′​​)a​(xi′​)b​).
  • gzig_{z_i}gzi​​ and gzi′g_{z'_i}gzi′​​ are the partial-derivative vectors of the InfoNCE loss defined above.
  • They are evaluated at the embeddings zj=Wxjz_j=Wx_jzj​=Wxj​ and zj′=Wxj′z'_j=Wx'_jzj′​=Wxj′​ for all jjj.
  • For N=0N=0N=0, G=0G=0G=0.

Xmat. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, input families x=(xi)x=(x_i)x=(xi​) and x′=(xi′)x'=(x'_i)x′=(xi′​) in Rn\mathbb{R}^nRn, and a real d×nd\times nd×n matrix WWW, this is the n×nn\times nn×n matrix

X=∑i=1N(∑j≠iαij (xi′−xj)+∑j≠iαji (xi−xj))xi⊤  −  ∑i=1N(1−αii) (xi′−xi) xi′⊤.X=\sum_{i=1}^N\Big(\sum_{j\neq i}\alpha_{ij}\,(x'_i-x_j)+\sum_{j\neq i}\alpha_{ji}\,(x_i-x_j)\Big)x_i^{\top}\;-\;\sum_{i=1}^N(1-\alpha_{ii})\,(x'_i-x_i)\,x_i'^{\top}.X=i=1∑N​(j=i∑​αij​(xi′​−xj​)+j=i∑​αji​(xi​−xj​))xi⊤​−i=1∑N​(1−αii​)(xi′​−xi​)xi′⊤​.
  • The weights αij=αij(Wx,Wx′)\alpha_{ij}=\alpha_{ij}(Wx,Wx')αij​=αij​(Wx,Wx′) are those defined above, computed from the embeddings zk=Wxkz_k=Wx_kzk​=Wxk​ and zk′=Wxk′z'_k=Wx'_kzk′​=Wxk′​. Thus XXX depends on WWW.
  • In the first inner sum the weight is αij\alpha_{ij}αij​, normalised by ZiZ_iZi​, and the vector is xi′−xjx'_i-x_jxi′​−xj​.
  • In the second inner sum the weight is αji\alpha_{ji}αji​, normalised by ZjZ_jZj​, and the vector is xi−xjx_i-x_jxi​−xj​.
  • All outer products are column vector times row vector.

Sigma0. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, input families x,x′x,x'x,x′ in Rn\mathbb{R}^nRn, and a real d×nd\times nd×n matrix WWW, this is the n×nn\times nn×n matrix

Σ^0=∑i=1N∑j=1Nαij (xi−xj)(xi−xj)⊤.\hat\Sigma_0=\sum_{i=1}^N\sum_{j=1}^N\alpha_{ij}\,(x_i-x_j)(x_i-x_j)^{\top}.Σ^0​=i=1∑N​j=1∑N​αij​(xi​−xj​)(xi​−xj​)⊤.
  • The weights are αij=αij(Wx,Wx′)\alpha_{ij}=\alpha_{ij}(Wx,Wx')αij​=αij​(Wx,Wx′) as above.
  • The double sum includes j=ij=ij=i, but those terms vanish because xi−xi=0x_i-x_i=0xi​−xi​=0. Hence αii\alpha_{ii}αii​ and x′x'x′ enter only through the normalisations ZiZ_iZi​ inside the off-diagonal weights.
  • Since αij≠αji\alpha_{ij}\ne\alpha_{ji}αij​=αji​ in general, the weights are not symmetrised. Each summand matrix is itself symmetric, however.

Sigma1. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, input families x,x′x,x'x,x′ in Rn\mathbb{R}^nRn, and a real d×nd\times nd×n matrix WWW, this is the n×nn\times nn×n matrix

Σ^1=∑i=1N(1−αii) (xi′−xi)(xi′−xi)⊤,\hat\Sigma_1=\sum_{i=1}^N\big(1-\alpha_{ii}\big)\,(x'_i-x_i)(x'_i-x_i)^{\top},Σ^1​=i=1∑N​(1−αii​)(xi′​−xi​)(xi′​−xi​)⊤,

where αii=exp⁡(−12∣Wxi−Wxi′∣2)/Zi(Wx,Wx′)\alpha_{ii}=\exp(-\tfrac12|Wx_i-Wx'_i|^2)/Z_i(Wx,Wx')αii​=exp(−21​∣Wxi​−Wxi′​∣2)/Zi​(Wx,Wx′) is the diagonal weight defined above. Since αii∈(0,1]\alpha_{ii}\in(0,1]αii​∈(0,1], each coefficient 1−αii1-\alpha_{ii}1−αii​ lies in [0,1)[0,1)[0,1).


matGrad. For m,n∈Nm,n\in\mathbb{N}m,n∈N, a real-valued function FFF on real m×nm\times nm×n matrices, and a matrix WWW, this is the m×nm\times nm×n matrix with entries

(∂F∂W)ab=dds∣s=0F(W+s Eab).\Big(\frac{\partial F}{\partial W}\Big)_{ab}=\frac{d}{ds}\Big|_{s=0}F\big(W+s\,E_{ab}\big).(∂W∂F​)ab​=dsd​​s=0​F(W+sEab​).
  • EabE_{ab}Eab​ is the matrix with a 111 in position (a,b)(a,b)(a,b) and 000 elsewhere.
  • So each entry is the partial derivative of FFF with respect to the single entry WabW_{ab}Wab​.
  • No differentiability of FFF is assumed. If a one-variable function s↦F(W+sEab)s\mapsto F(W+sE_{ab})s↦F(W+sEab​) is not differentiable at 000, the corresponding entry is 000 by convention.

linLoss. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, input families x=(xi)x=(x_i)x=(xi​) and x′=(xi′)x'=(x'_i)x′=(xi′​) in Rn\mathbb{R}^nRn, and a real d×nd\times nd×n matrix WWW, this is

Llin(W)=L((Wxi)i=1N, (Wxi′)i=1N).L_{\mathrm{lin}}(W)=L\big((Wx_i)_{i=1}^N,\,(Wx'_i)_{i=1}^N\big).Llin​(W)=L((Wxi​)i=1N​,(Wxi′​)i=1N​).

This is the InfoNCE loss LLL defined above, evaluated at the embeddings of both families under the same matrix WWW.


twoLayerLoss. For N,d∈NN,d\in\mathbb{N}N,d∈N, input families x=(xi)x=(x_i)x=(xi​) and x′=(xi′)x'=(x'_i)x′=(xi′​) in Rd\mathbb{R}^dRd, and real d×dd\times dd×d matrices W1,W2W_1,W_2W1​,W2​, this is

L2(W1,W2)=L((W2W1xi)i=1N, (W2W1xi′)i=1N).L_2(W_1,W_2)=L\big((W_2W_1x_i)_{i=1}^N,\,(W_2W_1x'_i)_{i=1}^N\big).L2​(W1​,W2​)=L((W2​W1​xi​)i=1N​,(W2​W1​xi′​)i=1N​).
  • This is the InfoNCE loss of the embeddings under the product W2W1W_2W_1W2​W1​.
  • The input dimension, the hidden dimension and the output dimension are all the same number ddd.
  • The loss depends on (W1,W2)(W_1,W_2)(W1​,W2​) only through the product W2W1W_2W_1W2​W1​.

IsTwoLayerFlowAt. Fix N,d∈NN,d\in\mathbb{N}N,d∈N, input families x,x′x,x'x,x′ in Rd\mathbb{R}^dRd, matrix-valued functions of time W1,W2:R→Rd×dW_1,W_2:\mathbb{R}\to\mathbb{R}^{d\times d}W1​,W2​:R→Rd×d, and a time t∈Rt\in\mathbb{R}t∈R. The proposition asserts that, for every pair of indices a,b∈{1,…,d}a,b\in\{1,\dots,d\}a,b∈{1,…,d}, both of the following hold:

  1. The scalar function s↦W1(s)abs\mapsto W_1(s)_{ab}s↦W1​(s)ab​ is differentiable at ttt with derivative
W˙1(t)ab=−(∂∂ML2(M,W2(t))∣M=W1(t))ab.\dot W_1(t)_{ab}=-\Big(\frac{\partial}{\partial M}L_2\big(M,W_2(t)\big)\Big|_{M=W_1(t)}\Big)_{ab}.W˙1​(t)ab​=−(∂M∂​L2​(M,W2​(t))​M=W1​(t)​)ab​.
  1. The scalar function s↦W2(s)abs\mapsto W_2(s)_{ab}s↦W2​(s)ab​ is differentiable at ttt with derivative
W˙2(t)ab=−(∂∂ML2(W1(t),M)∣M=W2(t))ab.\dot W_2(t)_{ab}=-\Big(\frac{\partial}{\partial M}L_2\big(W_1(t),M\big)\Big|_{M=W_2(t)}\Big)_{ab}.W˙2​(t)ab​=−(∂M∂​L2​(W1​(t),M)​M=W2​(t)​)ab​.
  • L2L_2L2​ is the two-layer InfoNCE loss above.
  • The partial derivatives are entrywise, as in matGrad, so they would be 000 at a point of non-differentiability.
  • This is a condition at the single instant ttt only. It says nothing about other times unless it is assumed for each of them separately.

IsDiffSVDAt. Fix d∈Nd\in\mathbb{N}d∈N, functions W,U,V:R→Rd×dW,U,V:\mathbb{R}\to\mathbb{R}^{d\times d}W,U,V:R→Rd×d, a function σ:R→Rd\sigma:\mathbb{R}\to\mathbb{R}^dσ:R→Rd, and a time ttt. The proposition asserts two things.

(i) A factorisation near ttt. There is a neighbourhood of ttt such that every sss in it satisfies all of the following:

  • U(s)U(s)U(s) and V(s)V(s)V(s) are orthogonal matrices, meaning U⊤U=UU⊤=IU^{\top}U=UU^{\top}=IU⊤U=UU⊤=I and likewise for VVV.
  • σk(s)≥0\sigma_k(s)\ge 0σk​(s)≥0 for every k=1,…,dk=1,\dots,dk=1,…,d.
  • The factorisation holds:
W(s)=U(s) diag⁡(σ1(s),…,σd(s)) V(s)⊤.W(s)=U(s)\,\operatorname{diag}\big(\sigma_1(s),\dots,\sigma_d(s)\big)\,V(s)^{\top}.W(s)=U(s)diag(σ1​(s),…,σd​(s))V(s)⊤.

(ii) Differentiability at ttt. Every entry s↦U(s)abs\mapsto U(s)_{ab}s↦U(s)ab​, every entry s↦V(s)abs\mapsto V(s)_{ab}s↦V(s)ab​, and every component s↦σk(s)s\mapsto\sigma_k(s)s↦σk​(s) is differentiable at the single point ttt.

Points to note:

  • The σk\sigma_kσk​ are not required to be sorted, distinct, or nonzero.
  • No differentiability of WWW is assumed separately.
  • Nothing is required outside the neighbourhood of ttt.
  • Only square matrices are covered.
  • For d=0d=0d=0 the condition holds trivially.

minSingVal. For m,n∈Nm,n\in\mathbb{N}m,n∈N and a real m×nm\times nm×n matrix AAA, regard AAA as a linear map Rn→Rm\mathbb{R}^n\to\mathbb{R}^mRn→Rm between Euclidean spaces. Let

s0(A)≥s1(A)≥s2(A)≥⋯s_0(A)\ge s_1(A)\ge s_2(A)\ge\cdotss0​(A)≥s1​(A)≥s2​(A)≥⋯

be its sequence of singular values: the square roots of the eigenvalues of A⊤AA^{\top}AA⊤A, counted with multiplicity, in decreasing order, indexed from 000, as provided by the library. The definition returns

σmin⁡(A)=sn−1(A),\sigma_{\min}(A)=s_{n-1}(A),σmin​(A)=sn−1​(A),

the nnn-th largest singular value.

  • n−1n-1n−1 is natural-number subtraction. When n=0n=0n=0 the index is 000, so the value is s0(A)s_0(A)s0​(A) of a map on the zero space.
  • When m<nm<nm<n, AAA has rank at most m<nm<nm<n, so this value is 000.

embCov. For N,n,d∈NN,n,d\in\mathbb{N}N,n,d∈N, inputs x=(x1,…,xN)x=(x_1,\dots,x_N)x=(x1​,…,xN​) in Rn\mathbb{R}^nRn, and a real d×nd\times nd×n matrix WWW, put zi=Wxiz_i=Wx_izi​=Wxi​. This is the d×dd\times dd×d matrix

C=1N∑i=1N(zi−zˉ)(zi−zˉ)⊤,zˉ=1N∑j=1Nzj.C=\frac1N\sum_{i=1}^N(z_i-\bar z)(z_i-\bar z)^{\top},\qquad \bar z=\frac1N\sum_{j=1}^N z_j.C=N1​i=1∑N​(zi​−zˉ)(zi​−zˉ)⊤,zˉ=N1​j=1∑N​zj​.
  • Only the first family xxx enters.
  • The normalisation is 1/N1/N1/N, not 1/(N−1)1/(N-1)1/(N−1).
  • When N=0N=0N=0, 1/N1/N1/N is interpreted as 000 and the sum is empty, so C=0C=0C=0.
Human review
  • Endorsed by Shuze Chen · Nov 24, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Xeroth · Nov 24, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me