Linear BYOL/SimSiam model: objective, augmentation moments, gradient flows and reduced dynamics
DefinitionSSLWithoutPairs_BasicObjects of Sections 2 and 3 and Appendices A and B of Tian, Chen and Ganguli (2021), for the bias-free linear BYOL/SimSiam model.
- Norms and eigenvalues. ; ; is the least real root of the characteristic polynomial of a square real matrix (used only for symmetric ).
- Trajectories. A matrix trajectory , , has derivative if every entry has that derivative within at every (one-sided at ).
- Data and augmentation (Section 2). The data has law and the augmentation is a Markov kernel . Then , and .
- Objective (eq. 1). For and , , where and are independent given . The target weight is a separate argument, which encodes the stop-gradient.
- Flows (eqs. 2, 3). and .
- No-stop-gradient matrix (Theorem 2). with , acting on (columns stacked).
- Reduced flows (Section 3). Under Assumptions 1 and 2 (, , , ) the predictor flow is (eq. 62). The symmetrised flow (eq. 7) is , , with . The commutator is , and
- Modes (Section 3.2). The right sides of eqs. (11), (12), (16) are , and . The fixed points are , and .
Formalization Note Vectors of are functions , and the Euclidean norm is written out, since Lean's default norm on this type is the sup norm. Derivatives of matrix trajectories are taken entrywise. is a Bochner integral against . Statements that use assume finite second moments, so that this integral is not a junk value.
import Mathlib
/-!
Tian, Chen and Ganguli, *Understanding Self-Supervised Learning Dynamics without Contrastive
Pairs*, arXiv:2102.06810v4 (ICML 2021): the bias-free linear BYOL/SimSiam model of Section 2
and Appendix A (objective, augmentation moments `X`, `X'`, gradient flows), the reduced dynamics
of Section 3 and Appendix B under Assumptions 1-3 (eqs. (7), (9), (11)-(13), (16), (17), (62)),
and the norms and minimal eigenvalue used in Theorems 2 and 3 and Lemma 2.
-/
open MeasureTheory ProbabilityTheory Matrix
noncomputable section
namespace SSLWithoutPairs
/-- Squared Euclidean norm `‖v‖₂²` of a vector of `ℝⁿ` (Section 2, eq. (1)). -/
def sqNorm {ι : Type*} [Fintype ι] (v : ι → ℝ) : ℝ := ∑ i, v i ^ 2
/-- Euclidean norm `‖v‖₂` of a vector (Lemma 2, Appendix A, p. 14). -/
def l2Norm {ι : Type*} [Fintype ι] (v : ι → ℝ) : ℝ := Real.sqrt (sqNorm v)
/-- Frobenius norm `‖A‖_F` of a real matrix (Theorem 3, eq. (10), p. 5). -/
def frobNorm {m n : Type*} [Fintype m] [Fintype n] (A : Matrix m n ℝ) : ℝ :=
Real.sqrt (∑ i, ∑ j, A i j ^ 2)
/-- The minimal eigenvalue `λ_min(A)` of a square real matrix (Theorems 2 and 3, Lemma 2):
the least real root of its characteristic polynomial. It is only applied to symmetric matrices,
whose eigenvalues are all real. -/
def minEig {n : Type*} [Fintype n] [DecidableEq n] (A : Matrix n n ℝ) : ℝ :=
sInf {μ : ℝ | A.charpoly.IsRoot μ}
/-- A matrix-valued trajectory `A(t)`, `t ≥ 0`, has time derivative `Ȧ(t) = A'(t)` for every
`t ≥ 0` (one-sided at `t = 0`), entrywise. This is how the gradient flows of Section 2 and the
reduced flows of Section 3 are read. -/
def HasMatDerivOn {m n : Type*} (A A' : ℝ → Matrix m n ℝ) : Prop :=
∀ t : ℝ, 0 ≤ t → ∀ i j, HasDerivWithinAt (fun s => A s i j) (A' t i j) (Set.Ici 0) t
section Moments
variable {n : ℕ}
/-- Section 2, p. 3: the average augmented view `x̄(x) = E_{x' ∼ p_aug(·|x)}[x']` of a data
point `x`, for the augmentation kernel `κ = p_aug`. -/
def augMean (κ : Kernel (Fin n → ℝ) (Fin n → ℝ)) (x : Fin n → ℝ) : Fin n → ℝ :=
fun i => ∫ y, y i ∂(κ x)
/-- Section 2, p. 3: `X = E_x[x̄(x) x̄(x)ᵀ]`, for the data distribution `μ = p(x)`. -/
def augMeanCorr (μ : Measure (Fin n → ℝ)) (κ : Kernel (Fin n → ℝ) (Fin n → ℝ)) :
Matrix (Fin n) (Fin n) ℝ :=
fun i j => ∫ x, augMean κ x i * augMean κ x j ∂μ
/-- Section 2, p. 3: `X' = E_x[Var_{x'|x}[x']]`, the covariance of the augmented views of `x`,
averaged over the data. -/
def augCov (μ : Measure (Fin n → ℝ)) (κ : Kernel (Fin n → ℝ) (Fin n → ℝ)) :
Matrix (Fin n) (Fin n) ℝ :=
fun i j => ∫ x, (∫ y, (y i - augMean κ x i) * (y j - augMean κ x j) ∂(κ x)) ∂μ
end Moments
/-- Eq. (1) (Appendix A, eq. (20)): the BYOL objective
`J = ½ E_{x ∼ p, x₁, x₂ ∼ p_aug(·|x)} ‖W_p W x₁ - W_a x₂‖²`, with the two views drawn
independently given `x`. The target weight `W_a` is a separate argument: the stop-gradient means
that `J` is differentiated in `W` and `W_p` with `W_a` held fixed (for SimSiam `W_a = W`). -/
def byolLoss {n₁ n₂ : ℕ} (μ : Measure (Fin n₁ → ℝ)) (κ : Kernel (Fin n₁ → ℝ) (Fin n₁ → ℝ))
[IsSFiniteKernel κ] (Wp : Matrix (Fin n₂) (Fin n₂) ℝ) (W Wa : Matrix (Fin n₂) (Fin n₁) ℝ) :
ℝ :=
(1 / 2 : ℝ) * ∫ q, sqNorm (Wp *ᵥ (W *ᵥ q.2.1) - Wa *ᵥ q.2.2) ∂(μ ⊗ₘ (κ ×ₖ κ))
/-- Eq. (2): the right side of the predictor flow
`Ẇ_p = α_p (-W_p W (X + X') + W_a X) Wᵀ - η W_p`. -/
def byolFlowWp {n₁ n₂ : ℕ} (αp η : ℝ) (X X' : Matrix (Fin n₁) (Fin n₁) ℝ)
(Wp : Matrix (Fin n₂) (Fin n₂) ℝ) (W Wa : Matrix (Fin n₂) (Fin n₁) ℝ) :
Matrix (Fin n₂) (Fin n₂) ℝ :=
αp • ((-(Wp * W * (X + X')) + Wa * X) * Wᵀ) - η • Wp
/-- Eq. (3): the right side of the online flow `Ẇ = W_pᵀ (-W_p W (X + X') + W_a X) - η W`. -/
def byolFlowW {n₁ n₂ : ℕ} (η : ℝ) (X X' : Matrix (Fin n₁) (Fin n₁) ℝ)
(Wp : Matrix (Fin n₂) (Fin n₂) ℝ) (W Wa : Matrix (Fin n₂) (Fin n₁) ℝ) :
Matrix (Fin n₂) (Fin n₁) ℝ :=
Wpᵀ * (-(Wp * W * (X + X')) + Wa * X) - η • W
/-- Theorem 2, p. 3: `H = X' ⊗ (W_pᵀ W_p + I_{n₂}) + X ⊗ W̃_pᵀ W̃_p + η I_{n₁ n₂}` with
`W̃_p = W_p - I_{n₂}` and `⊗` the Kronecker product, indexed so that it acts on
`vec(W)` (`Matrix.vec`, columns stacked). -/
def noStopH {n₁ n₂ : ℕ} (X X' : Matrix (Fin n₁) (Fin n₁) ℝ) (η : ℝ)
(Wp : Matrix (Fin n₂) (Fin n₂) ℝ) : Matrix (Fin n₁ × Fin n₂) (Fin n₁ × Fin n₂) ℝ :=
Matrix.kronecker X' (Wpᵀ * Wp + 1) + Matrix.kronecker X ((Wp - 1)ᵀ * (Wp - 1)) + η • 1
/-- Eq. (62) (Appendix B, p. 15): the predictor flow of eq. (2) under Assumptions 1 and 2
(`W_a = τ W`, `X = I`, `X' = σ² I`, `F = W Wᵀ`):
`Ẇ_p = -α_p (1 + σ²) W_p F + α_p τ F - η W_p`. -/
def isoFlowWp {d : ℕ} (αp σ η τ : ℝ) (Wp F : Matrix (Fin d) (Fin d) ℝ) :
Matrix (Fin d) (Fin d) ℝ :=
-(αp * (1 + σ ^ 2)) • (Wp * F) + (αp * τ) • F - η • Wp
/-- Eq. (7), first line: the symmetrised predictor flow
`Ẇ_p = -(α_p/2)(1 + σ²){W_p, F} + α_p τ F - η W_p`, `{A, B} = AB + BA`. -/
def symFlowWp {d : ℕ} (αp σ η τ : ℝ) (Wp F : Matrix (Fin d) (Fin d) ℝ) :
Matrix (Fin d) (Fin d) ℝ :=
-(αp / 2 * (1 + σ ^ 2)) • (Wp * F + F * Wp) + (αp * τ) • F - η • Wp
/-- Eq. (7), second line: `Ḟ = -(1 + σ²){W_p², F} + τ{W_p, F} - 2η F`. -/
def symFlowF {d : ℕ} (σ η τ : ℝ) (Wp F : Matrix (Fin d) (Fin d) ℝ) :
Matrix (Fin d) (Fin d) ℝ :=
-(1 + σ ^ 2) • (Wp * Wp * F + F * (Wp * Wp)) + τ • (Wp * F + F * Wp) - (2 * η) • F
/-- Theorem 3: the commutator `[F, W_p] = F W_p - W_p F`. -/
def commutator {d : ℕ} (F Wp : Matrix (Fin d) (Fin d) ℝ) : Matrix (Fin d) (Fin d) ℝ :=
F * Wp - Wp * F
/-- Eq. (9): `K = (1 + σ²)[(α_p/2) F + W_p² - τ/(1 + σ²) W_p] + (3/2) η I`. -/
def alignK {d : ℕ} (αp σ η τ : ℝ) (F Wp : Matrix (Fin d) (Fin d) ℝ) :
Matrix (Fin d) (Fin d) ℝ :=
(1 + σ ^ 2) • ((αp / 2) • F + Wp * Wp - (τ / (1 + σ ^ 2)) • Wp) + ((3 / 2 : ℝ) * η) • 1
/-- Eq. (11): the decoupled predictor mode `ṗ_j = α_p s_j [τ - (1 + σ²) p_j] - η p_j`. -/
def modeFlowP (αp σ η τ p s : ℝ) : ℝ := αp * s * (τ - (1 + σ ^ 2) * p) - η * p
/-- Eq. (12): the decoupled online mode `ṡ_j = 2 p_j s_j [τ - (1 + σ²) p_j] - 2η s_j`. -/
def modeFlowS (σ η τ p s : ℝ) : ℝ := 2 * p * s * (τ - (1 + σ ^ 2) * p) - 2 * η * s
/-- Eq. (16): the predictor mode on the invariant parabola `s_j = α_p⁻¹ p_j²`, at fixed `τ`:
`ṗ_j = p_j² [τ - (1 + σ²) p_j] - η p_j`. -/
def parabolaFlowP (σ η τ p : ℝ) : ℝ := p ^ 2 * (τ - (1 + σ ^ 2) * p) - η * p
/-- Section 3.2, p. 6: the fixed point `p*_{j-} = (τ - √(τ² - 4η(1 + σ²))) / (2(1 + σ²))`
of eq. (16). -/
def fixedPointMinus (σ η τ : ℝ) : ℝ :=
(τ - Real.sqrt (τ ^ 2 - 4 * η * (1 + σ ^ 2))) / (2 * (1 + σ ^ 2))
/-- Section 3.2, p. 6: the fixed point `p*_{j+} = (τ + √(τ² - 4η(1 + σ²))) / (2(1 + σ²))`
of eq. (16). -/
def fixedPointPlus (σ η τ : ℝ) : ℝ :=
(τ + Real.sqrt (τ ^ 2 - 4 * η * (1 + σ ^ 2))) / (2 * (1 + σ ^ 2))
/-- Section 3.2, p. 6: `Δ_j = p_j [τ - (1 + σ²) p_j] - η`. -/
def modeDelta (σ η τ p : ℝ) : ℝ := p * (τ - (1 + σ ^ 2) * p) - η
end SSLWithoutPairs
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Squared norm sqNorm. For a vector with real entries indexed by an arbitrary finite index set , this is
If is empty, the value is .
Euclidean norm l2Norm. For a real vector indexed by a finite set , this is , the non-negative square root of the squared norm above. It is when is empty.
Frobenius norm frobNorm. For a real matrix with rows indexed by a finite set and columns by a finite set , this is
It is if either index set is empty.
Minimal eigenvalue minEig. For a square real matrix indexed by a finite set (with decidable equality on indices), let be its characteristic polynomial. The definition is
the infimum of the set of real roots of . Points to note:
- is monic, so this set is finite. When it is nonempty, the infimum is the least real eigenvalue of , and no symmetry of is assumed.
- If has no real eigenvalues, the set is empty. This can happen for non-symmetric , and always happens when is empty, where . In that case the real infimum of the empty set is taken by convention to be , so .
- Complex eigenvalues are ignored entirely.
Derivative of a matrix trajectory HasMatDerivOn. Let be functions from to real matrices, with arbitrary index sets . The proposition asserts:
- for every and every pair of indices , the scalar function has derivative at , taken within the half-line .
For this is the ordinary two-sided derivative. At it is the right derivative. The condition says nothing about or for , beyond what is needed to evaluate derivatives inside .
Average augmented view augMean. Here is any natural number, possibly , and is a measurable kernel assigning to each a measure on . For , is the vector with components
Points to note:
- is not required to be a probability measure, so this is an expectation only when has total mass .
- If is not -integrable, the component is by convention.
Moment matrix , augMeanCorr. For a measure on (not required to be a probability measure) and a kernel as above, is the matrix with entries
Here is the average augmented view defined above. Any entry whose integrand is not -integrable is by convention.
Moment matrix , augCov. For and as above, is the matrix with entries
Points to note:
- Neither nor is required to be a probability measure. So the inner integral is a covariance of , and the outer one an average over , only when both have total mass .
- Non-integrable inner or outer integrals are replaced by .
BYOL objective byolLoss. The inputs are:
- natural numbers ;
- a measure on ;
- an s-finite kernel from to ;
- an real matrix ;
- two real matrices and .
The value is
The integration measure is defined as follows. First draw according to . Then, given , draw the pair according to the product measure , so the two views are independent given . The integrand depends only on and , not directly on .
Points to note:
- The value is a plain integral. No normalization is applied, and and need not be probability measures.
- If the integrand is not integrable, the value is by convention.
- The definition only gives the value of at the given matrices. It encodes no differentiation and no relation between and .
Predictor flow field byolFlowWp. For real numbers , arbitrary real matrices , an matrix , and matrices , this is the matrix
Here are free matrix arguments. They are not tied to the moment matrices defined above. No sign or positivity is assumed for or .
Online flow field byolFlowW. For a real number , arbitrary real matrices , an matrix , and matrices , this is the matrix
No learning-rate factor multiplies the first term. Again, are arbitrary and is unrestricted.
Matrix , noStopH. The inputs are real matrices , a real number , and an real matrix . Write . is the square matrix indexed by pairs with and :
where is the Kronecker product. Entrywise,
The first component of each index pair ranges over the -indices. The definition only specifies this matrix and does not itself connect it to any flow or vectorization. are arbitrary and is unrestricted.
Isotropic predictor flow field isoFlowWp. For a natural number , real numbers , and real matrices , this is
is an arbitrary matrix: it is not required to equal , nor to be symmetric. enters only through .
Symmetrized predictor flow field symFlowWp. For a natural number , real numbers , and arbitrary real matrices , this is
Symmetrized flow field symFlowF. For a natural number , real numbers , and arbitrary real matrices , this is
No appears in it.
Commutator commutator. For real matrices , this is
Matrix , alignK. For a natural number , real numbers , and arbitrary real matrices , this is
Since , there is no division by zero. The expression equals .
Predictor mode field modeFlowP. For real numbers , this is the real number
Online mode field modeFlowS. For real numbers , this is the real number
No appears in it.
Parabola predictor field parabolaFlowP. For real numbers , this is the real number
It does not involve or . The definition encodes no relation of the form . It is simply this cubic expression in .
Lower fixed point fixedPointMinus. For real numbers , this is
Here is the real square root, which returns for negative arguments. So when , the value is ; no complex root and no undefined value occurs. The denominator is always at least . No sign conditions are imposed on or .
Upper fixed point fixedPointPlus. For real numbers , this is
The same conventions apply as for the lower fixed point. In particular, when , the value is , which coincides with .
Mode quantity , modeDelta. For real numbers , this is the real number
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.