Sample-contrastive and dimension-contrastive criteria of an embedding matrix
DefinitionContrastiveDuality_BasicObjects of Section 3 and Appendix C of Garrido, Chen, Bardes, Najman and LeCun (ICLR 2023).
- Embedding matrix. A batch of embeddings of dimension is a real matrix whose -th column is the embedding of the -th sample and whose -th row is the -th embedding dimension across the batch. and are the Euclidean norms of a column and a row.
- Extracted diagonal (Definition 3.1). For , has entries and for .
- Frobenius norm. for a real matrix of any shape.
- Criteria (Definition 3.2). The sample-contrastive criterion is , the sum of the squared off-diagonal entries of the Gram matrix . The dimension-contrastive criterion is , the sum of the squared off-diagonal entries of .
- Uniform law on the sphere (Lemma C.1). is the uniform probability measure on the unit sphere , viewed as a measure on .
Formalization Note Matrices are indexed by . The uniform law is the normalised rotation-invariant surface measure of the sphere, built from Lebesgue measure by Mathlib's Measure.toSphere; for the sphere is empty and the construction gives the zero measure, a case no statement uses.
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
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Extracted diagonal. For a natural number and a real matrix , with indices , is the real matrix
It keeps the diagonal of and sets every off-diagonal entry to . It is defined only for square matrices. When it is the empty matrix.
Squared Frobenius norm. For natural numbers and a real matrix ,
This is the plain sum of squared entries, with no square root taken. It equals when or .
Column norm. For natural numbers , a real matrix and a column index ,
This is the Euclidean norm of the -th column. When the sum is empty and the value is .
Row norm. For natural numbers , a real matrix and a row index ,
This is the Euclidean norm of the -th row. When the value is .
Sample-contrastive criterion. For natural numbers and a real matrix ,
Here is the Gram matrix of the columns of .
- 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 whenever or .
Dimension-contrastive criterion. For natural numbers and a real matrix ,
Here is the Gram matrix of the rows of .
- 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 whenever or .
Uniform law on the sphere. For a natural number , is a measure on with its Euclidean structure, built in three steps.
- Take the standard surface measure on the unit sphere induced by Lebesgue measure. For a set , it is times the Lebesgue volume of the truncated cone .
- Push forward along the inclusion to get a measure on .
- Set
where the inverse is taken in the extended non-negative reals .
For , the total mass is finite and positive. Then is a probability measure concentrated on . For example, when it gives mass to each of and .
For , the sphere is empty, because the only point of has norm . Then is the zero measure, its total mass is , its inverse is , and . So is the zero measure and not a probability measure.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.