InfoNCE linear models, gradients, the matrix and differentiable SVDs
DefinitionDimensionalCollapse_BasicObjects of Sections 3–5 and Appendices A–B of Jing, Vincent, LeCun and Tian (ICLR 2022).
- Data and embeddings. There are pairs of inputs , , where is an augmented view of . A linear model gives embeddings , . The two-layer model has (inputs in ) and . We write .
- InfoNCE (eq. 2). With ,
The softmax weights are for and , so . 3. Gradients (Lemma 1, eq. 3). and are the gradients of with respect to and , and , evaluated at . The gradient of a function of a matrix is the matrix of its partial derivatives . 4. The matrix (eq. 6, in the form of Appendix B.2).
with computed from , . The weighted covariances of Lemma 2 are and . 5. Gradient flows. The linear loss is with ; the two-layer loss is with . The two-layer weights follow the gradient flow at time if and at . 6. Differentiable SVD (Section 5.3, Appendix A). with orthogonal and for all near , and differentiable at . The columns of are the singular vectors of . 7. Spectra. is the smallest of the singular values of (zero when ). The embedding covariance (eq. 1) is with , .
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 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.
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
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
sqNorm. For a natural number and a vector , this is the squared Euclidean norm
When the sum is empty and the value is .
partitionZ. Fix and two families of vectors and in , together with an index . The definition is
where is the squared Euclidean norm.
- The sum over uses only vectors of the first family . The only member of that appears is .
- Every term is strictly positive, so always.
- When , the first sum is empty and .
infoNCE. For and families , in , the loss is
Here is the quantity above:
Some consequences of the definition:
- The argument of is always a positive number in , so no junk value of the logarithm or of division arises.
- Equivalently, .
- There is no temperature parameter.
- The "negatives" in are only the with , never the .
- For , .
alpha. For , families of vectors in , and indices , the weights are
with as defined above.
- The normalisation always uses , the partition function of the first index. Hence and generally differ.
- By the definition of , each row satisfies , and every .
- This normalisation is a consequence, not part of the definition.
embed. For , a real matrix , and inputs with , this is the family of embeddings
where is the ordinary matrix–vector product.
gradZ. For , families of vectors in , and an index , this is the vector whose -th component () is
Here is the InfoNCE loss above and is the -th standard basis vector of .
- So it is the vector of partial derivatives of with respect to the coordinates of the single vector , with and all other held fixed.
- Note that appears both in the -th summand of and in the terms of the other .
- Convention: if the one-variable function were not differentiable at , the value would be defined to be .
gradZ'. For , families of vectors in , and an index , this is the vector with components
- This is the vector of partial derivatives of the InfoNCE loss with respect to the coordinates of , with and all other fixed.
- enters only through the -th summand, both in its numerator and in .
- The same convention applies: the value would be at a point of non-differentiability.
Gmat. For , input families and in , and a real matrix , this is the matrix
- and are the partial-derivative vectors of the InfoNCE loss defined above.
- They are evaluated at the embeddings and for all .
- For , .
Xmat. For , input families and in , and a real matrix , this is the matrix
- The weights are those defined above, computed from the embeddings and . Thus depends on .
- In the first inner sum the weight is , normalised by , and the vector is .
- In the second inner sum the weight is , normalised by , and the vector is .
- All outer products are column vector times row vector.
Sigma0. For , input families in , and a real matrix , this is the matrix
- The weights are as above.
- The double sum includes , but those terms vanish because . Hence and enter only through the normalisations inside the off-diagonal weights.
- Since in general, the weights are not symmetrised. Each summand matrix is itself symmetric, however.
Sigma1. For , input families in , and a real matrix , this is the matrix
where is the diagonal weight defined above. Since , each coefficient lies in .
matGrad. For , a real-valued function on real matrices, and a matrix , this is the matrix with entries
- is the matrix with a in position and elsewhere.
- So each entry is the partial derivative of with respect to the single entry .
- No differentiability of is assumed. If a one-variable function is not differentiable at , the corresponding entry is by convention.
linLoss. For , input families and in , and a real matrix , this is
This is the InfoNCE loss defined above, evaluated at the embeddings of both families under the same matrix .
twoLayerLoss. For , input families and in , and real matrices , this is
- This is the InfoNCE loss of the embeddings under the product .
- The input dimension, the hidden dimension and the output dimension are all the same number .
- The loss depends on only through the product .
IsTwoLayerFlowAt. Fix , input families in , matrix-valued functions of time , and a time . The proposition asserts that, for every pair of indices , both of the following hold:
- The scalar function is differentiable at with derivative
- The scalar function is differentiable at with derivative
- is the two-layer InfoNCE loss above.
- The partial derivatives are entrywise, as in
matGrad, so they would be at a point of non-differentiability. - This is a condition at the single instant only. It says nothing about other times unless it is assumed for each of them separately.
IsDiffSVDAt. Fix , functions , a function , and a time . The proposition asserts two things.
(i) A factorisation near . There is a neighbourhood of such that every in it satisfies all of the following:
- and are orthogonal matrices, meaning and likewise for .
- for every .
- The factorisation holds:
(ii) Differentiability at . Every entry , every entry , and every component is differentiable at the single point .
Points to note:
- The are not required to be sorted, distinct, or nonzero.
- No differentiability of is assumed separately.
- Nothing is required outside the neighbourhood of .
- Only square matrices are covered.
- For the condition holds trivially.
minSingVal. For and a real matrix , regard as a linear map between Euclidean spaces. Let
be its sequence of singular values: the square roots of the eigenvalues of , counted with multiplicity, in decreasing order, indexed from , as provided by the library. The definition returns
the -th largest singular value.
- is natural-number subtraction. When the index is , so the value is of a map on the zero space.
- When , has rank at most , so this value is .
embCov. For , inputs in , and a real matrix , put . This is the matrix
- Only the first family enters.
- The normalisation is , not .
- When , is interpreted as and the sum is empty, so .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.