Skip to main content
QUICK REVIEW

[论文解读] Physics of Language Models: Part 1, Learning Hierarchical Language Structures

Zeyuan Allen-Zhu, Yuanzhi Li|arXiv (Cornell University)|May 23, 2023
Natural Language Processing TechniquesComputer Science被引用 3
一句话总结

本文通过受控的合成实验,研究自回归变换器如何学习并表征上下文无关语法(CFG)。结果表明,GPT-2 在生成复杂、长序列且具有歧义的 CFG 时可达到接近完美的准确率,其隐藏状态精确编码了非终结符结构,注意力机制通过边界到边界的注意力模式,模拟了动态规划的行为。

ABSTRACT

Transformer-based language models are effective but complex, and understanding their inner workings and reasoning mechanisms is a significant challenge. Previous research has primarily explored how these models handle simple tasks like name copying or selection, and we extend this by investigating how these models perform recursive language structure reasoning defined by context-free grammars (CFGs). We introduce a family of synthetic CFGs that produce hierarchical rules, capable of generating lengthy sentences (e.g., hundreds of tokens) that are locally ambiguous and require dynamic programming to parse. Despite this complexity, we demonstrate that generative models like GPT can accurately learn and reason over CFG-defined hierarchies and generate sentences based on it. We explore the model's internals, revealing that its hidden states precisely capture the structure of CFGs, and its attention patterns resemble the information passing in a dynamic programming algorithm. This paper also presents several corollaries, including showing why absolute positional embeddings is inferior to relative and rotary embeddings; uniform attention alone is surprisingly effective (motivating our follow-up work on Canon layers); encoder-only models (e.g., BERT, DeBERTa) struggle with deep structure reasoning on CFGs compared to autoregressive models (e.g., GPT); and injecting structural or syntactic noise into pretraining data markedly improves robustness to corrupted language prompts.

研究动机与目标

  • 理解变换器学习复杂、层次化语言结构(如上下文无关语法,CFG)的物理机制。
  • 研究变换器在处理 CFG 中长距离依赖和歧义时的表现,特别是在需要动态规划进行验证的情况下。
  • 开发并验证探测技术,以分析隐藏状态是否编码了非终结符(NT)结构和边界信息。
  • 考察变换器对语法规则错误的鲁棒性,以及位置嵌入和相对位置嵌入在 CFG 学习中的作用。
  • 证明训练模型中的注意力模式通过形成“边界到边界”的注意力流,类似于动态规划。

提出的方法

  • 构建具有不同难度级别的合成非平凡 CFG,包括长序列、具有歧义性和全局歧义性的语法(例如,cfg8、cfg9、cfg0 系列)。
  • 使用语言建模目标,在大规模从这些 CFG 采样的合成数据集上微调 GPT-2-small。
  • 采用线性探测评估隐藏状态是否以高准确率编码非终结符(NT)祖先信息。
  • 可视化并量化注意力模式,以检测“边界到边界”的注意力模式,表明存在类似动态规划的行为。
  • 应用拒绝采样筛选出具有唯一动态规划解的 CFG,从而在 cfg9 系列上实现可靠的探测。
  • 比较不同注意力机制(如旋转位置编码、相对位置编码)对 CFG 学习性能的影响。

实验结果

研究问题

  • RQ1变换器能否以高准确率和多样性学习生成来自复杂、长序列且具有歧义的上下文无关语法的句子?
  • RQ2训练后的变换器中,隐藏状态如何编码 CFG 中的非终结符(NT)结构和层次边界?
  • RQ3变换器中的注意力机制是否形成类似动态规划的模式,例如“边界到边界”的注意力流?
  • RQ4位置嵌入和相对位置嵌入在多大程度上影响模型学习 CFG 的能力?
  • RQ5变换器对语法规则错误的鲁棒性如何?在具有歧义的 CFG 中,它们能否从局部不一致性中恢复?

主要发现

  • GPT-2-small 在所有测试的 CFG 系列上均实现了接近完美的生成准确率(接近 100%),包括长序列和具有歧义的语法。
  • 预训练后,隐藏状态线性编码了非终结符(NT)祖先信息,且准确率很高,尤其是在子树边界处。
  • 该模型学会了形成“边界到边界”的注意力模式,其结构与动态规划的递归结构相匹配,从而实现了高效的解析。
  • cfg9 系列确保了唯一的动态规划解,使得探测更加可靠,并证实隐藏状态中精确编码了 NT 结构。
  • 相对位置嵌入和旋转位置嵌入显著提升了 CFG 学习性能,尤其在长序列设置下表现更优。
  • 即使在全局歧义的 CFG 中(如 cfg8、cfg0),模型仍能生成多样且合法的字符串,表明其对结构歧义具有强鲁棒性。

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。