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).
★☆☆☆☆ 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. SwAV predicts…
2. Prototypes are…
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. Samples per prototype for B = 64, K = 20 under equipartition:
2. Views per image with SwAV multi-crop:
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 cropsscores = z @ F.normalize(prototypes, dim=1).Twith torch.no_grad(): q_t = sinkhorn(scores[:B]) # codes from a large crop, equal prototype usageloss = -(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. SwAV's number of prototypes (paper):
2. With small batches, add…
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. Without equipartition the trivial solution is…
2. Computing codes is an instance of…
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
M. Caron et al. (2020). “Unsupervised learning of visual features by contrasting cluster assignments.” NeurIPS.
M. Caron, P. Bojanowski, A. Joulin and M. Douze (2018). “Deep clustering for unsupervised learning of visual features.” Proc. ECCV.
M. Cuturi (2013). “Sinkhorn distances: lightspeed computation of optimal transport.” NeurIPS.
Sinkhorn: probe 0.76 with 17 of 20 prototypes used; no equipartition: 1 prototype, probe 0.31.
Practice
Show:
Level 0
Problem 1Level 0
Year SwAV appeared at NeurIPS?
2020.
Problem 2Level 0
Samples per prototype for B = 64, K = 20?
3.2.
Problem 3Level 0
Views per image with 2 large and 6 small crops?
8.
Level 1
Problem 4Level 1
Usage perplexity of 4 equally used prototypes?
4.
Problem 5Level 1
Second entry of row (1, 3) after row normalisation?
0.75.
Problem 6Level 1
Prototypes in the SwAV paper?
3000.
Problem 7Level 1
SwAV’s Sinkhorn ε?
0.05.
Level 2
Problem 8Level 2
Probe accuracy with Sinkhorn codes in exercise 2?
0.756.
Problem 9Level 2
Probe accuracy without equipartition?
0.306.
Problem 10Level 2
Prototypes in use with Sinkhorn?
17.26.
Problem 11Level 2
Prototypes in use without equipartition?
1.
Problem 12Level 2
Fraction of prototypes used with Sinkhorn (17.26/20)?
0.863.
Problem 13Level 2
Sinkhorn probe accuracy with seed 1?
0.776.
Level 3
Problem 14Level 3
Prove that equipartition makes one-cluster collapse infeasible.
Equipartition requires each prototype’s column of Q to sum to 1/K (of the total mass 1). If every sample put all its mass on prototype 1, that column would sum to 1 and the others to 0, violating the constraint for K ≥ 2. Hence the collapsed assignment is not allowed. ∎
Problem 15Level 3
Explain how the unconstrained version collapsed.
With plain softmax codes, one prototype that happens to score slightly higher for all samples receives more target mass, and training reinforces it: the network moves all embeddings toward it and the prototype toward the embeddings. The assignments become constant and trivially consistent across views, so the loss falls while features lose information. ∎
Problem 16Level 3
Compare SwAV with SimCLR.
SimCLR contrasts each image with all other images in a batch (cost grows with batch size; same-class images are negatives). SwAV compares images through K prototypes and balanced assignments (cost grows with K; similar images can share a prototype). Both reach similar accuracy; SwAV with multi-crop was stronger at its release. ∎
Problem 17Level 3
Advise on discovering terrain types from unlabelled rover images.
Use SwAV with a moderate number of prototypes (more than the expected terrain types), multi-crop augmentation, and a queue if batches are small. Inspect images nearest each prototype to interpret clusters, check usage perplexity for collapse, and validate the features with a small labelled set. ∎
Level 4
Problem 18Level 4
No-equipartition probe accuracy with seed 2?
0.282.
Problem 19Level 4
Prototypes in exercise 2?
20.
Problem 20Level 4
Sinkhorn iterations used?
3.
Coding practice
🐍 Code 1 (Level 0): equal share
B/K.
🐍 Code 2 (Level 0): perplexity
exp(entropy).
🐍 Code 3 (Level 0): multi-crop
Total views.
🐍 Code 4 (Level 0): row normalisation
Half a Sinkhorn step.
🐍 Code 5 (Level 0): used fraction
perplexity / K.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
SwAV: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.