VICReg
VICReg
Keep views consistent without erasing batch variation; derive and test all three penalties.
vicregStart with the problem
Two photographs of one bicycle should produce similar features. Making every photograph produce the same feature would satisfy that instruction perfectly—and destroy the representation. VICReg adds two further requirements: each coordinate must vary across examples, and different coordinates should avoid redundant covariance. These are three separate pressures, not three names for the same loss. The method was presented by Bardes, Ponce and LeCun at ICLR 2022. VICReg: Variance-Invariance-Covariance Regularization for Self-Supervised Learning — arXiv v3
Picture a class spreadsheet. Matching two views is like making two measurements of each pupil agree. Maintaining variance stops every pupil receiving an identical row. Penalizing covariance discourages columns that repeat the same measurement. None of these checks says that a column must mean “age” or “bicycle wheel.” Statistical spread is not a semantic label.
This chapter derives a small, fully differentiated objective. It makes the batch normalization convention explicit, then tests an awkward boundary: a constant batch can have a positive variance penalty and exactly zero gradient.
Definitions and derivation
Let the two feature tables be , with . Center a table by subtracting its column means. We use sample covariance, an added inside the square root, and target standard deviation one:
Our invariance term averages all entries. The variance term averages across both branches; the covariance term sums the branches. This is a declared teaching normalization: copying the paper’s numerical coefficients without checking these factors would change the relative weights.
Differentiate one branch. The mean subtraction contributes the centering operator; it disappears from the final expression because the centered columns sum to zero. Write for covariance with its diagonal set to zero:
The gradient has the opposite invariance term and its own variance/covariance terms. At the hinge boundary the code chooses zero. For a shared linear encoder, , the parameter gradient is . Forming the dense covariance costs operations and extra storage. A distributed implementation must decide whether statistics cover the local or global batch; that changes the objective. The three regularizers follow the original formulation; the explicit averaging above defines this example. VICReg: Variance-Invariance-Covariance Regularization for Self-Supervised Learning — arXiv v3
Run the calculation
Run with CPython 3.12.4 and NumPy 1.26.4. Four orthogonal sign patterns form the first view; coordinate rescaling forms the second. Nine shared weights receive 600 updates at learning rate 0.003. The loss falls from 15.934080915 to 0.159309453. Its unweighted components end near 0.006270174, 0 and 0.002555108. This is optimization on one constructed batch, with no image data or downstream evaluation.
import numpy as np
def loss_gradient(a,b):
a,b=np.asarray(a,dtype=float),np.asarray(b,dtype=float)
if a.ndim!=2 or a.shape!=b.shape or not 2<=len(a)<=16 or not 1<=a.shape[1]<=8 or not np.isfinite(a).all() or not np.isfinite(b).all():raise ValueError('Use matching finite batches with at least two rows.')
n,d=a.shape;invariance=float(np.mean((a-b)**2));ga=50*(a-b)/(n*d);gb=-ga;variance=0.;covariance=0.
for x,g in ((a,ga),(b,gb)):
centered=x-x.mean(axis=0);std=np.sqrt(np.sum(centered**2,axis=0)/(n-1)+1e-4);variance+=.5*np.mean(np.maximum(0,1-std));g-=25*centered*(std<1)/(2*d*(n-1)*std)
cov=centered.T@centered/(n-1);np.fill_diagonal(cov,0);covariance+=np.sum(cov**2)/d;g+=4*centered@cov/((n-1)*d)
return 25*invariance+25*variance+covariance,ga,gb,np.array([invariance,variance,covariance])
def views():
x=np.array([[1.,1,1],[1,-1,-1],[-1,1,-1],[-1,-1,1]])
return x,x*np.array([1.1,.9,1.05])
def train(steps=600):
a,b=views();w=.3*np.array([[1.,.3,0],[.1,1,.2],[.3,-.1,1]]);start=loss_gradient(a@w,b@w)[0]
for _ in range(steps):loss,ga,gb,parts=loss_gradient(a@w,b@w);w-=.003*(a.T@ga+b.T@gb)
return w,start,loss_gradient(a@w,b@w)
if __name__=='__main__':
w,start,(end,ga,gb,parts)=train();a,b=views();print('VICReg-style objective before/after',round(start,9),round(end,9),'unweighted invariance/variance/covariance',parts.round(9).tolist());print('view-A sample standard deviations',np.std(a@w,axis=0,ddof=1).round(9).tolist())
c=np.ones((4,3));loss,ga,gb,parts=loss_gradient(c,c);print('constant objective',loss,'gradient norms',float(np.linalg.norm(ga)),float(np.linalg.norm(gb)))
VICReg-style objective before/after 15.934080915 0.159309453 unweighted invariance/variance/covariance [0.006270174, 0.0, 0.002555108]
view-A sample standard deviations [1.009271848, 1.11470797, 1.001057148]
constant objective 24.749999999999996 gradient norms 0.0 0.0
When the method fails
For identical constant tables, the stabilized standard deviation is 0.01. The objective is therefore 24.75, yet every centered entry is zero and both gradients vanish. A penalty against collapse is not a proof that gradient descent escapes an exactly collapsed stationary point. Random initialization and the surrounding training dynamics matter.
Batch size also constrains what can happen: centered covariance has rank at most . If , every coordinate cannot simultaneously have nonzero variance and zero pairwise covariance. Small batches make estimates noisy even before that rank limit becomes decisive.
VICReg’s covariance penalty controls second moments. Zero covariance does not establish independence, and positive variance does not guarantee useful features. Compare Barlow Twins, which drives cross-correlation toward the identity, with VICReg’s separate within-view covariance penalties. Neither removes the need to choose augmentations that preserve the information a task needs. Barlow Twins: Self-Supervised Learning via Redundancy Reduction
Exercises with answers
- Why is a one-row batch rejected?
Answer 1: Sample covariance divides by N−1, which would be zero.
- Compute the constant-table penalty with epsilon 0.0001.
Answer 2: Each standard deviation is 0.01; I=K=0, V=0.99 and L=24.75.
- Can three centered examples have four uncorrelated nonconstant coordinates?
Answer 3: No. Their covariance rank is at most two; a positive diagonal four-dimensional covariance has rank four.
- How should the shared-encoder gradient be checked?
Answer 4: Perturb each shared weight and recompute both branches. Perturbing only one branch checks a different function.
Sources and next steps
Read the VICReg paper’s loss definitions with its normalization constants visible. Then inspect the Barlow Twins cross-correlation objective to identify which statistics couple the views. Our example adds no negative samples, target teacher or stop-gradient branch. To extend it, vary batch size and feature dimension separately and report variance, covariance and downstream accuracy as separate measurements. VICReg: Variance-Invariance-Covariance Regularization for Self-Supervised Learning — arXiv v3; Barlow Twins: Self-Supervised Learning via Redundancy Reduction
Make a prediction, then check
A constant batch has a positive variance penalty. What follows?
Learning objectives
- Derive feature and shared-encoder gradients with an explicit variance convention.
- Explain constant stationary points and finite-batch rank limits.
Prerequisites & connections
Explore related knowledge →References
- VICReg: Variance-Invariance-Covariance Regularization for Self-Supervised Learning — arXiv v3
Adrien Bardes; Jean Ponce; Yann LeCun · 2022 - Barlow Twins: Self-Supervised Learning via Redundancy Reduction
Jure Zbontar; Li Jing; Ishan Misra; Yann LeCun; Stéphane Deny · 2021
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-07 · Codex single-AI-author technical check: primary-source scope, explicit assumptions, original calculations, independent mathematical references and bilingual tests; not independent human or second-agent review.
- Single-AI-author technical check: bounded primary-source reading, explicit assumptions, original calculations, runnable examples and matching bilingual equations, code and exercises. Not an independent human or second-agent review.
- Four focused CPython 3.12.4 tests passed per chapter: finite-difference gradients, sample covariance, teacher/center update order, hidden-input isolation, independent least squares, PSD projection, margin feasibility, tied-branch derivatives and transported factor actions as applicable, plus bilingual structure. All eight programs use NumPy 1.26.4; teaching calculations, not image benchmarks.
- Promoted the full prose, equations, code and exercises. Tests read formal MDX only; removed temporary fallbacks/draft skips and updated review assertion to checked. Algorithm and independent references are unchanged; final record binds both locale hashes.
Source locations checked:
- VICReg: Variance-Invariance-Covariance Regularization for Self-Supervised Learning — arXiv v3 · Section 3 variance, invariance and covariance definitions; stabilized standard deviations and objective weighting.
- Barlow Twins: Self-Supervised Learning via Redundancy Reduction · Section 2 cross-correlation objective and diagonal/off-diagonal terms.
Related topics