← playground

Sparse autoencoders, live

Neural networks are tangled and messy, and each neuron usually responds to many unrelated things. A sparse autoencoder projects activations into a higher-dimensional space and forces only a few features to activate for each input. Still confused? Keep reading for a visualization of how SAEs work and to understand where they still fall short.

The explanations follow two guides, Adam Karvonen's intuitive explanation of sparse autoencoders and a gentle introduction to sparse autoencoders on LessWrong.

01

At every layer, a language model turns each token into a long list of numbers called an activation. GPT-3 uses 12,288 numbers per token. My simulation uses 3, so each activation is a point you can see. Each axis stands for one neuron, and the point's coordinates are that neuron's values.

02

Here are the activations for 600 different inputs. The model sees only these coordinates, with no understanding of what the points mean.

03

Each input contains one or two of 6 hidden concepts, labelled A to F, and each concept has its own direction. An input that holds only concept A sits on A's line, and its distance from the center shows how much of A it holds.

$$\mathbf{x} = \sum_{i=1}^{6} w_i\,\mathbf{f}_i$$

The LessWrong guide writes activations this way. Each \(\mathbf{f}_i\) is a feature vector of length 1, and the weight \(w_i\) is the feature activation, how much of that feature the input holds.

04

Three dimensions fit at most three directions at right angles to each other, but six do not fit. The six directions overlap, and every pair meets at 63.4° or 116.6°, an overlap of \(|\cos\theta| = 0.447\). Researchers call this packing of more features than neurons superposition.

The LessWrong guide states the superposition hypothesis. Models learn feature vectors that are almost orthogonal, and in high dimensions the number of such directions grows exponentially (the Johnson–Lindenstrauss lemma).

05

Overlap does little harm when few concepts appear at once, a property researchers call sparsity. Here, … of the inputs hold one concept. Scroll to light the one-concept inputs on the six lines, then the rest in the thin fans between them.

The LessWrong guide's example is a coffee mug. You describe it by size and use and need no astronomy, so most feature activations stay at zero. Karvonen adds that one celebrity's birthplace can appear in fewer than one in a billion training tokens, and large models still learn it.

06

Superposition breaks the direct way of reading a network, one neuron at a time. A neuron measures the input along its own axis, so every concept leaning toward that axis moves it. Scroll to light each neuron in turn. Here … of the 6 concepts move every neuron, so each neuron is polysemantic.

Karvonen describes a real neuron that responded to academic citations, English dialogue, HTTP requests and Korean text.

07

Karvonen starts from a regular autoencoder, which squeezes its input through a smaller layer and rebuilds it. A sparse autoencoder does the opposite and widens the middle layer. Its encoder turns an activation into one score per feature, and its decoder turns the scores back into an activation, \(\hat{\mathbf{x}}\).

This one has 12 features for 3 neurons. Each arrow is one feature's decoder vector, still pointing at random before training.

Karvonen explains why the width needs a rule. A layer this wide could copy its input and tell us nothing, so a sparsity penalty keeps most scores at zero.

08

Training adjusts the encoder and decoder to lower the loss.

$$\mathcal{L} = \lVert \mathbf{x} - \hat{\mathbf{x}} \rVert^2 + \lambda \sum_{j} c_j$$

The first term is the reconstruction error, the red line on the right. The second adds up the feature scores \(c_j\) and multiplies them by \(\lambda\), the L1 coefficient. A larger \(\lambda\) pushes more scores to zero. I rescale every decoder vector to length 1 after each step, so the model cannot dodge the penalty by shrinking scores.

Karvonen points out that nothing in this loss asks for readable features. They appear as a side effect of sparsity and accurate reconstruction.

09

Scroll to train. Your browser trained this autoencoder for 2,400 steps with \(\lambda = 0.4\) and saved a snapshot every 10 steps, and your scroll position picks the snapshot. The arrows turn onto the concept lines, and each takes a concept's color once it comes within 25° of it.

10

Some arrows never settle. In this run … of the 12 features fire on fewer than 1% of inputs. These dead features, the pulsing dashed lines, use compute and explain nothing.

The LessWrong guide reports that about 65% of the features in Anthropic's largest autoencoder were dead.

11

Scroll slowly to follow one input through the trained autoencoder. The encoder multiplies the activation by a matrix and adds a bias, giving one score per feature. ReLU sets every negative score to zero, so most scores end at zero.

The decoder multiplies those scores by a second matrix, which adds up the decoder vectors, each scaled by its score. On the right they join head to tail and end at the reconstruction \(\hat{\mathbf{x}}\).

Karvonen sums up the model as a matrix, then a ReLU, then a matrix. On GPT-3 with 4 times the width, the encoder maps 12,288 numbers to 49,152 scores, and researchers aim for fewer than 100 nonzero scores.

12

This is why people call decoder vectors features. Whenever feature … scores above zero, the decoder adds a scaled copy of its arrow to the reconstruction. The lit points are those inputs, brighter for higher scores.

Karvonen's example is a "Golden Retriever" direction in GPT-3. If decoder vector 317 learns it, then each time feature 317 turns on, a scaled Golden Retriever vector joins the reconstruction.

13

Each feature has two vectors. The thick decoder vector is the direction the feature writes. The thin encoder vector decides when the feature turns on, so it also has to ignore overlapping concepts. …

Karvonen reports a median cosine of about 0.5 between the two vectors in real autoencoders.

14

\(\lambda\) sets a trade-off between sparsity and reconstruction. Scroll to move across three autoencoders trained with \(\lambda\) = 0.02, 0.4 and 1.2.

At 0.02, about … features stay on per input and the reconstruction explains … of the variance, but the arrows find only … of 6 concepts. At 1.2, they find … of 6, but the penalty shrinks every score and the reconstruction explains only ….

Karvonen compares autoencoders by plotting L0, the mean number of nonzero scores, against loss recovered, how much of the model's own loss survives with the reconstruction in place. With no language model here, I show variance explained instead.

Karvonen now prefers BatchTopK, which keeps the k largest scores across a batch, so you set sparsity directly with no penalty to tune. The LessWrong guide mentions OpenAI's related k-sparse autoencoders.

15

On a real model nobody knows the true directions, so researchers name features from their top examples. The panel lists the inputs that fire feature … hardest, with the concepts each one holds. Here I can check the name, since feature … points within 25° of concept ….

The LessWrong guide describes Anthropic's method. They collected the text where a feature scored highest and wrote a description that fit, such as "food places". Karvonen calls this check a gut reaction, because natural language gives no ground truth.

16

A name read off examples might fit by coincidence. To test it, you change the activation and watch what happens. Researchers call this steering. A decoder vector has the same shape as an activation, so you can add it straight in.

Scroll to add up to 2.4 times feature …'s decoder vector to one input. The point slides along the arrow, and its score rises from … to ….

When Anthropic added the Golden Gate Bridge feature to Claude's activations, Karvonen notes, Claude mentioned the bridge in every response.

17

My simulation has 3 dimensions and 12 features, and I planted every concept, so it cannot show what a real model hides. Anthropic's largest autoencoder had about 34 million features, and a real model gives you no planted directions to check against.

Karvonen describes a chain of stand-ins. The training loss stands in for proxy metrics, the metrics stand in for human judgment, and that judgment stands in for understanding the model.

The LessWrong guide notes that nobody has proved superposition, that autoencoders may find combinations of features instead of single ones, and that it is surprising a nonlinear network splits so cleanly into weighted sums.

Still, Karvonen argues that so simple an objective would not find readable features unless the model had learned something meaningful.

18

Your turn. Choose a sparsity rule and train a new autoencoder. Click any point to see its scores.

rule
λ0.4

Check out other SAE writing I like!