A transcoder is a sparse dictionary trained to map an MLP's input to its output, so that the MLP's computation, not just its activations, is expressed in interpretable latents. Implement it with exact gradients and compare it with an SAE fitted to the same outputs.
transcoder_forward(X, W_enc, b_enc, W_dec, b_dec)
X (B, d) MLP inputs. pre = X @ W_enc + b_enc, Z = max(pre, 0), Yhat = Z @ W_dec + b_dec. Return (pre, Z, Yhat).transcoder_loss_and_grads(X, Y, W_enc, b_enc, W_dec, b_dec, lam)
Y (B, d_out) MLP outputs. Loss . Return (loss, grads) with exact gradients for the four parameters (dict keys "W_enc", "b_enc", "W_dec", "b_dec").transcoder_step(params, X, Y, lam, lr)
W_dec; return a new dict.fve(Y, Yhat)
mlp_reference(X, W1, b1, W2, b2)
max(X @ W1 + b1, 0) @ W2 + b2.Y; the encoder still reads X.