Projects / What stop-gradient does in SimSiam

What stop-gradient does in SimSiam

Jorge Tyrakowski

Multimodal Artificial Intelligence, National Taiwan University

Abstract

I re-implement SimSiam [1] on STL-10, pretraining a ResNet-18 on the 100k unlabeled images. A linear probe on the frozen encoder reaches 75.38% test accuracy with only 1,000 labels, against 48.85% for the same network trained from scratch. I reproduce four ablations from the paper and monitor collapse with the output standard deviation and the effective rank of the features, which reveals a transient dimensional collapse during training. Beyond the paper, I ask why stop-gradient prevents collapse: it cannot be relaxed (letting 1% of the gradient through already costs 5 points), but it can be replaced, since variance and covariance penalties on the normalized output match SimSiam without any stop-gradient (75.8% against 75.1%, two seeds).

What I did

Two augmented views x1,x2x_1, x_2 of an image pass through the same encoder ff (ResNet-18 and a projection MLP), giving z1,z2z_1, z_2, and a predictor hh gives pi=h(zi)p_i = h(z_i). The loss compares each prediction with the other view’s output, through a stop-gradient sg\mathrm{sg}:

L=12 D(p1,sg(z2))+12 D(p2,sg(z1)),D(p,z)=−p∥p∥⋅z∥z∥.\mathcal{L} = \tfrac{1}{2}\,\mathcal{D}\big(p_1, \mathrm{sg}(z_2)\big) + \tfrac{1}{2}\,\mathcal{D}\big(p_2, \mathrm{sg}(z_1)\big), \qquad \mathcal{D}(p, z) = -\frac{p}{\lVert p \rVert} \cdot \frac{z}{\lVert z \rVert}.

With no negatives and no momentum encoder, a constant output minimizes this loss (collapse). Besides the loss and a kNN monitor I log two collapse metrics every epoch: the per-channel standard deviation of z/∥z∥z / \lVert z \rVert, which is about 1/d=0.0221/\sqrt{d} = 0.022 when outputs are well spread and 0 under collapse, and the effective rank [3] of the backbone features, which also catches partial (dimensional) collapse. Every run uses the full protocol: 100 epochs of pretraining (about 39 minutes on one RTX 4090), then a linear probe on the 1,000 labeled images.

Results

Pretraining and linear probing

The loss reaches −0.850 and kNN accuracy 71.8%, matching the course’s reference curves, and a second seed gives the same numbers. The effective rank, however, falls from about 340 to about 150 right after warm-up, when the learning rate peaks, and only recovers as the cosine schedule lowers it (Figure 1). SimSiam goes through a phase of partial collapse that the loss curve does not show.

Four panels over 100 epochs: training loss, kNN accuracy, output standard deviation and effective rank, for two seeds.

SimSiam pretraining (two seeds): loss, kNN validation accuracy, output std and effective rank of the backbone features. The shaded band is the transient dimensional collapse.

Classification on STL-10 with 1,000 labels (course reference: 47.56% and 75.14%).

MethodVal acc. (%)Test acc. (%)
Supervised from scratch47.6048.85
SimSiam + linear probe (seed 0)76.2075.38
SimSiam + linear probe (seed 1)75.4074.71

Ablations from the paper

Ablations from the paper (100-epoch pretraining). std: per-channel std of the normalized output (0.022 when healthy); rank: effective rank of the backbone features; LP: test linear-probe accuracy (%).

VariantstdrankLPPaper (ImageNet)
Baseline.022025675.468.1
Without stop-gradient.0000819.10.1
No predictor.00005315.20.1
Fixed random predictor.0159633.71.5
Cross-entropy similarity.01808243.163.2
Asymmetric loss.022023472.164.8

Removing stop-gradient or the predictor collapses the output to a constant, as in the paper. The asymmetric loss costs 3.3 points, exactly the paper’s drop. A fixed random predictor keeps a healthy output std, but its backbone features collapse to rank 6: the two monitors measure different things (Figure 2).

kNN accuracy, output std and effective rank during pretraining for the baseline and five ablations.

Ablations during pretraining. Without stop-gradient or predictor the std goes to 0; the fixed predictor keeps a healthy std while its features collapse; cross-entropy slowly degrades.

The cross-entropy variant was the one disagreement with the paper (43.1% against 63.2%). Its loss rises back to exactly ln⁡2048\ln 2048, the entropy of a uniform distribution: weight decay shrinks the scale of the projector’s output from 1 to about 0.05, which the cosine ignores but which flattens the softmax. With that scale fixed to 1 it reaches 69.2%, close to the paper’s gap.

Beyond the paper: can stop-gradient be relaxed or replaced?

I let a fraction λ\lambda of the gradient flow through the target branch, z~=sg(z)+λ (z−sg(z))\tilde z = \mathrm{sg}(z) + \lambda\,(z - \mathrm{sg}(z)), which leaves the forward pass unchanged. Accuracy falls from the smallest leak I tried: 1% of the gradient costs 4.9 points, 5% costs 28, and from 20% on the model is fully collapsed (Figure 3, left). Then I removed stop-gradient entirely and added two terms on the normalized output d z/∥z∥\sqrt{d}\,z / \lVert z \rVert, in the spirit of VICReg [2]: a variance hinge that targets the healthy per-channel std, and the mean squared off-diagonal covariance, with weights fixed so that a random embedding pays about 0.01 in each.

Left: linear-probe accuracy against the gradient fraction lambda. Right: kNN accuracy curves for SimSiam, no stop-gradient, and no stop-gradient with variance and covariance terms.

Left: linear-probe accuracy against the fraction λ\lambda of gradient through the target (dotted: chance). Right: without stop-gradient, a variance term prevents complete collapse, and adding a covariance term matches SimSiam.

The variance term alone recovers 39 points (19.1% to 58.1%) but the dimensions stay correlated; adding the covariance term gives 75.8% without any stop-gradient, matching SimSiam (75.1%, two seeds each). A momentum target, in contrast, keeps stop-gradient and removes the transient dip in rank (Figure 4), with a small but consistent gain (76.0%).

Effective rank over 100 epochs for SimSiam, with an EMA target, and with variance and covariance terms, two seeds each.

Effective rank of the backbone features (two seeds each). SimSiam’s transient dip (shaded) disappears with a momentum target or with the variance and covariance terms.

Findings

  • Pretraining is worth 26 points with 1,000 labels: 75.4% for a linear probe against 48.9% from scratch.
  • Stop-gradient cannot be relaxed, but it can be replaced. It acts as a guard against both complete and dimensional collapse, and only for the similarity term: a covariance term added with stop-gradient bypasses it and collapses the model.
  • The effective rank is worth monitoring. It exposed the transient collapse in the baseline and the feature collapse of the fixed predictor, which the paper’s output-std metric misses.
  • A low loss can hide a shortcut: without color distortion the loss is lower than the baseline’s, but the features are worse than training from scratch (46.1%).

References

  1. X. Chen and K. He. Exploring simple Siamese representation learning. CVPR, 2021.
  2. A. Bardes, J. Ponce and Y. LeCun. VICReg: Variance-invariance-covariance regularization for self-supervised learning. ICLR, 2022.
  3. O. Roy and M. Vetterli. The effective rank: a measure of effective dimensionality. EUSIPCO, 2007.