Search

Search papers and lessons

Sparse Autoencoder

Level 3 · 11 min read

A sparse autoencoder learns a large set of features from a model's internal activations, with a penalty that forces each input to use only a few of them.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Probability
  • Neural network basics
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

A sparse autoencoder is a small network that learns to rebuild a model's internal activations from a large set of features, with a penalty that makes each activation use only a few of them. The features it finds are often easier to read than the raw neurons, because each feature tends to respond to one concept. This makes it a main tool for looking inside large language models, and it is also the way researchers try to undo superposition, where one neuron serves many concepts.

Builds on: Autoencoder, Dictionary Learning, Rectified Linear Unit (ReLU) · Leads to: Mechanistic Interpretability and the scaling work in Gao et al. (2024)

Where it's used

  • Reading a language model's internals. Anthropic's 2023 paper "Towards Monosemanticity" trained sparse autoencoders on the activations of a small one-layer transformer. Many of the learned features turned out to respond to a recognisable thing, such as a language, a syntactic role or a topic, where single neurons had been hard to interpret.
  • Scaling to larger models. Gao and colleagues (2024) trained sparse autoencoders on the activations of a large GPT-4 model with a fixed top-k rule, which keeps exactly kk features active per input. They report clean scaling behaviour as the number of features grows, and they propose ways to score the features they find.
  • Explaining superposition. A small network can store more features than it has neurons, if the features rarely appear together. This is the situation described in Toy Models of Superposition. A sparse autoencoder tries to recover those features as separate directions, so a single neuron no longer has to stand for several concepts.

How it works

Take a tiny instance. The model's activation is a point in two dimensions, x=(1.2, 1.6)x = (1.2,\ 1.6). The sparse autoencoder has three candidate features (dictionary atoms) of unit length: d1=(1,0)d_1 = (1, 0), d2=(0,1)d_2 = (0, 1) and d3=(0.6, 0.8)d_3 = (0.6,\ 0.8). The autoencoder must write xx as a weighted sum of these atoms, with weights that are all zero or positive. It should use as few atoms as it can.

  1. Start with the activation. x=(1.2, 1.6)x = (1.2,\ 1.6) comes from a layer of the model. Check: it has two numbers, one per coordinate.

  2. Encode into a code. The encoder maps xx to a code ff with one number per feature. A ReLU keeps each number zero or positive. Training has pushed the encoder to output f=(0, 0, 2)f = (0,\ 0,\ 2): only the third feature is on, with weight 2. Check: three features, one active.

  3. Decode back into the activation. The reconstruction is x^=∑ifidi=2×(0.6, 0.8)=(1.2, 1.6)\hat x = \sum_i f_i d_i = 2 \times (0.6,\ 0.8) = (1.2,\ 1.6). Check: this equals xx exactly, so the reconstruction error is zero.

  4. Score the code with a sparsity penalty. The loss adds the reconstruction error to λ∥f∥1\lambda \|f\|_1, the sum of the code's entries. Take λ=0.1\lambda = 0.1. For this code, ∥f∥1=2\|f\|_1 = 2, so the penalty is 0.20.2 and the loss is 0.20.2.

  5. Compare a denser code. Another code, f′=(1.2, 1.6, 0)f' = (1.2,\ 1.6,\ 0), also reconstructs xx exactly, since 1.2 d1+1.6 d2=x1.2\,d_1 + 1.6\,d_2 = x. But ∥f′∥1=2.8\|f'\|_1 = 2.8, so its penalty is 0.280.28 and its loss is 0.280.28.

  6. Conclusion. Both codes rebuild the activation perfectly, and the penalty picks the one that uses one atom. Training does this for every activation in a large dataset, so the atoms that survive are the ones that explain many activations on their own.

flowchart LR
  A[Activation x] --> B[Encoder: ReLU of a linear map]
  B --> C[Sparse code f]
  C --> D[Decoder: weighted sum of atoms]
  D --> E[Reconstruction x hat]
  E --> F[Loss: error plus L1 penalty on f]
  F -.-> B

The L1 penalty is what creates the sparsity. Its shape near zero explains why it works:

−2−1.5−1−0.500.511.5201234code valuepenalty
  • L1 penalty |x|
  • squared penalty x^2
The L1 penalty has a sharp corner at zero, so gradient descent pushes small code values all the way to zero. The squared penalty is flat there and leaves them small but nonzero.

The math

A sparse autoencoder has three pieces. Let x∈Rdx \in \mathbb{R}^{d} be an activation, and let there be mm features with m>dm > d, so the dictionary is larger than the space it lives in.

Step 1 in symbols: the encoder

f=ReLU(Wenc x+benc),Wenc∈Rm×d,benc∈Rm.f = \text{ReLU}(W_{enc}\, x + b_{enc}), \qquad W_{enc} \in \mathbb{R}^{m \times d}, \quad b_{enc} \in \mathbb{R}^{m}.

Here f∈Rmf \in \mathbb{R}^{m} is the code, and ReLU sets each negative entry to zero, so every entry is zero or positive. Each entry fif_i is the strength of feature ii.

Step 2 in symbols: the decoder and the dictionary

x^=Wdec f+bdec.\hat x = W_{dec}\, f + b_{dec}.

The columns of WdecW_{dec} are the dictionary atoms did_i. Writing ff with its entries, Wdecf=∑ifidiW_{dec} f = \sum_i f_i d_i. This is the sum used in the walkthrough, with d1d_1, d2d_2 and d3d_3 as the columns. Each column is usually normalised to unit length so that fif_i measures strength on one scale.

Step 3 in symbols: the loss

L=∥x−x^∥22+λ∥f∥1,∥f∥1=∑i=1m∣fi∣.L = \|x - \hat x\|_2^2 + \lambda \|f\|_1, \qquad \|f\|_1 = \sum_{i=1}^{m} |f_i|.

The first term rewards a faithful reconstruction. The second term, weighted by λ\lambda, is the sparsity penalty, the same kind of penalty used in lasso regression. Because ReLU makes every fif_i nonnegative, ∥f∥1\|f\|_1 is just the sum of the code entries. Step 4 of the walkthrough computed this term for the two codes: ∥(0,0,2)∥1=2\|(0, 0, 2)\|_1 = 2 and ∥(1.2,1.6,0)∥1=2.8\|(1.2, 1.6, 0)\|_1 = 2.8.

Step 4 in symbols: the two codes compared

Using λ=0.1\lambda = 0.1 and the reconstruction error of zero for both codes:

L(0,0,2)=0+0.1×2=0.2,L(1.2,1.6,0)=0+0.1×2.8=0.28.L(0,0,2) = 0 + 0.1 \times 2 = 0.2, \qquad L(1.2, 1.6, 0) = 0 + 0.1 \times 2.8 = 0.28.

The code with the smaller ℓ1\ell_1 norm has the smaller loss, so the optimiser prefers it.

Note on the top-k variant

Some sparse autoencoders replace the penalty with a rule: keep the kk largest entries of the encoder output and set the rest to zero. This makes the sparsity exact and avoids shrinking the surviving entries, which an L1 penalty does. The 2024 scaling paper uses this rule.

Worked examples

Example
1. Two atoms, one input, by hand

Use the walkthrough numbers: x=(1.2, 1.6)x = (1.2,\ 1.6), λ=0.1\lambda = 0.1.

  1. Code (0, 0, 2)(0,\ 0,\ 2). Reconstruction: 2×(0.6, 0.8)=(1.2, 1.6)2 \times (0.6,\ 0.8) = (1.2,\ 1.6). Error: 00.
  2. Penalty: 0.1×(0+0+2)=0.20.1 \times (0 + 0 + 2) = 0.2. Loss: 0.20.2.
  3. Code (1.2, 1.6, 0)(1.2,\ 1.6,\ 0). Reconstruction: 1.2×(1,0)+1.6×(0,1)=(1.2, 1.6)1.2 \times (1, 0) + 1.6 \times (0, 1) = (1.2,\ 1.6). Error: 00.
  4. Penalty: 0.1×(1.2+1.6+0)=0.280.1 \times (1.2 + 1.6 + 0) = 0.28. Loss: 0.280.28.
  5. The first code has the lower loss.

Interpretation: when two codes reconstruct equally well, the L1 penalty picks the sparser one. The features it keeps are the ones that explain the activation with the fewest pieces.

Example
2. A tie that breaks with the penalty

Input x=(1, 1)x = (1,\ 1) with the same three atoms. Two codes are possible.

  1. Code A=(1, 1, 0)A = (1,\ 1,\ 0). Reconstruction: (1,1)(1, 1). Error: 00. ∥A∥1=2\|A\|_1 = 2.
  2. Code B=(0, 0, 1.5)B = (0,\ 0,\ 1.5). Reconstruction: 1.5×(0.6, 0.8)=(0.9, 1.2)1.5 \times (0.6,\ 0.8) = (0.9,\ 1.2). Error vector: (0.1, −0.2)(0.1,\ -0.2).
  3. Squared error of BB: 0.12+0.22=0.01+0.04=0.050.1^2 + 0.2^2 = 0.01 + 0.04 = 0.05. ∥B∥1=1.5\|B\|_1 = 1.5.
  4. Set λ=0.1\lambda = 0.1. Loss of AA: 0+0.1×2=0.20 + 0.1 \times 2 = 0.2. Loss of BB: 0.05+0.1×1.5=0.200.05 + 0.1 \times 1.5 = 0.20. A tie.
  5. Set λ=0.2\lambda = 0.2 instead. Loss of AA: 0+0.2×2=0.40 + 0.2 \times 2 = 0.4. Loss of BB: 0.05+0.2×1.5=0.350.05 + 0.2 \times 1.5 = 0.35.
  6. The single-atom code BB now wins.

Interpretation: λ\lambda is a dial. Small values favour exact reconstruction with more features on. Larger values favour the single-feature explanation, at the cost of a small reconstruction error.

Example
3. The realistic case: a residual stream

Suppose the activation is a 512-dimensional vector from a model's residual stream, and the sparse autoencoder has m=4096m = 4096 features. Suppose on average 20 features are active for each input.

  1. Encoder weights: 512×4096=2,097,152512 \times 4096 = 2{,}097{,}152.
  2. Decoder weights: the same, 2,097,1522{,}097{,}152.
  3. Total weights, ignoring biases: 2×2,097,152=4,194,3042 \times 2{,}097{,}152 = 4{,}194{,}304, about 4.2 million.
  4. Fraction of features active per input: 20/4096≈0.004920 / 4096 \approx 0.0049.
  5. As a percentage: about 0.49 percent of the features are on for any one input.

Interpretation: the autoencoder is about eight times wider than the activation, but any input uses only a small fraction of its features. This is the sparse code the method aims for.

Try it: if the same autoencoder had 40 features active per input instead of 20, what percentage would be active? Answer: 40/4096≈0.9840 / 4096 \approx 0.98 percent.

Common mistakes and tradeoffs

It is tempting to think that each learned feature is a true feature of the model. In fact the dictionary depends on the dictionary size and on λ\lambda. A larger dictionary can split one concept into several features, and a smaller one can merge several concepts into one. The features are useful evidence, not ground truth, and they should be tested by changing the model and watching the output.

It is tempting to think that sparse means few neurons are active in the model. In fact the sparsity is in the autoencoder's code, not in the model's neurons. The autoencoder re-expresses each activation using a few of its many features.

It is tempting to think that a bigger dictionary is always better. In fact a larger dictionary costs memory and compute, and some features can die, meaning they never activate on the training data. Reconstruction improves with size but the features can become harder to interpret.

When to use it: reading the activations of a trained model, or finding directions that represent single concepts. When not to use it: when you need the exact mechanism of a computation, since the autoencoder gives a dictionary of features, not the circuit that uses them. Alternatives include a plain autoencoder without the sparsity penalty, which reconstructs well but mixes concepts, and direct analysis of individual neurons, which is simple but often misleading under superposition.

History

Sparse coding began with models of the visual cortex. In 1996 Olshausen and Field showed that a sparsity penalty applied to natural image patches yields edge-like filters, an early landmark in Dictionary Learning. Autoencoders were long used to learn compressed codes for data. The 2013 K-sparse autoencoder paper enforced sparsity with a top-k rule instead of a penalty. In 2022 the Toy Models of Superposition paper showed that networks pack more features than they have neurons, which made the interpretability question sharper. In 2023 Anthropic's Towards Monosemanticity paper applied sparse autoencoders to a small transformer and found interpretable features. In 2024 Gao and colleagues scaled the approach to much larger models with the top-k rule.

Recap

  • An autoencoder reconstructs its input through a code; a sparse autoencoder adds a penalty so that the code uses few features.
  • The encoder is a linear map followed by ReLU. The decoder is a weighted sum of dictionary atoms, which are the learned features.
  • The loss is the reconstruction error plus λ\lambda times the L1 norm of the code, and the L1 penalty prefers sparser codes when reconstruction is equal.
  • Applied to a model's activations, the features often respond to single concepts, which helps against superposition.
  • What to read next: the scaling paper and Mechanistic Interpretability for how the features are used to explain a model.

Check yourself

QuestionRecall: what are the columns of the decoder weight matrix in a sparse autoencoder?Show answer

The dictionary atoms, the learned feature directions. The decoder output is the weighted sum of these columns, with the code entries as weights.

QuestionA code is (0.5, 0, 1.5)(0.5,\ 0,\ 1.5). What is its L1 norm, and what is its penalty when λ=0.2\lambda = 0.2?Show answer

∥f∥1=0.5+0+1.5=2.0\|f\|_1 = 0.5 + 0 + 1.5 = 2.0. The penalty is 0.2×2.0=0.40.2 \times 2.0 = 0.4.

QuestionWhy does the L1 penalty make codes sparse, while a squared penalty does not?Show answer

The absolute value has a sharp corner at zero, so the gradient of the penalty stays the same size as a code entry gets close to zero, and gradient descent can drive it exactly to zero. The squared penalty is flat near zero, so small entries shrink but rarely reach zero exactly.

QuestionWhy can two runs of the same method find different features, and what does this say about treating features as facts?Show answer

The dictionary depends on its size and on λ\lambda, and the optimiser can converge to different solutions. A feature is therefore evidence about the model, which must be tested by intervening on the model, not a fixed fact about it.

QuestionScenario: a researcher trains a sparse autoencoder on a 1024-dimensional activation with 8192 features. Each input activates about 32 features. What fraction of features is active?Show answer

32/8192=0.003932 / 8192 = 0.0039, about 0.39 percent. The code is sparse, and the autoencoder is eight times wider than the activation.

Sources