Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Sample-contrastive and dimension-contrastive criteria of an embedding matrix

Definition
ContrastiveDuality_Basic

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

contrastive-learningfrobenius-normgram-matrixself-supervised-learninguniform-sphere

Objects of Section 3 and Appendix C of Garrido, Chen, Bardes, Najman and LeCun (ICLR 2023).

  1. Embedding matrix. A batch of NNN embeddings of dimension MMM is a real matrix K∈RM×N\mathcal K\in\mathbb{R}^{M\times N}K∈RM×N whose iii-th column K⋅,i\mathcal K_{\cdot,i}K⋅,i​ is the embedding of the iii-th sample and whose jjj-th row Kj,⋅\mathcal K_{j,\cdot}Kj,⋅​ is the jjj-th embedding dimension across the batch. ∥K⋅,i∥2\|\mathcal K_{\cdot,i}\|_2∥K⋅,i​∥2​ and ∥Kj,⋅∥2\|\mathcal K_{j,\cdot}\|_2∥Kj,⋅​∥2​ are the Euclidean norms of a column and a row.
  2. Extracted diagonal (Definition 3.1). For A∈Rn×nA\in\mathbb{R}^{n\times n}A∈Rn×n, diag⁡(A)∈Rn×n\operatorname{diag}(A)\in\mathbb{R}^{n\times n}diag(A)∈Rn×n has entries diag⁡(A)i,i=Ai,i\operatorname{diag}(A)_{i,i}=A_{i,i}diag(A)i,i​=Ai,i​ and diag⁡(A)i,j=0\operatorname{diag}(A)_{i,j}=0diag(A)i,j​=0 for i≠ji\ne ji=j.
  3. Frobenius norm. ∥A∥F2=∑i,jAi,j2\|A\|_F^2=\sum_{i,j}A_{i,j}^2∥A∥F2​=∑i,j​Ai,j2​ for a real matrix AAA of any shape.
  4. Criteria (Definition 3.2). The sample-contrastive criterion is Lc=∥KTK−diag⁡(KTK)∥F2L_c=\|\mathcal K^T\mathcal K-\operatorname{diag}(\mathcal K^T\mathcal K)\|_F^2Lc​=∥KTK−diag(KTK)∥F2​, the sum of the squared off-diagonal entries of the Gram matrix KTK∈RN×N\mathcal K^T\mathcal K\in\mathbb{R}^{N\times N}KTK∈RN×N. The dimension-contrastive criterion is Lnc=∥KKT−diag⁡(KKT)∥F2L_{nc}=\|\mathcal K\mathcal K^T-\operatorname{diag}(\mathcal K\mathcal K^T)\|_F^2Lnc​=∥KKT−diag(KKT)∥F2​, the sum of the squared off-diagonal entries of KKT∈RM×M\mathcal K\mathcal K^T\in\mathbb{R}^{M\times M}KKT∈RM×M.
  5. Uniform law on the sphere (Lemma C.1). σD−1\sigma^{D-1}σD−1 is the uniform probability measure on the unit sphere SD−1⊂RDS^{D-1}\subset\mathbb{R}^DSD−1⊂RD, viewed as a measure on RD\mathbb{R}^DRD.

Formalization Note Matrices are indexed by {0,…,M−1}×{0,…,N−1}\{0,\dots,M-1\}\times\{0,\dots,N-1\}{0,…,M−1}×{0,…,N−1}. The uniform law is the normalised rotation-invariant surface measure of the sphere, built from Lebesgue measure by Mathlib's Measure.toSphere; for D=0D=0D=0 the sphere is empty and the construction gives the zero measure, a case no statement uses.

Definition code
import Mathlib

/-!
Garrido, Chen, Bardes, Najman and LeCun, *On the duality between contrastive and non-contrastive
self-supervised learning*, arXiv:2206.02574v3 (ICLR 2023): the extracted diagonal, the
sample-contrastive and dimension-contrastive criteria and the row and column norms of an
embedding matrix (Section 3, Definitions 3.1 and 3.2), and the uniform law on the unit sphere
(Appendix C, Lemma C.1).
-/

open MeasureTheory Matrix

noncomputable section

namespace ContrastiveDuality

/-- Definition 3.1 (Section 3, eq. (1), p. 3): the extracted diagonal `diag(A)` of a square
matrix `A`, which keeps the diagonal entries of `A` and sets every other entry to `0`. -/
def extractedDiag {n : ℕ} (A : Matrix (Fin n) (Fin n) ℝ) : Matrix (Fin n) (Fin n) ℝ :=
  Matrix.diagonal fun i => A i i

/-- The squared Frobenius norm `‖A‖_F² = ∑_{i,j} A_{i,j}²` used in Definition 3.2
(Section 3, p. 3). -/
def frobSq {m n : ℕ} (A : Matrix (Fin m) (Fin n) ℝ) : ℝ :=
  ∑ i, ∑ j, A i j ^ 2

/-- Section 3, p. 3: the Euclidean norm `‖K_{·,i}‖₂` of the `i`-th column of the embedding matrix
`K ∈ ℝ^{M×N}`, that is, of the embedding of the `i`-th sample of the batch. -/
def colNorm {M N : ℕ} (K : Matrix (Fin M) (Fin N) ℝ) (i : Fin N) : ℝ :=
  Real.sqrt (∑ j, K j i ^ 2)

/-- Section 3, p. 3: the Euclidean norm `‖K_{j,·}‖₂` of the `j`-th row of the embedding matrix
`K ∈ ℝ^{M×N}`, that is, of the `j`-th embedding dimension across the batch. -/
def rowNorm {M N : ℕ} (K : Matrix (Fin M) (Fin N) ℝ) (j : Fin M) : ℝ :=
  Real.sqrt (∑ i, K j i ^ 2)

/-- Definition 3.2 (Section 3, p. 3): the sample-contrastive (contrastive) criterion
`L_c = ‖KᵀK - diag(KᵀK)‖_F²` of an embedding matrix `K ∈ ℝ^{M×N}`. -/
def contrastiveCriterion {M N : ℕ} (K : Matrix (Fin M) (Fin N) ℝ) : ℝ :=
  frobSq (Kᵀ * K - extractedDiag (Kᵀ * K))

/-- Definition 3.2 (Section 3, p. 3): the dimension-contrastive (non-contrastive) criterion
`L_nc = ‖KKᵀ - diag(KKᵀ)‖_F²` of an embedding matrix `K ∈ ℝ^{M×N}`. -/
def nonContrastiveCriterion {M N : ℕ} (K : Matrix (Fin M) (Fin N) ℝ) : ℝ :=
  frobSq (K * Kᵀ - extractedDiag (K * Kᵀ))

/-- Lemma C.1 (Appendix C, p. 14): the uniform probability law `σ^{D-1}` on the unit sphere
`S^{D-1} ⊂ ℝ^D`, as a measure on `ℝ^D`: the rotation-invariant surface measure of the sphere
(Mathlib's `Measure.toSphere` of Lebesgue measure), pushed into `ℝ^D` and normalised to total
mass `1`. -/
def uniformSphere (D : ℕ) : Measure (EuclideanSpace ℝ (Fin D)) :=
  let μ := (volume : Measure (EuclideanSpace ℝ (Fin D))).toSphere.map Subtype.val
  (μ Set.univ)⁻¹ • μ

end ContrastiveDuality
Source
Garrido, Chen, Bardes, Najman and LeCun, On the duality between contrastive and non-contrastive self-supervised learning, arXiv:2206.02574v3 (ICLR 2023), Section 3, Definitions 3.1-3.2, eq. (1), p. 3; Appendix C, Lemma C.1, p. 14
Read-back

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

Extracted diagonal. For a natural number nnn and a real n×nn\times nn×n matrix AAA, with indices {0,…,n−1}\{0,\dots,n-1\}{0,…,n−1}, diag⁡(A)\operatorname{diag}(A)diag(A) is the real n×nn\times nn×n matrix

diag⁡(A)i,j  =  {Ai,iif i=j,0if i≠j.\operatorname{diag}(A)_{i,j} \;=\; \begin{cases} A_{i,i} & \text{if } i=j,\\ 0 & \text{if } i\neq j.\end{cases}diag(A)i,j​={Ai,i​0​if i=j,if i=j.​

It keeps the diagonal of AAA and sets every off-diagonal entry to 000. It is defined only for square matrices. When n=0n=0n=0 it is the empty matrix.

Squared Frobenius norm. For natural numbers m,nm,nm,n and a real m×nm\times nm×n matrix AAA,

∥A∥F2  =  ∑i=1m∑j=1nAi,j2.\|A\|_F^2 \;=\; \sum_{i=1}^{m}\sum_{j=1}^{n} A_{i,j}^2 .∥A∥F2​=i=1∑m​j=1∑n​Ai,j2​.

This is the plain sum of squared entries, with no square root taken. It equals 000 when m=0m=0m=0 or n=0n=0n=0.

Column norm. For natural numbers M,NM,NM,N, a real M×NM\times NM×N matrix KKK and a column index i∈{1,…,N}i\in\{1,\dots,N\}i∈{1,…,N},

∥K⋅,i∥2  =  ∑j=1MKj,i2.\|K_{\cdot,i}\|_2 \;=\; \sqrt{\textstyle\sum_{j=1}^{M} K_{j,i}^2}.∥K⋅,i​∥2​=∑j=1M​Kj,i2​​.

This is the Euclidean norm of the iii-th column. When M=0M=0M=0 the sum is empty and the value is 000.

Row norm. For natural numbers M,NM,NM,N, a real M×NM\times NM×N matrix KKK and a row index j∈{1,…,M}j\in\{1,\dots,M\}j∈{1,…,M},

∥Kj,⋅∥2  =  ∑i=1NKj,i2.\|K_{j,\cdot}\|_2 \;=\; \sqrt{\textstyle\sum_{i=1}^{N} K_{j,i}^2}.∥Kj,⋅​∥2​=∑i=1N​Kj,i2​​.

This is the Euclidean norm of the jjj-th row. When N=0N=0N=0 the value is 000.

Sample-contrastive criterion. For natural numbers M,NM,NM,N and a real M×NM\times NM×N matrix KKK,

Lc(K)  =  ∥K⊤K−diag⁡(K⊤K)∥F2  =  ∑i=1N∑i′=1i′≠iN(∑j=1MKj,iKj,i′)2.L_c(K) \;=\; \big\|K^\top K - \operatorname{diag}(K^\top K)\big\|_F^2 \;=\; \sum_{i=1}^{N}\sum_{\substack{i'=1\\ i'\neq i}}^{N} \Big(\sum_{j=1}^{M} K_{j,i}K_{j,i'}\Big)^2 .Lc​(K)=​K⊤K−diag(K⊤K)​F2​=i=1∑N​i′=1i′=i​∑N​(j=1∑M​Kj,i​Kj,i′​)2.

Here K⊤KK^\top KK⊤K is the N×NN\times NN×N Gram matrix of the columns of KKK.

  • The value is the sum, over all ordered pairs of distinct columns, of the squared inner product of those two columns.
  • Each unordered pair is therefore counted twice.
  • No normalisation of the columns or of the total is applied.
  • The value is 000 whenever N≤1N\le 1N≤1 or M=0M=0M=0.

Dimension-contrastive criterion. For natural numbers M,NM,NM,N and a real M×NM\times NM×N matrix KKK,

Lnc(K)  =  ∥KK⊤−diag⁡(KK⊤)∥F2  =  ∑j=1M∑j′=1j′≠jM(∑i=1NKj,iKj′,i)2.L_{nc}(K) \;=\; \big\|K K^\top - \operatorname{diag}(K K^\top)\big\|_F^2 \;=\; \sum_{j=1}^{M}\sum_{\substack{j'=1\\ j'\neq j}}^{M} \Big(\sum_{i=1}^{N} K_{j,i}K_{j',i}\Big)^2 .Lnc​(K)=​KK⊤−diag(KK⊤)​F2​=j=1∑M​j′=1j′=j​∑M​(i=1∑N​Kj,i​Kj′,i​)2.

Here KK⊤KK^\topKK⊤ is the M×MM\times MM×M Gram matrix of the rows of KKK.

  • The value is the sum, over all ordered pairs of distinct rows, of the squared inner product of those two rows.
  • Each unordered pair is counted twice.
  • No normalisation is applied.
  • The value is 000 whenever M≤1M\le 1M≤1 or N=0N=0N=0.

Uniform law on the sphere. For a natural number DDD, σD−1\sigma^{D-1}σD−1 is a measure on RD\mathbb{R}^DRD with its Euclidean structure, built in three steps.

  1. Take the standard surface measure μS\mu_SμS​ on the unit sphere SD−1={x∈RD:∥x∥=1}S^{D-1}=\{x\in\mathbb{R}^D:\|x\|=1\}SD−1={x∈RD:∥x∥=1} induced by Lebesgue measure. For a set A⊆SD−1A\subseteq S^{D-1}A⊆SD−1, it is DDD times the Lebesgue volume of the truncated cone {ra:a∈A, 0<r≤1}\{r a : a\in A,\ 0<r\le 1\}{ra:a∈A, 0<r≤1}.
  2. Push μS\mu_SμS​ forward along the inclusion SD−1↪RDS^{D-1}\hookrightarrow\mathbb{R}^DSD−1↪RD to get a measure μ\muμ on RD\mathbb{R}^DRD.
  3. Set
σD−1  =  (μ(RD))−1⋅μ,\sigma^{D-1} \;=\; \big(\mu(\mathbb{R}^D)\big)^{-1}\cdot \mu ,σD−1=(μ(RD))−1⋅μ,

where the inverse is taken in the extended non-negative reals [0,∞][0,\infty][0,∞].

For D≥1D\ge 1D≥1, the total mass μ(RD)\mu(\mathbb{R}^D)μ(RD) is finite and positive. Then σD−1\sigma^{D-1}σD−1 is a probability measure concentrated on SD−1S^{D-1}SD−1. For example, when D=1D=1D=1 it gives mass 12\tfrac1221​ to each of −1-1−1 and +1+1+1.

For D=0D=0D=0, the sphere is empty, because the only point of R0\mathbb{R}^0R0 has norm 000. Then μ\muμ is the zero measure, its total mass is 000, its inverse is ∞\infty∞, and ∞⋅0=0\infty\cdot 0=0∞⋅0=0. So σ−1\sigma^{-1}σ−1 is the zero measure and not a probability measure.

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