the-puzzler Matteo

Beautiful on the Inside

How to create beautiful internal representations (and grok 6× faster) by pulling each activation towards its nearest neighbours.

Unregularised maps
24 sampled hidden-unit maps from the unregularised network
Reconstruction A front-facing tabby cat reconstructed by the ANNA network.
ANNA maps
24 sampled hidden-unit maps from the ANNA network
Each tile shows one hidden unit. Red marks its lowest activation and blue its highest. Both networks trained with the same activation functions.

Introduction

Intuitively, there is a difference between a student who passes a test by learning the mark scheme and a student who passes a test with a deeper understanding of the subject. Training networks on a single objective can certainly achieve the former. However, as we seek generalisation and adaptability in our networks, what we would like to achieve is the latter. The problem arises if both students pass the test equally well. To tell them apart, we have to look inside: better representations reuse the same building blocks to solve the same problems, so a symmetric problem should not need different building blocks for each half.

Previously, Kumar et al. showed that open-ended search can find beautiful (symmetric) representations in toy settings. However, until recently it was not clear whether SGD-based optimisation could also achieve this style of internal organisation.

This latest paper by Sheppard et al. shows that an auxiliary loss provided by a compressing network can help achieve them. They train a separate network, a unified factored organizer (UFO), to model regularities in another network’s representations.

How a UFO guides the main network The main network is trained on its usual task loss, computed from its prediction. A second branch takes hidden activations from that same network into the UFO. Their reconstruction error supplies an additional training signal to the main network. Main network prediction Task loss Hidden activations UFO Reconstruction reconstruction error

The compressing network’s role is to put pressure on the main network’s activations to become more compressible. In effect, it rewards the main network for reusing the same activation patterns rather than spurious ones, which would be shortcut learning and not generalisation.

This raises two questions:

  1. Can we reward this reuse directly, without a UFO?
  2. If so, which activations should share a pattern?

Theory

Close inputs, far outputs A curve maps input to output. Inputs A and B are close together on the input axis, but the curve jumps steeply between them, so their outputs are far apart. A B input output close far
Close in input space ≠ close in output space.

Naively, we could say that similar inputs should share a pattern, but distance in input space can be very misleading. Instead, we use layer-by-layer activations as a guide. Activations that are already close to one another should be brought closer.

Small k aligns activations within each cluster; large k aligns several clusters together small k large k
Outlines mark activations that are aligned, and so pushed to share computation.

But how close is close enough? Aligning everything would be intuitively destructive to useful information, not just shortcuts. So we only align each activation with its k nearest neighbours. A large k smooths activations over a broad neighbourhood, while a small k only aligns activations that are already near-identical.

Near the input, activations have not yet had the chance to separate, so closeness means less: hence k should start large and shrink with layer depth. Early on, we only smooth over a broad neighbourhood, and as activations separate and closeness becomes more meaningful, we let them cluster.

The same activations at three depths. Early on they are mixed together and one broad outline aligns them all. Deeper in, they separate into clusters and the outlines shrink to match. Early layer, large k Middle layer Late layer, small k
As activations separate with layer depth, k shrinks to match.

Method

CPPN

A CPPN (compositional pattern-producing network, used here as a neural field) is a network that takes coordinates as input and outputs, in this case, a pixel’s colour. These networks have the useful property of being able to show their output over the entire input domain as an image, which makes them ideal for this kind of analysis.

Two ways to draw the same CPPN The network takes each pixel’s coordinates and distance from the centre of the grid. Reading the colour output at every position draws the cat. Reading layer 10, unit 1 at those same positions draws the activation map below it, which picks out the face. Pixelx, y, r CPPN ReconstructionOne unit’s map

For every neuron, we can draw its activation over the entire input space as an image and hence see whether its activations have symmetry or are otherwise intuitive, or whether they appear spuriously entangled.

Following the convention in Sheppard et al.’s UFO paper, I use CPPNs for the experiments pertaining to images.

ANNA objective

Activation Nearest-Neighbour Alignment (ANNA) works like this: at each layer, we batch-normalise the activations, scale each one to unit length, and pull it towards its k nearest neighbours.

\[ \mathcal{L} = \mathcal{L}_{\mathrm{task}} + \lambda(t)\,\mathcal{L}_{\mathrm{ANNA}} \]
\[ \mathcal{L}_{\mathrm{ANNA}} = \underset{\ell,\;a,\;b\in N_{\ell}(a)}{\operatorname{mean}}\, \lVert \hat{z}_a-\hat{z}_b \rVert_2^2 \]
\(\hat{z}\) is an activation after batch normalisation and L2 normalisation. \(N_\ell(a)\) holds the \(k_\ell\) nearest neighbours of \(a\) by cosine similarity.

If we pull activations together from the start, the patterns we are asking the network to preserve come from a random initialisation. If we wait too late, the patterns may already be too pathological. My intuition was that early in training we might find activations that are relevant to the task, but not yet short-cutting.

ANNA weight during warm-up The ANNA weight stays at zero for the first 1,000 steps, while the network trains on the task loss alone. It then rises linearly, reaching full strength at step 2,000. ANNA weightStep Full0 01,0002,000

For this reason, on the images, I let the network train on the task loss alone for the first 1,000 steps, then increased the weight on the ANNA loss linearly over the next 1,000.

Following the intuition above, k shrinks geometrically with depth, from 128 neighbours at the first layer to five at the last.

Neighbourhood size by layer, from 128 neighbours at layer 1 to five at layer 16 k = 128 235 Layer 1Layer 9Layer 16

Experiments

I compare ANNA with the paper’s UFO, and with a network trained on the task alone, in two settings.

Images. Each method trains a CPPN to paint one of three images: a butterfly, a skull and a cat. For each image, I run 16 seeds, matched so that all three methods start from the same weights. ANNA uses the same setting on every image: a strength of 0.3, with k shrinking from 128 to 5.

I measure two things:

  • Symmetry: how closely each unit’s activation map matches its left–right mirror image (a proxy for beauty).
  • MSE: how well the main network completes its task.

Grokking. Following the UFO paper, a two-layer transformer learns modular division, x / y mod 97, from half of all input pairs and is tested on the other half. ANNA and the UFO act on either the attention weights at the “=” token, or the MLP units there. I run eight seeds per setting, all with weight decay. With only two layers in the model, I used k schedules from 384 to 5 on attention and from 511 (whole batch) to 32 on the MLP.

Results

Cat training

Out of curiosity, I wanted to see how the internals change during training, with and without ANNA. Unlike the other experiments, I also lowered ANNA’s strength in two steps along the way, to get a better trade-off between MSE and symmetry.

View a network
Paintings and hidden-unit maps of the ANNA and unregularised cats at step 150,000.

Symmetry and MSE

Across the three images, I compared ANNA with the paper’s UFO and an unregularised network. ANNA matches the UFO’s symmetry on the butterfly and the cat, and is clearly ahead on the skull, at a similar or lower MSE. Without a regulariser, the network often paints a more accurate image, but its maps are far less symmetric.

ANNA UFO Unregularised

Map symmetry ↑

Map symmetry by image and methodEach row is one method, in the order ANNA, UFO, unregularised. Dots are individual seeds and the dark tick is the median. Cat: median ANNA 0.64, UFO 0.59, unregularised 0.36. Butterfly: median ANNA 0.95, UFO 0.95, unregularised 0.84. Skull: median ANNA 0.95, UFO 0.82, unregularised 0.70. image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ 0 0.25 0.5 0.75 1 Cat Butterfly Skull

Pixel MSE ↓

Pixel MSE by image and method, on a log scaleEach row is one method, in the order ANNA, UFO, unregularised. Dots are individual seeds and the dark tick is the median. Cat: median ANNA 0.0139, UFO 0.0138, unregularised 0.0077. Butterfly: median ANNA 0.0045, UFO 0.0059, unregularised 0.0056. Skull: median ANNA 0.0024, UFO 0.0040, unregularised 0.0010. image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ 0.0003 0.001 0.003 0.01 0.03 0.1 0.3 Cat Butterfly Skull
Each dot is one starting seed, and the dark tick marks the median.

Grokking

In the UFO paper, they also show that the regularisation pressure accelerates grokking. To see whether ANNA has a comparable effect, I replicated the experiments with the paper’s code. On attention, ANNA groks about as fast as the UFO. On the MLP it is somewhat slower. Both are several times faster than weight decay alone.

Held-out accuracy during training on modular divisionTwo panels, regularising either the attention or the MLP at the equals token. Each line averages eight seeds, with a shaded band of one standard deviation: ANNA solid, the UFO dashed, weight decay alone dotted. Median steps to grok: attention, ANNA 16,000 and UFO 15,500; MLP, ANNA 37,500 and UFO 30,000; weight decay alone 104,500. image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ 0 40k 80k 120k 0 50 100 Held-out accuracy (%) Attention 0 40k 80k 120k MLP Training step
ANNA UFO Weight decay alone
Mean held-out accuracy across seeds, with a band of one standard deviation. Runs stop once they grok and keep their final score.

Conclusion

ANNA is a simple way to encourage a network to structurise its activations. It only asks that activations which are already close become closer, and lets the network’s own activations decide which those are. Without training an auxiliary network, it matched the UFO’s symmetry on images and its speed of grokking on attention, although it was slower on the MLP.

Going back to the two students, both cat networks pass the test, but only by looking inside can we see that one of them has found a more structured solution. Notably, this structure does come at some cost to MSE. Whether it reflects deeper understanding, i.e. better generalisation beyond these toy settings, is still an open question, though the grokking results are an encouraging first sign.

Open questions:

  • The relationship between k and batch size.
  • A cooldown schedule for λ, which seemed to help on the showcase cat.
  • Generality and scalability, with encouraging early signs.

Comments