Sparse autoencoders for interpretable features
Train a sparse autoencoder on model activations: a network that reconstructs them through a much wider hidden layer, with a penalty keeping only a few hidden units active. Its units recover interpretable feature directions. Explain how these features are inspected and used to steer the model.
Builds on: Superposition and polysemantic neurons
What the animation says
Undoing superposition
Superposition packs many sparse features into a few dimensions, so one neuron ends up meaning several things. To pull the features back out, train a second, small network on saved activations: a sparse autoencoder. It encodes each activation vector into many more hidden units, say sixteen times the width, then decodes it back. Two pressures: rebuild the input accurately, and let only a few units fire per token. What's the cheapest way to satisfy both?
The machine: encode, decode, loss
Right: the cheapest fit is one hidden unit per real feature. Here's the machine that finds them. Subtract the decoder bias, about the mean activation, multiply by encoder weights, add a bias, apply ReLU: non-negative feature activations. Decoding multiplies by the decoder weights and adds the bias back. Each decoder row is one feature's direction, kept at unit length. Together they form the dictionary. So the rebuild is a weighted sum of directions. The loss adds squared error to alpha times the total activation.
Two exact codes, one input
Let's price it in two dimensions. Three dictionary directions: right, up, and the diagonal between them. The input points to one, one. Alpha is zero point one. Code A uses right and up, one each. The arrows add to exactly one, one. Code B uses only the diagonal, at strength root two, about one point four one. Also exact. Both have zero error. Which pays less penalty?
Cheaper, but not too short
Code B wins. A's activations sum to two, penalty zero point two. B's sum to one point four one, penalty about zero point one four. One unit along the true direction is cheaper than a mix of others. So why not shrink B further? Code C uses the diagonal at only zero point seven one. Its penalty halves. But the rebuilt arrow stops at half, half, short of the input. Tap the code with the lowest total loss: error plus penalty.
Inspect and steer a feature
Trained on millions of real activation rows, only a few percent of units fire per token. Inspect: list the snippets where one feature fires hardest. All about French cooking? Label it that. Steer: add that feature's direction, times a constant c, to the residual stream at every position. Unit length puts c on the activation's scale. Peak near five? c of ten pushes toward cooking; negative c suppresses. A matching behavior change is evidence the label is right.
The sparse autoencoder (SAE)
Why the penalty picks one feature
Inspecting and steering a feature
SAE forward pass and loss in numpy
import numpy as np
rng = np.random.default_rng(0)
d, m, alpha = 64, 512, 1e-3 # m = 8d hidden units
acts = rng.standard_normal((1024, d)) # stand-in activations [n_tok, d]
W_enc = rng.standard_normal((d, m)) / np.sqrt(d)
b_enc = np.zeros(m)
W_dec = W_enc.T / np.linalg.norm(W_enc.T, axis=1, keepdims=True) # unit rows u_i
b_dec = acts.mean(0)
F = np.maximum(0, (acts - b_dec) @ W_enc + b_enc) # feature acts [n_tok, m]
acts_hat = F @ W_dec + b_dec
loss = ((acts - acts_hat)**2).sum(1).mean() + alpha * F.sum(1).mean()
print(loss, (F > 0).mean()) # loss, fraction active
What to remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses