Relation matrices, graph Laplacians, VICReg and SimCLR losses, and linear-probe objects
DefinitionSSLSpectralEmbedding_BasicObjects of Sections 2, 3, 4.1 and 6 and Appendix A.15 of Balestriero and LeCun (2022). A data set has samples; a representation is a matrix whose -th row is the embedding of sample .
- Relation matrix and Laplacian. A relation matrix is symmetric, has nonnegative entries and zero diagonal. is its degree matrix and its graph Laplacian. is the centring matrix, and .
- VICReg (eq. 2). , , , and . The squared variant (Section 3.1) replaces by .
- Spectral data of VICReg (eq. 8). with orthogonal and . The candidate optimum is .
- Laplacian Eigenmaps (eq. 11). minimises over with and columns of zero mean.
- Linear VICReg (eq. 12). For data , .
- Graph estimation (eqs. 14-16, 3). For a dissimilarity on embeddings, . The objective is with (and ). is the set of symmetric nonnegative matrices with zero diagonal, and its right-stochastic elements. The kernels are and its row-normalised version. The cosine distance is , and the SimCLR estimate is for , on the diagonal.
- Linear probe (Section 6.1). for a target .
- LDA scatter matrices (Appendix A.15). For labels with class sizes , if and otherwise. Then and .
Formalization Note is normalised by , as in the proofs of Appendix A.7-A.9 and the code for eq. (12). The code for eq. (2) uses torch.cov, which normalises by . The sum in runs over all ordered off-diagonal pairs, as in the code for eq. (2). The indicator in eq. (3) is printed as and in eq. (16) as ; both are read as . For zero vectors the cosine similarity takes Lean's value .
import Mathlib
/-!
Balestriero and LeCun, *Contrastive and Non-Contrastive Self-Supervised Learning Recover Global
and Local Spectral Embedding Methods*, arXiv:2205.11508v3 (NeurIPS 2022): the relation matrix,
graph Laplacian and VICReg losses of Sections 2 and 3, the Laplacian-Eigenmaps problem of
Section 3.2, the graph estimates of SimCLR in Section 4.1, the linear-probe loss of Section 6.1
and the scatter matrices of the LDA argument in Appendix A.15.
A data set of `N` samples with `K`-dimensional embeddings is a matrix `Z : Matrix (Fin N) (Fin K) ℝ`
whose `i`-th row is the embedding of sample `i`.
-/
noncomputable section
namespace SSLSpectralEmbedding
open Matrix
variable {N K : ℕ}
/-- Section 2, p. 3 and Fig. 1: a relation matrix `G` is symmetric, has nonnegative entries and
zero diagonal; `G i j > 0` marks samples `i` and `j` as a positive pair. -/
def IsRelationMatrix (G : Matrix (Fin N) (Fin N) ℝ) : Prop :=
G.IsSymm ∧ (∀ i j, 0 ≤ G i j) ∧ ∀ i, G i i = 0
/-- Section 3.1, below eq. (7), p. 6: the diagonal degree matrix `D` of `G`, `D i i = ∑ j, G i j`. -/
def degMat (G : Matrix (Fin N) (Fin N) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
Matrix.diagonal fun i => ∑ j, G i j
/-- Section 3.1, below eq. (7), p. 6: the graph Laplacian `L = D - G`. -/
def laplacian (G : Matrix (Fin N) (Fin N) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
degMat G - G
/-- Eq. (8), p. 6 and Appendix A.7: the centring matrix `H = I - (1/N) 1 1ᵀ` (the Laplacian of the
complete graph, scaled by `1/N`). -/
def centering (N : ℕ) : Matrix (Fin N) (Fin N) ℝ :=
1 - Matrix.of fun _ _ => (1 / (N : ℝ))
/-- Appendix A.7–A.9: the covariance matrix of the columns of `Z`, `Cov(Z) = (1/N) Zᵀ H Z`
(normalised by `N`). -/
def cov (Z : Matrix (Fin N) (Fin K) ℝ) : Matrix (Fin K) (Fin K) ℝ :=
(1 / (N : ℝ)) • (Zᵀ * centering N * Z)
/-- Eq. (2) and eq. (7): the pairwise sum `∑_{i,j} G i j ‖Z_{i,.} - Z_{j,.}‖²` of the invariance
term. -/
def pairSum (G : Matrix (Fin N) (Fin N) ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) : ℝ :=
∑ i, ∑ j, G i j * ∑ k, (Z i k - Z j k) ^ 2
/-- Eq. (2): the invariance term `L_inv(Z) = (γ/N) ∑_{i,j} G i j ‖Z_{i,.} - Z_{j,.}‖²`. -/
def invTerm (γ : ℝ) (G : Matrix (Fin N) (Fin N) ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) : ℝ :=
γ / N * pairSum G Z
/-- Eq. (2): the variance (hinge) term `L_var(Z) = ∑_k max(0, 1 - √Cov(Z)_{k,k})`. -/
def varTerm (Z : Matrix (Fin N) (Fin K) ℝ) : ℝ :=
∑ k, max 0 (1 - Real.sqrt (cov Z k k))
/-- Eq. (2): the covariance term `L_cov(Z) = ∑_k ∑_{j ≠ k} Cov(Z)_{k,j}²`. -/
def covTerm (Z : Matrix (Fin N) (Fin K) ℝ) : ℝ :=
∑ k, ∑ j, if j = k then 0 else cov Z k j ^ 2
/-- Eq. (2), p. 4: the VICReg loss `α L_var + β L_cov + L_inv`. -/
def vicLoss (α β γ : ℝ) (G : Matrix (Fin N) (Fin N) ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) : ℝ :=
α * varTerm Z + β * covTerm Z + invTerm γ G Z
/-- Section 3.1, p. 5: the VICReg loss with the hinge variance term replaced by the squared loss
`∑_k (1 - Cov(Z)_{k,k})²`. -/
def vicLossSq (α β γ : ℝ) (G : Matrix (Fin N) (Fin N) ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) : ℝ :=
α * ∑ k, (1 - cov Z k k) ^ 2 + β * covTerm Z + invTerm γ G Z
/-- Eq. (8), p. 6: the matrix `I - (1/N) 1 1ᵀ - (γ/α)(D - G)` whose eigendecomposition gives the
optimal VICReg representation. -/
def vicMat (α γ : ℝ) (G : Matrix (Fin N) (Fin N) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
centering N - (γ / α) • laplacian G
/-- Eq. (8): `M = P diag(λ) Pᵀ` with `P` orthogonal and the eigenvalues `λ` in descending order. -/
def IsDescEigendecomp (M P : Matrix (Fin N) (Fin N) ℝ) (lam : Fin N → ℝ) : Prop :=
Pᵀ * P = 1 ∧ M = P * Matrix.diagonal lam * Pᵀ ∧ Antitone lam
/-- Theorem 1, p. 7: `Z*_{α,γ} = (P (diag(λ) N)^{1/2})_{:,1:K}`, the first `K` columns of `P`
scaled by `√(N λ_k)`. -/
def vicOpt (P : Matrix (Fin N) (Fin N) ℝ) (lam : Fin N → ℝ) (hKN : K ≤ N) :
Matrix (Fin N) (Fin K) ℝ :=
fun i k => P i (Fin.castLE hKN k) * Real.sqrt (lam (Fin.castLE hKN k) * N)
/-- Eq. (11), p. 9 and Section 3.2: an optimal Laplacian-Eigenmaps representation `Z*_LE`: it
minimises `Tr(Zᵀ (D - G) Z)` subject to `Zᵀ D Z = I`, among representations whose columns have
zero mean (LE discards the eigenvector of the eigenvalue `0`). -/
def IsLESolution (G : Matrix (Fin N) (Fin N) ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) : Prop :=
(Zᵀ * degMat G * Z = 1 ∧ ∀ k, ∑ i, Z i k = 0) ∧
∀ Z' : Matrix (Fin N) (Fin K) ℝ, Z'ᵀ * degMat G * Z' = 1 → (∀ k, ∑ i, Z' i k = 0) →
(Zᵀ * laplacian G * Z).trace ≤ (Z'ᵀ * laplacian G * Z').trace
/-- Eq. (12), p. 10: `B = Cov(X) - (γ/(α N)) Xᵀ L X`, whose square root gives the optimal weights of
linear VICReg. -/
def linB {D : ℕ} (α γ : ℝ) (G : Matrix (Fin N) (Fin N) ℝ) (X : Matrix (Fin N) (Fin D) ℝ) :
Matrix (Fin D) (Fin D) ℝ :=
cov X - (γ / (α * N)) • (Xᵀ * laplacian G * X)
/-- Section 4.1, p. 11: the matrix of pairwise distances `(D)_{i,j} = d(f(x_i), f(x_j))` of the
embeddings (rows of `Z`). -/
def distMat (d : (Fin K → ℝ) → (Fin K → ℝ) → ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) :
Matrix (Fin N) (Fin N) ℝ :=
fun i j => d (Z i) (Z j)
/-- Eq. (14), p. 11: the graph-estimation objective `∑_{i,j} (D)_{i,j} (G)_{i,j} + R(G)`. -/
def graphObjective (Dm : Matrix (Fin N) (Fin N) ℝ) (R : Matrix (Fin N) (Fin N) ℝ → ℝ)
(W : Matrix (Fin N) (Fin N) ℝ) : ℝ :=
∑ i, ∑ j, Dm i j * W i j + R W
/-- Eq. (15), p. 11: the entropic regulariser `R_log(G) = ∑_{i≠j} τ G_{i,j} (log G_{i,j} - 1)`
(with `0 log 0 = 0`). -/
def entropyReg (τ : ℝ) (W : Matrix (Fin N) (Fin N) ℝ) : ℝ :=
∑ i, ∑ j, if i = j then 0 else τ * W i j * (Real.log (W i j) - 1)
/-- Section 4.1, p. 11: `𝒢`, the symmetric matrices with nonnegative entries and zero diagonal. -/
def graphSet (N : ℕ) : Set (Matrix (Fin N) (Fin N) ℝ) :=
{W | W.IsSymm ∧ (∀ i j, 0 ≤ W i j) ∧ ∀ i, W i i = 0}
/-- Section 4.1, p. 11: `𝒢_rsto = {G ∈ 𝒢 : G 1 = 1}`, the right-stochastic elements of `𝒢`. -/
def rstoSet (N : ℕ) : Set (Matrix (Fin N) (Fin N) ℝ) :=
{W | W ∈ graphSet N ∧ ∀ i, ∑ j, W i j = 1}
/-- Appendix A.10, p. 27: the set over which the proof of Theorem 4 solves the `𝒢_rsto` problem:
nonnegative, zero-diagonal, right-stochastic matrices (the proof does not impose symmetry). -/
def rowStochSet (N : ℕ) : Set (Matrix (Fin N) (Fin N) ℝ) :=
{W | (∀ i j, 0 ≤ W i j) ∧ (∀ i, W i i = 0) ∧ ∀ i, ∑ j, W i j = 1}
/-- Eq. (16), first line: `(Ĝ)_{i,j} = exp(-(D)_{i,j}/τ) 1_{i≠j}`. -/
def expKernel (τ : ℝ) (Dm : Matrix (Fin N) (Fin N) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
fun i j => if i = j then 0 else Real.exp (-Dm i j / τ)
/-- Eq. (16), second line: `(Ĝ)_{i,j} = exp(-(D)_{i,j}/τ) / ∑_{k≠i} exp(-(D)_{i,k}/τ) 1_{i≠j}`. -/
def softmaxKernel (τ : ℝ) (Dm : Matrix (Fin N) (Fin N) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
fun i j => if i = j then 0 else
Real.exp (-Dm i j / τ) / ∑ k, (if k = i then 0 else Real.exp (-Dm i k / τ))
/-- Section 2, p. 4: the cosine similarity `⟨u, v⟩ / (‖u‖₂ ‖v‖₂)`. -/
def cosSim (u v : Fin K → ℝ) : ℝ :=
(∑ k, u k * v k) / (Real.sqrt (∑ k, u k ^ 2) * Real.sqrt (∑ k, v k ^ 2))
/-- Appendix A.10, p. 28: the cosine distance `1 - ⟨u, v⟩ / (‖u‖ ‖v‖)`. -/
def cosineDist (u v : Fin K → ℝ) : ℝ :=
1 - cosSim u v
/-- Eq. (3), p. 4: the SimCLR similarity estimate
`(Ĝ(Z))_{i,j} = exp(CosSim(z_i,z_j)/τ) / ∑_{k≠i} exp(CosSim(z_i,z_k)/τ)` for `i ≠ j`, and `0` on the
diagonal. -/
def simclrSim (τ : ℝ) (Z : Matrix (Fin N) (Fin K) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
fun i j => if i = j then 0 else
Real.exp (cosSim (Z i) (Z j) / τ) / ∑ k, (if k = i then 0 else Real.exp (cosSim (Z i) (Z k) / τ))
/-- Squared Frobenius norm `‖A‖_F²`. -/
def frobSq {m n : ℕ} (A : Matrix (Fin m) (Fin n) ℝ) : ℝ :=
∑ i, ∑ j, A i j ^ 2
/-- Section 6.1, p. 15: the linear-probe loss `L(W) = (1/2) ‖Y - Z W‖_F²`. -/
def lsLoss {C : ℕ} (Y : Matrix (Fin N) (Fin C) ℝ) (Z : Matrix (Fin N) (Fin K) ℝ)
(W : Matrix (Fin K) (Fin C) ℝ) : ℝ :=
(1 / 2) * frobSq (Y - Z * W)
/-- Appendix A.15, p. 30: the supervised relation matrix normalised to degree `I`:
`(G)_{i,j} = 1/N_c` if samples `i` and `j` both belong to class `c` (with `N_c` samples), else `0`. -/
def ldaRelation {C : ℕ} (y : Fin N → Fin C) : Matrix (Fin N) (Fin N) ℝ :=
fun i j => if y i = y j then 1 / ((Finset.univ.filter fun k => y k = y i).card : ℝ) else 0
/-- Appendix A.15, p. 30: the within-class scatter `Σ_w = Xᵀ (I - G) X`. -/
def withinScatter {D C : ℕ} (X : Matrix (Fin N) (Fin D) ℝ) (y : Fin N → Fin C) :
Matrix (Fin D) (Fin D) ℝ :=
Xᵀ * (1 - ldaRelation y) * X
/-- Appendix A.15, p. 30: the between-class scatter `Σ_b = Xᵀ G X`. -/
def betweenScatter {D C : ℕ} (X : Matrix (Fin N) (Fin D) ℝ) (y : Fin N → Fin C) :
Matrix (Fin D) (Fin D) ℝ :=
Xᵀ * ldaRelation y * X
end SSLSpectralEmbedding
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Throughout, and are arbitrary natural numbers (including ). All matrices are real, and rows and columns are indexed by or . A data matrix has -th row . Several conventions of the underlying system matter below:
- division by zero returns ;
- the square root of a negative number returns ;
- , and for .
Relation matrix. A matrix is called a relation matrix when it satisfies all three of the following:
- ;
- for all ;
- for all .
No other condition is imposed. In particular, need not be nonzero, need not be connected, and need not have positive row sums. For the condition holds trivially.
Degree matrix. For any (no assumptions), the degree matrix is the diagonal matrix of row sums of :
Graph Laplacian. For any (no assumptions), the Laplacian is
Centring matrix. For a natural number , is the matrix
that is, . For it is the empty matrix.
Covariance. For ,
The normalisation is by , not . When , the factor is , and is the zero matrix.
Pairwise sum. For any and ,
The sum runs over all ordered pairs , including (those terms vanish).
Invariance term. For a real , any , and any ,
When , this is .
Variance term. For ,
The square root is taken with the convention for . For , .
Covariance term. For ,
The sum is over ordered pairs, so each unordered off-diagonal pair is counted twice.
VICReg loss. For arbitrary reals (no sign constraints), any (not required to be a relation matrix), and any ,
Squared-variance VICReg loss. For arbitrary reals , any , and any ,
This is the same as , except that the hinge on is replaced by the square of . No square root is involved.
VICReg matrix. For reals and any ,
When , the coefficient is , so .
Descending eigendecomposition. For and , the predicate holds when all three of the following are true:
- , so is orthogonal;
- ;
- is non-increasing, meaning whenever .
Ties are allowed. No separate symmetry assumption on is stated.
VICReg optimum. Given , (no assumptions on either), and , this is the matrix built from the first columns of , each scaled:
If , the square root is and the whole -th column is .
Laplacian-Eigenmaps solution. For any (no assumptions) and , the predicate says that is feasible and optimal:
- Feasibility: , and every column of has unweighted sum zero, for every .
- Optimality: for every that satisfies the same two constraints,
If no matrix satisfies the constraints, no satisfies the predicate. Note that the zero-mean constraint is the plain column sum, not a -weighted sum.
Linear-VICReg matrix. For a natural number , reals , any , and ,
When , the coefficient is and .
Distance matrix. For an arbitrary function (no metric axioms assumed) and , the distance matrix has entries
Graph-estimation objective. For any , any function , and any ,
Entropic regulariser. For a real and any ,
The diagonal is excluded. Entries with contribute , because by convention. Negative entries are allowed, and for them means .
Graph set. is the set of all that satisfy all three of the following:
- ;
- for all ;
- for all .
These are the same three conditions as for a relation matrix.
Right-stochastic graph set. . These matrices are symmetric, nonnegative, zero-diagonal, and have every row summing to . For the set is empty: the only zero-diagonal matrix is , whose row sum is . For the set contains the empty matrix.
Row-stochastic set. is the set of all that satisfy all three of the following:
- for all ;
- for all ;
- for every .
Symmetry is not required. For this set is empty, and for it contains the empty matrix.
Exponential kernel. For a real and any ,
When , every off-diagonal entry equals .
Softmax kernel. For a real and any ,
For the denominators are positive. When , every off-diagonal entry is .
Cosine similarity. For ,
If or (in particular whenever ), the denominator is and the value is .
Cosine distance. For ,
This equals whenever or .
SimCLR similarity. For a real and , the matrix has entries
The exponent is (similarity, not distance). When , every off-diagonal entry is . Zero rows of have cosine similarity with everything.
Squared Frobenius norm. For ,
Linear-probe loss. For a natural number , , , and ,
Supervised (LDA) relation matrix. For a natural number and a labelling , let . The matrix has entries
Since itself has label , the count . The diagonal entries are , not . So this matrix does not have the zero diagonal required of a relation matrix above.
Within-class scatter. For natural numbers , , and labels ,
No centring and no normalisation are applied.
Between-class scatter. For natural numbers , , and labels ,
No centring and no normalisation are applied.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.