---
title: 'SAG-VICReg: Stable & Global SSL'
url: https://www.emergentmind.com/topics/sag-vicreg
type: topic
---

# SAG-VICReg: Stable & Global SSL

SAG-VICReg (Stable and Generalizable VICReg) is a framework for self-supervised representation learning that modifies the Variance-Invariance-Covariance Regularization (VICReg) objective by injecting semantically meaningful cross-image positive pairs using a random-walk pairing mechanism. This approach targets both improved generalization to unseen data and enhanced capture of global semantic structures in the learned embeddings, addressing structural limitations inherent in VICReg's original formulation [2506.18104].

## 1. Background: VICReg and Spectral Embedding View

VICReg is a self-supervised learning (SSL) algorithm that learns image representations by optimizing three terms:

- **Invariance**: 
  \[
    s(Y,Y') = \frac{1}{n} \sum_{i=1}^n \|y_i - y_i'\|_2^2
  \]
  This term enforces that two random augmentations of the same image produce similar embeddings.

- **Variance**: 
  \[
    v(Y) = \frac{1}{k} \sum_{j=1}^k \max\left(0, \gamma - S(y^j, \epsilon)\right), \ \ S(y^j,\epsilon)=\sqrt{\mathrm{Var}(y^j) + \epsilon}
  \]
  It ensures a minimal spread in each embedding dimension, preventing collapse.

- **Covariance**: 
  \[
    c(Y) = \frac{1}{k} \sum_{p \neq q} [C(Y)]_{pq}^2
  \]
  This encourages decorrelation across embedding dimensions.

The combined loss function is:
\[
L_{\rm VICReg}(Y,Y') = \lambda s(Y,Y') + \mu\left[v(Y) + v(Y')\right] + \nu\left[c(Y) + c(Y')\right]
\]

VICReg can be interpreted as spectral embedding on a block-diagonal graph where edges connect only augmentations of the same image. Consequently, it does not capture global relationships between images, leading to sub-optimal representation of unseen data clusters and potentially distorted embeddings for out-of-distribution samples. This view is analogous to SpectralNet objectives but with the restriction that the graph only encodes instance-level locality.

## 2. Random-Walk Pairing: Injecting Global Structure

To overcome the absence of cross-image relationships in VICReg, SAG-VICReg extends the local, block-diagonal neighborhood by constructing semantically-informed, stochastic cross-image pairings using random walks on a learned affinity graph. The procedure is as follows:

1. Generate two batches of embeddings:
   \[
     Z = \{z_i\}_{i=1}^n,\quad Z' = \{z_i'\}_{i=1}^n
   \]
   Each derived from random augmentations.

2. Build an affinity matrix $W \in \mathbb{R}^{n \times n}$ based on pairwise cosine distances between $z_i$ and $z_j'$:
   \[
     d_{ij} = 1 - \cos(z_i, z_j')
   \]
   For each $i$, retain $k$ nearest neighbors (typically $k=5$), and set a local scale $\sigma_i$ as the 20th percentile of $\{d_{ij} - \min_j d_{ij}\}$:
   \[
     W_{ij} = 
     \begin{cases}
       \exp\left(-d_{ij}^2/\sigma_i^2\right) & j \in \mathrm{NN}_k(i) \\
       0 & \text{otherwise}
     \end{cases}
   \]

3. Define the normalized transition matrix for a one-step random walk:
   \[
     D_{ii} = \sum_j W_{ij}, \quad P = D^{-1} W
   \]
   $P_{ij}$ gives the probability of transitioning from $i$ to $j$.

4. Sample for each $i$ an index $j \sim P_{i,:}$ and set $z_i'' = z_j'$. The new batch $Z'' = \{z_i''\}_i$ comprises these cross-image positive pairs.

5. Define a weighted invariance term:
   \[
     s_{rw}(Z, Z'') = \frac{1}{n} \sum_{i=1}^n \sum_{j=1}^n P_{ij} \|z_i - z_j'\|^2
   \]
   (In implementation, one $j$ is sampled per $i$; the expectation matches the above expression.)

This process integrates semantically related cross-image pairs into the invariance objective, enriching the geometry underlying the embedding space.

## 3. The SAG-VICReg Objective

The loss function for SAG-VICReg retains the original variance and covariance regularization but replaces instance-only invariance with random-walk weighted invariance. The objective is:

\[
L_{\rm SAG}(Z,Z',Z'') = \frac{1}{n}\sum_{i,j} P_{ij} \|z_i - z_j'\|^2 + \mu[v(Z) + v(Z'')] + \nu[c(Z) + c(Z'')]
\]
or, grouping terms,
\[
L_{\rm SAG} = \lambda s_{rw}(Z, Z'') + \mu [v(Z) + v(Z'')] + \nu [c(Z) + c(Z'')]
\]
with the canonical hyperparameters $\lambda=25$, $\mu=25$, $\nu=1$.

The inclusion of $s_{rw}$ in place of instance-only $s$ promotes invariance not just among multiple views of the same image, but also across semantically proximate images, directly enhancing global semantic structure in the learned representation.

## 4. Standalone Evaluation Metric for Global Structure

To assess embedding quality on global semantic structure without requiring labels, SAG-VICReg introduces a dendrogram-based similarity metric:

- Use agglomerative clustering (Ward linkage, cosine distance) to construct dendrograms $T_A$ and $T_B$ for two embedding sets $A$ and $B$ (e.g., train and test splits).
- Compute Lowest Common Ancestor (LCA) distances for all pairs $(i,j)$: the dendrogram height where pair $(i,j)$ merges in $T_A$ yields $LCA_A(i,j)$ and analogously for $T_B$.
- Calculate Pearson, Spearman, and Kendall correlations between $\{LCA_A(i,j)\}$ and $\{LCA_B(i,j)\}$; high rank correlation indicates similar hierarchical structure.
- Assess cophenetic correlation via:
  \[
  c = \frac{\sum_{i<j} (D_A(i,j)-\bar D_A)(T_B(i,j)-\bar T_B)} 
           {\sqrt{\sum_{i<j} (D_A(i,j)-\bar D_A)^2}\,\sqrt{\sum_{i<j} (T_B(i,j)-\bar T_B)^2}}
  \]
  where $D_A(i,j)$ are the original distances, $T_B(i,j)$ are dendrogram distances.

This metric directly quantifies the preservation of global structure across domains or data splits in the absence of labels.

## 5. Algorithm and Hyperparameters

The SAG-VICReg procedure can be summarized as follows:

1. Apply two random augmentations to each batch sample, yielding $x_i, x_i'$.
2. Compute representations $y_i = f_\theta(x_i), y_i' = f_\theta(x_i')$ and embeddings $z_i = h_\phi(y_i), z_i' = h_\phi(y_i')$.
3. Form batches $Z = \{z_i\}, Z' = \{z_i'\}$.
4. Compute pairwise cosine distances $d_{ij} = 1 - \cos(z_i, z_j')$.
5. For each $i$, construct local scale $\sigma_i$ from the 20th percentile of $d_{i,:}$ and compute affinity $W_{ij}$ over $k=5$ nearest neighbors.
6. Normalize $W$ to transition matrix $P$.
7. For each $i$, sample $j \sim P_{i,:}$, set $z_i'' = z_j'$, yielding $Z'' = \{z_i''\}$.
8. Calculate weighted invariance $s_{rw}$, variances, and covariances on $Z$ and $Z''$.
9. Compute total loss: $L = \lambda s_{rw} + \mu [v(Z) + v(Z'')] + \nu [c(Z) + c(Z'')]$.
10. Backpropagate gradients and update parameters.

Hyperparameters: $\lambda=25$, $\mu=25$, $\nu=1$, batch size $n=256$, random-walk neighbors $k=5$, local scale via 20th percentile.

## 6. Empirical Results and Benchmarks

Evaluations on ImageNet-1k, CIFAR-100, and Caltech-256 reveal that SAG-VICReg consistently surpasses or matches VICReg and state-of-the-art SSL baselines on both label-free and conventional structure-preserving metrics:

- LCA Similarity (Spearman): SAG-VICReg achieves 0.433 on ImageNet versus VICReg's 0.403 (+7%), and 0.296 on CIFAR-100 versus VICReg's 0.223 (+33%).
- Cophenetic similarity: On CIFAR-100 D1$\rightarrow$P2, SAG-VICReg yields 0.405 compared to the next best 0.349 (+16%).
- Hierarchical Rand Index: Superior results at all hierarchy levels, with larger gains at intermediate granularity.
- Hierarchical $k$-NN and linear probes: Best or competitive performance on both coarse and fine levels, indicating no compromise in local discriminability.
- Masking-based comparisons on CIFAR-100: Outperforms DINO, MAE, and I-JEPA in rank correlations despite differing architectural design philosophies.

These results indicate that the random-walk pairing mechanism meaningfully enhances global semantic coherence and stability of the learned representations, an effect captured distinctly by the proposed global-structure metric.

## 7. Significance and Implications

By integrating a principled random-walk–based extension over the original block-diagonal affinity graph of VICReg, SAG-VICReg addresses limitations in generalization and semantic fidelity for self-supervised representations. The approach leverages spectral embedding theory to diagnose sub-optimalities and prescribes an efficient and differentiable modification applicable in current deep learning workflows. The dendrogram-based metric facilitates objective, label-free benchmarking for global embedding structure, of practical value where annotated data is unavailable. A plausible implication is that random-walk pairing or similar spectral enrichment mechanisms may serve as a general template for augmenting contrastive and non-contrastive SSL objectives to better capture data manifold geometry, irrespective of domain [2506.18104].

Source: https://www.emergentmind.com/topics/sag-vicreg