Foundations to Frontier
Tests
E. Deep Learning › E8 CNN Architectures (one page each)

Capsule Networks (dynamic routing)

Capsule networks (Sabour, Frosst and Hinton, 2017) replace scalar feature detectors with capsules: vectors whose length is the probability that an entity is present and whose direction encodes its pose. Lower capsules predict the pose of higher ones through learned matrices, and an iterative routing-by-agreement sends each lower capsule's output to the parents whose predictions agree. CapsNet reached 0.25% error on MNIST and separated overlapping digits, but proved costly and hard to scale. This page covers squashing, prediction matrices, dynamic routing, the margin loss, and the evidence for and against capsules. In the exercises you build the pieces, then run routing on synthetic scenes: it raised agreeing children's coupling from 0.33 to 0.46 in 3 iterations and sharpened the present parent's lead from 2.9 to 4.0 times, amplifying agreement that plain summation already showed.

Level 0: Foundation

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

Parts that vote on wholes

A face is a face only if its eyes, nose and mouth are in the right places relative to one another. Ordinary CNNs detect parts but, after pooling, partly forget where they were. Capsule networks represent each part by a small vector, its pose. Each part predicts where the whole should be. If several parts’ predictions agree, the whole is probably there, and those parts send their output to it. This “routing by agreement” was Geoffrey Hinton’s proposal for networks that understand spatial relationships.

CNN building blocks
input 9 × 9
→
output 8 × 8
output size ⌊(9 − 2)/1⌋ + 1 = 8 · no parameters to learn
Deep-learning "convolution" slides a small learned kernel over the input (no flip). Stride shrinks the output, padding preserves borders, dilation spreads the kernel to see further without more weights. Pooling summarises windows. Stacking layers grows the receptive field. Depthwise-separable and grouped convolutions cut cost by not connecting every input channel to every output channel. Transposed convolution upsamples, but uneven kernel overlap leaves checkerboard patterns. Squeeze-and-excitation rescales whole channels by gates computed from their global averages; CBAM adds a per-pixel spatial gate. (Here the gates are fixed functions of the averages so you can see their effect; in a network the excitation is learned.)
Why engineers care

Capsules address a real weakness, viewpoint and part–whole structure, that matters for recognising objects from unusual angles, as robots and aerial cameras must. They also show how an appealing idea can struggle to scale.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A capsule's length represents…
  2. 2. A capsule's direction encodes…
  3. 3. Routing by agreement sends outputs to…

Level 1: Beginner

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

By hand

  • Squash: v = (‖s‖²/(1 + ‖s‖²))·s/‖s‖. It keeps the direction and maps length l to l²/(1 + l²), so 1 → 0.5, 3 → 0.9 and 5 → 0.96.

  • Predictions: û_(j|i) = W_ij u_i, where child i predicts parent j’s pose through a learned matrix.

  • Routing (r iterations):

    1. Coupling c_ij = softmax_j(b_ij).
    2. s_j = Σ_i c_ij û_(j|i), and v_j = squash(s_j).
    3. b_ij ← b_ij + û_(j|i)·v_j.

    With b = 0, every child splits equally: c = 1/3 for 3 parents.

  • Margin loss: Σ_k T_k max(0, 0.9 − ‖v_k‖)² + 0.5(1 − T_k) max(0, ‖v_k‖ − 0.1)². A true-class length of 0.5 costs 0.16.

  • CapsNet on MNIST:

    • a 9 × 9 conv with 256 channels;
    • PrimaryCaps: 32 maps × 6 × 6 = 1,152 capsules of 8 dimensions;
    • DigitCaps: 10 capsules of 16 dimensions, with 1,152·10·16·8 = 1,474,560 routing weights.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Squashed length of a vector of length 3:
  2. 2. Initial coupling to each of 3 parents:
  3. 3. Number of PrimaryCaps in CapsNet:

Level 2: Intermediate

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

Capsules in code

Exercise 1.

  • Squash kept the direction (0.6, 0.8) of (3, 4) and gave length 25/26.
  • The margin loss was 0 for a confident correct output.
  • The prediction tensor had shape (children, parents, 16).

Practice.

  • CapsNet trains with Adam and 3 routing iterations.
  • A decoder reconstructs the input from the true class’s capsule, with loss weight 0.0005, as a regulariser.
  • Routing is iterative and per example, so it is slow. Memory grows with the number of children × parents × dimensions, which makes large images expensive.
  • Matrix capsules with EM routing (Hinton, Sabour and Frosst, 2018) use 4 × 4 pose matrices and an EM-based routing.
def route(u_hat, iters=3):            # u_hat: (batch, children, parents, dim)
    b = torch.zeros(u_hat.shape[:3])
    for _ in range(iters):
        c = b.softmax(dim=2); v = squash((c[..., None] * u_hat).sum(1))
        b = b + (u_hat * v[:, None]).sum(-1)
    return v
🐍 Exercise 1: capsule building blocks
Implement squash, the margin loss and capsule predictions.
📝 Quick check (Level 2)
0/3 answered
  1. 1. CapsNet's reconstruction loss weight:
  2. 2. Routing iterations in CapsNet:
  3. 3. Matrix capsules use…

Level 3: Advanced

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

Theory

Equivariance instead of invariance. Max pooling discards exact positions, building some invariance. Capsules aim for equivariance: when the viewpoint changes, pose vectors change predictably while presence (length) stays the same. The learned matrices W_ij encode part–whole geometry, which in principle lets a network generalise to new viewpoints.

Routing as clustering. Each iteration raises the coupling of child i to parent j in proportion to the agreement û_(j|i)·v_j, a soft clustering of predictions. Agreement among many independent predictions is unlikely by chance, so it is strong evidence that the parent is present.

Evidence.

  • On MNIST, CapsNet reached 0.25% error.
  • On affNIST (MNIST digits under affine transforms, not seen in training) it reached 79% accuracy, against 66% for a CNN with similar MNIST accuracy.
  • It segmented highly overlapping digits (MultiMNIST).

Limits.

  • Routing is costly, and capsule networks did not scale competitively to ImageNet.
  • Later analyses (e.g. Paik et al., 2019) found that routing often adds little over uniform coupling, and that coupling can become uniform or collapse.
  • Exercise 2 shows a related point: plain summation of agreeing predictions already favours the present parent, and routing mainly sharpens it.

Attention mechanisms now fill much of the role capsules aimed at.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Capsules aim for…
  2. 2. Routing resembles…
  3. 3. On affNIST CapsNet reached…

Level 4: Expert

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

What does routing actually do?

History.

  • G. Hinton, A. Krizhevsky and S. Wang (2011) introduced “transforming auto-encoders” with capsules.
  • S. Sabour, N. Frosst and G. Hinton (2017) introduced dynamic routing between capsules (CapsNet).
  • G. Hinton, S. Sabour and N. Frosst (2018) introduced matrix capsules with EM routing.
  • I. Paik, T. Kwak and I. Kim (2019) questioned the routing algorithm’s benefit.

Exercise 2. Each of 200 synthetic scenes has 10 child capsules and 3 parents, with 8-dimensional poses:

  • 7 children predict nearly the same pose for parent 0 (the “present” entity);
  • the other predictions are random.

Dynamic routing is run for 1, 2, 3 and 5 iterations:

iterations coupling of agreeing children to parent 0 of other children length of parent 0 of absent parents
1 0.333 0.333 0.589 0.206
2 0.392 0.335 0.664 0.196
3 0.464 0.337 0.735 0.182
5 0.634 0.344 0.839 0.147
  • Routing works as designed. Agreeing children shift their output to the present parent, while the others stay near 1/3. The present parent’s length grows and the absent parents’ shrink.
  • But the present parent already stands out with uniform coupling (0.59 against 0.21, 2.9 times). Agreeing predictions add up, random ones cancel. Routing sharpens the contrast (4.0 times after 3 iterations, 5.7 after 5) rather than discovering it.
  • So routing’s benefit depends on whether sharper contrast matters for the task. That is consistent with later findings that routing often adds little in practice.
🐍 Exercise 2 (research): what does routing actually do?
Run dynamic routing on synthetic scenes and measure coupling coefficients and parent lengths over iterations.

Capsules in AI, robotics and aerospace

  • Viewpoint robustness: part–whole reasoning is attractive for recognising objects from unusual viewpoints, such as aerial and satellite views, and capsule variants have been tried on remote-sensing data.
  • Pose estimation: capsule-style pose vectors relate to object pose estimation for robotic grasping.
  • Research lesson: a principled idea needs careful ablation (does routing help over uniform coupling?) before adoption.

References

  1. S. Sabour, N. Frosst, G. E. Hinton (2017). “Dynamic routing between capsules.” Advances in NeurIPS.
  2. G. E. Hinton, S. Sabour, N. Frosst (2018). “Matrix capsules with EM routing.” Proc. ICLR.
  3. G. E. Hinton, A. Krizhevsky, S. D. Wang (2011). “Transforming auto-encoders.” Proc. ICANN.
  4. I. Paik, T. Kwak, I. Kim (2019). “Capsule networks need an improved routing algorithm.” Proc. ACML.