语言人工智能指南

Logit Lens 和中间层解码

The logit lens is an interpretability technique that applies a language model's final output layer to the hidden states of earlier layers, showing which next token each layer is leaning toward.

听听这个指南AI叙述 · ~8 min

在本页阅读时间 9 分钟
  1. 概述
  2. 主要要点
  3. 深入探讨
  4. Running a logit lens on a small open model
  5. 现实世界的实施
  6. Logit Lens 和中间层解码的未来
  7. 付诸实践
  8. 资料来源与延伸阅读
  9. 不断探索
  10. 常见问题

概述

Introduced in a 2020 blog post on GPT-2 and refined in 2023 by the tuned lens, it is one of the simplest ways to watch a prediction form layer by layer, though its readouts are approximations rather than proof of what the model knows.

主要要点

  1. The logit lens applies a model's own final normalization and unembedding to earlier hidden states to see each layer's implied next-token prediction.
  2. It was introduced by the researcher nostalgebraist in an August 2020 analysis of GPT-2 1.5B on LessWrong.
  3. The logit lens works poorly on models such as GPT-Neo, BLOOM and OPT 125M, and is biased toward some tokens even where it works.
  4. The 2023 tuned lens trains one affine translator per layer to match the final output, giving lower perplexity and less bias than the logit lens.
  5. A token ranked highly by a lens at some layer is evidence about the computation, not proof of where the model stores a fact.

深入探讨

A transformer language model processes text as a stack of layers. Each token position carries a vector of numbers, called the hidden state or residual stream, that every layer reads and then adds an update to. Only after the last layer does the model convert this vector into a prediction: it applies a final normalization and multiplies by the unembedding matrix, a learned table that turns the vector into one score, or logit, per vocabulary token. A softmax turns those logits into probabilities for the next token. Intermediate layer decoding asks what would happen if that final readout were applied early, at layer 5 or layer 20, instead of at the end.

The logit lens is the simplest answer. Take the hidden state after some layer, apply the model's own final layer normalization and unembedding, and read off the top tokens. No training is needed, because it reuses the model's own weights. Doing this at every layer and every position produces a grid showing the most likely token at each depth, which researchers call a prediction trajectory. If the trajectory settles on the final answer early, the later layers are mostly refining or sharpening that answer; if it changes late, the decision is made near the end.

The technique was introduced in a LessWrong post by the pseudonymous author nostalgebraist on August 31, 2020. Running GPT-2's 1.5 billion parameter version on the abstract of the GPT-3 paper, the post reported that intermediate layers already produce plausible guesses, moving from nonsense or shallow guesses with the right part of speech in early layers to refined predictions later. It also reported a surprise: the input token itself was not preserved in early layers, as the hidden state moved almost at once toward something closer to the output. The author added caveats in the post, including that top tokens show only part of what the activations contain.

The logit lens did not generalize well. In 2023, Nora Belrose and colleagues at EleutherAI, FAR AI, the University of Toronto, Boston University and UC Berkeley reported that it fails to produce plausible predictions for model families such as GPT-Neo, BLOOM and OPT 125M. For BLOOM and OPT 125M, its top prediction was often just the input token in more than half the layers. Even where it worked, it was biased, systematically giving more probability to some vocabulary items than the final layer does. One cause is representational drift: a feature can be encoded differently at different depths, so the final layer's readout is the wrong decoder for earlier layers.

Their fix, the tuned lens, trains one small affine transformation, a matrix plus a bias called a translator, for each layer of a frozen model. Each translator maps that layer's hidden state into the coordinate system the final readout expects, and is trained so that its decoded distribution matches the model's real final output as closely as possible, measured by Kullback-Leibler (KL) divergence, a standard measure of how different two probability distributions are. Tested on models with up to 20 billion parameters, the paper reports that tuned lens predictions have substantially lower perplexity, meaning they predict the next token better, and are less biased than the logit lens. On GPT-Neo-2.7B, the logit lens produced uninterpretable output before layer 21, while the tuned lens gave sensible predictions throughout.

These lenses have been used for several research questions. Halawi, Denain and Steinhardt (2023) found that when a few-shot prompt contains wrong labels, middle-layer predictions are often more accurate than final outputs, and traced the late switch to attention heads that copy the false labels. The tuned lens paper reproduced this on models where the logit lens fails. Wendler and colleagues (2024) used the logit lens to argue that Llama-2 passes through a concept space closer to English when processing other languages. Patchscopes, from Google researchers in 2024, generalizes the idea by patching a hidden state into a separate prompt and letting the model describe it in words, and its authors argue that vocabulary-projection methods are special cases of their framework.

The main limitation is interpretive. A lens is a probe, a separate readout attached to the model, and what it shows depends on how it is built. A token that a lens ranks highly at layer 12 does not prove the model represents that fact at layer 12, and a token that does not appear may still be encoded in a form the readout cannot decode. The tuned lens is trained to imitate the final output, so it may reveal what later layers will do with the state rather than what the current layer computes. Belrose and colleagues partly addressed this with causal experiments, removing the directions the lens relies on and showing that this also changes the model's own output, but such checks are evidence, not a complete causal account. Both lenses were designed for pre-normalization transformers, the common modern layout, and work less cleanly on other designs.

In practice the logit lens costs almost nothing: a few lines of code that reuse the model's own weights, run on any open-weights model whose hidden states can be accessed. It cannot be applied to models available only through an API that hides internal activations. The tuned lens requires training one translator per layer on text similar to the model's pretraining data; the authors trained theirs on a slice of the Pile validation set, initialized each translator to the identity and used stochastic gradient descent with Nesterov momentum for 250 steps, a small cost compared with training the model itself.

04工作示例

Running a logit lens on a small open model

  1. Load an open model such as GPT-2 small with a library that returns hidden states for every layer.

  2. Feed the prompt The Eiffel Tower is located in the city of and keep the hidden state at the last position for each layer.

  3. For each layer, apply the model's final layer normalization and multiply by the unembedding matrix to get logits.

  4. Apply softmax and record the top 5 tokens and the probability of the token Paris at each layer.

  5. Compute the KL divergence between each layer's distribution and the final layer's distribution.

  6. Plot the probability of Paris and the KL divergence against layer number.

它显示了什么

The learner gets a curve showing at which depth the model's implied prediction approaches its final answer. KL divergence should fall toward zero at the last layer by construction; if early layers show only noise or the input token, that is the known weakness a tuned lens is designed to fix.

现实世界的实施

The original analysis: in August 2020 the pseudonymous researcher nostalgebraist decoded every layer of the 48-layer GPT-2 1558M model and found that next-token guesses become sensible well before the final layer and converge gradually.

Multilingual research: a 2024 study of Llama-2 used the logit lens on non-English prompts, such as translating the French word fleur into Chinese, and found that middle layers often favor the English version of the correct word before the output shifts to the input language.

Few-shot prompting: Halawi and colleagues (2023) decoded intermediate layers and found that models given deliberately wrong examples often predict correctly at middle layers, then switch to copying the false pattern after a critical layer.

Attack detection: the tuned lens paper ran Pythia 12B on nine multiple-choice tasks with and without a prompt injection, and fed the layer-by-layer answer probabilities to standard outlier detectors to flag attacked inputs, often with near-perfect accuracy.

Tooling: EleutherAI and FAR AI's tuned-lens package, installed with pip install tuned-lens and released under the MIT license, trains and applies per-layer lenses to open models.

Logit Lens 和中间层解码的未来

Intermediate layer decoding has become a standard first tool in mechanistic interpretability, the study of how trained networks compute their outputs, but research has moved toward methods that ask the model to explain more than a single next token. Patchscopes (Ghandeharioun and colleagues, ICML 2024) inserts a hidden state into a separate prompt and lets the model describe it in natural language, and its authors report that this can inspect early layers where vocabulary projections fail, use a stronger model to read a smaller model's states, and help correct multi-step reasoning errors. Open questions remain. Lens readouts show a distribution over output tokens, so they say little about intermediate computations that never resemble a word, such as numerical or positional features. It is also unresolved how faithfully any learned probe reflects the model rather than the probe's own fitting, which is why causal tests that change the model's hidden states are increasingly paired with decoding. Findings such as an English-leaning concept space in Llama-2 (Wendler and colleagues, 2024) raise further questions about bias in multilingual models, and whether they hold across architectures and training mixes needs testing model by model. Applied uses are also being explored, including the tuned lens paper's 2023 finding that prediction trajectories can flag prompt-injection attacks. Whether such signals stay reliable against attackers who know about them, and whether they work on much larger models, has not been established.

付诸实践

为什么这很重要

  • 速度与规模. 语言工作流程可以在不牺牲一致性的情况下更快地移动。
  • 交通与覆盖范围. 它扩展了跨语言和沟通方式的访问。
  • 更清晰的判决. 团队可以花更多时间进行判断,而自动化则可以处理重复。

留意

  • 幻觉的事实可以悄悄地进入报告、支持流程或研究成果。
  • 及时的敏感性可能会在类似的请求中产生不一致的结果。
  • 如果访问控制薄弱,敏感文本数据可能会暴露。

后续步骤

  1. 在推出之前定义输出格式、语气和质量标准。
  2. 当准确性很重要时,请使用可信来源进行地面响应。
  3. 为高风险输出保留人工审查检查点。
  4. 跟踪故障模式并定期重新训练提示或工作流程。

资料来源与延伸阅读

  1. LessWronginterpreting GPT: the logit lens
  2. arXivEliciting Latent Predictions from Transformers with the Tuned Lens
  3. GitHub (AlignmentResearch)tuned-lens
  4. arXivOverthinking the Truth: Understanding how Language Models Process False Demonstrations
  5. arXivDo Llamas Work in English? On the Latent Language of Multilingual Transformers
  6. arXiv (ICML 2024)Patchscopes: A Unifying Framework for Inspecting Hidden Representations of Language Models

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

Take the Logit Lens and Intermediate Layer Decoding quiz

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

What is Logit Lens and Intermediate Layer Decoding?

The logit lens is an interpretability technique that applies a language model's final output layer to the hidden states of earlier layers, showing which next token each layer is leaning toward. Introduced in a 2020 blog post on GPT-2 and refined in 2023 by the tuned lens, it is one of the simplest ways to watch a prediction form layer by layer, though its readouts are approximations rather than proof of what the model knows.

What readout does a plain logit lens reuse to obtain vocabulary scores from an intermediate state?

The projection maps a compatible hidden-width state into vocabulary logits, while its reliability on intermediate states still needs assessment.

Why can the same output projection be applied mathematically at multiple residual-stream depths?

Dimensional compatibility permits the readout; it does not make every layer's representation equivalent to the final one.

相对于普通 Logit 镜头,“调谐镜头”解决了什么问题?

调谐透镜训练一个小的每层仿射图,以便更忠实地解码中间状态,从而减少平面透镜的早期层偏差和噪声。

If an intermediate lens readout favors a correct factual token, what has been directly observed?

The observation is a descriptive probe result. Causal interpretation needs additional evidence and interventions.

Logit 透镜最直接的用途是什么?

其核心价值是可解释性:揭示模型预测代币分布的逐层演化。