Foundations to Frontier
Tests
E. Deep Learning › E16 Self-Supervised and Contrastive Learning

SwAV

SwAV (Swapping Assignments between Views; Caron and colleagues, NeurIPS 2020) combines clustering with contrastive ideas. Each view's embedding is softly assigned to a set of learnable prototypes, and the network must predict one view's assignment (its "code") from the other view. The codes are computed online with the Sinkhorn–Knopp algorithm, which forces every batch to spread evenly over the prototypes. That equipartition constraint is what prevents the trivial solution of assigning everything to one cluster. SwAV also introduced multi-crop augmentation. This page covers prototypes, swapped prediction, Sinkhorn equipartition and multi-crop. In the exercises you implement Sinkhorn and the swapped loss, then train SwAV with and without equipartition. With Sinkhorn, the probe reached 0.76 and about 17 of 20 prototypes stayed in use. Without it, everything fell into one prototype and the probe dropped to 0.31 (three seeds agree).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Cluster one view, predict the cluster from the other

SwAV keeps a set of learnable “prototypes,” like cluster centres. Each altered view of an image is assigned to these prototypes, and the network learns to predict the assignment of one view from the other view. To stop the network from dumping every image into one cluster, each batch must be spread evenly across the prototypes, which is enforced by a quick balancing algorithm called Sinkhorn. The demo shows the kind of collapse this balancing prevents.

Self-supervised learning lab
Effective rank 15.6 of 16 dimensions. Non-contrastive methods need stop-gradients, predictors, centring or variance terms to keep the spectrum from collapsing to a few directions.
Why engineers care

Clustering-based self-supervision discovers groups in unlabelled data (terrain types, defect families, flight regimes) while learning features, and its balanced assignments avoid one-cluster collapse.

📝 Quick check (Level 0)
0/3 answered
  1. 1. SwAV predicts…
  2. 2. Prototypes are…
  3. 3. Sinkhorn balancing prevents…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Scores: for normalised embeddings z and K normalised prototypes c_k, the score is s_k = zᵀc_k.
  • Codes: q = Sinkhorn(S) for a batch, a soft assignment whose rows sum to 1 and whose prototype columns are balanced, B/K each (3.2 for B = 64, K = 20).
    • Sinkhorn alternately normalises rows and columns. (1, 3), (2, 2) rows become (0.25, 0.75), (0.5, 0.5).
    • SwAV uses ε = 0.05 and 3 iterations.
  • Swapped loss: ℓ = −Σ_k q_(t,k) log softmax_k(z_sᵀC/τ) + the same with s and t swapped.
  • Multi-crop: 2 large crops plus 6 small crops (8 views); codes come only from the large crops.
  • Prototype usage: the perplexity of assignments is K when all prototypes are used equally and 1 when collapsed.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Samples per prototype for B = 64, K = 20 under equipartition:
  2. 2. Views per image with SwAV multi-crop:
  3. 3. Usage perplexity of a collapsed assignment:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

SwAV in code

Exercise 1.

  • Sinkhorn equalised prototype usage even when every sample preferred one prototype.
  • The swapped loss was near 0 for confident, matching codes.
  • Usage perplexity was 4 for four equally used prototypes and 1 for one.

Practice.

  • SwAV’s reference settings: 3000 prototypes, ε = 0.05, 3 Sinkhorn iterations, τ = 0.1, multi-crop. Freeze the prototypes during the first epoch.
  • For small batches, add a queue of past embeddings to the Sinkhorn step, so equipartition is computed over enough samples.
  • Normalise the prototypes after each step.
z = F.normalize(proj(enc(x)), dim=1)                 # all crops
scores = z @ F.normalize(prototypes, dim=1).T
with torch.no_grad():
    q_t = sinkhorn(scores[:B])                         # codes from a large crop, equal prototype usage
loss = -(q_t * F.log_softmax(scores[B:2 * B] / tau, dim=1)).sum(1).mean()   # predict them from another view
🐍 Exercise 1: Sinkhorn and swapped prediction
Implement Sinkhorn equipartition, the swapped loss and prototype-usage perplexity.
📝 Quick check (Level 2)
0/3 answered
  1. 1. SwAV's number of prototypes (paper):
  2. 2. With small batches, add…
  3. 3. Codes are computed from…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why equipartition.

  • Without a constraint, the cheapest way to make two views’ assignments agree is to assign everything to one prototype, which is trivially consistent and useless.
  • Requiring each prototype to receive B/K of the batch’s mass rules this out and forces the assignments, and hence the features, to distinguish images.

Optimal transport.

  • Computing codes is an entropy-regularised optimal-transport problem: maximise Tr(QᵀS) + εH(Q) subject to uniform marginals.
  • Sinkhorn–Knopp solves it by alternate row and column scaling, which converges quickly for moderate ε; small ε converges slowly when scores are very unequal.

Compared with contrastive methods. SwAV compares each image to K prototypes instead of to many other images, so its cost scales with K rather than batch size. It also groups semantically similar images without treating them as negatives, avoiding the false-negative problem.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Without equipartition the trivial solution is…
  2. 2. Computing codes is an instance of…
  3. 3. SwAV's comparisons scale with…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does equipartition prevent cluster collapse?

History.

  • M. Caron, P. Bojanowski, A. Joulin and M. Douze, “Deep clustering for unsupervised learning of visual features” (DeepCluster, ECCV 2018).
  • Y. M. Asano, C. Rupprecht and A. Vedaldi (SeLa, ICLR 2020), with optimal-transport labels.
  • M. Caron, I. Misra, J. Mairal, P. Goyal, P. Bojanowski and A. Joulin, “Unsupervised learning of visual features by contrasting cluster assignments” (SwAV, NeurIPS 2020).

Exercise 2.

  • Setup: the same style-dominated world as before, with encoder 32 → 64 → 16, projector 16 → 32 → 16 and 20 learnable prototypes (τ = 0.1, 1500 steps).
  • Codes: from Sinkhorn equipartition (ε = 0.05, 3 iterations), or from a plain sharpened softmax with no equipartition.
  • Measurements: probe accuracy and prototype usage (perplexity of nearest-prototype assignments).
codes probe accuracy prototypes in use (of 20)
raw observations 0.522 —
Sinkhorn equipartition 0.756 17.3
no equipartition 0.306 1.0
  • Equipartition prevents collapse: with Sinkhorn, about 17 of 20 prototypes stay in use and the probe reaches 0.76, close to SimCLR (0.78) without any negatives.
  • Without it, training collapses to a single cluster (usage exactly 1.0). The features lose class information and fall below raw observations.
  • Seeds: seeds 1 and 2 gave 0.78 and 0.73 (Sinkhorn, about 16–17 prototypes) against 0.31 and 0.28 (no equipartition, 1 prototype).
🐍 Exercise 2 (research): does equipartition prevent cluster collapse?
Train SwAV with Sinkhorn codes and with plain softmax codes, measuring probe accuracy and prototype usage.

SwAV in AI, robotics and aerospace

  • Unsupervised discovery: grouping unlabelled terrain patches, cloud types or defect images while learning features.
  • Telemetry: clustering flight or engine operating regimes from unlabelled sensor windows.
  • Design lesson: clustering objectives need a balance constraint, or they collapse into one cluster.

References

  1. M. Caron et al. (2020). “Unsupervised learning of visual features by contrasting cluster assignments.” NeurIPS.
  2. M. Caron, P. Bojanowski, A. Joulin and M. Douze (2018). “Deep clustering for unsupervised learning of visual features.” Proc. ECCV.
  3. M. Cuturi (2013). “Sinkhorn distances: lightspeed computation of optimal transport.” NeurIPS.