解析Transformer如何学习识别规律语言,揭示其训练动态与隐式偏好。
How Transformers Learn Regular Language Recognition: A Theoretical Study on Training Dynamics and Implicit Bias
- 通过理论分析一维Transformer的梯度下降过程,研究其学习机制。
- 注意力层先快速变化,再趋于稳定;线性层缓慢增长并逼近最优超平面。
- 实验验证了理论结果,适用于理解大模型内在工作原理的研究者。
语言识别是自然语言处理中的基础任务,常用于评估大语言模型性能,并解释Transformer的工作机制。本文聚焦两类典型的正则语言识别任务——'偶数对'与'奇偶校验',目标是判断给定序列中特定子序列出现次数是否为偶数。研究对象为包含注意力层与线性层的一层Transformer,在梯度下降下的训练动态。结果显示,偶数对任务可直接由一层Transformer解决,而奇偶校验需引入思维链(CoT),或在训练阶段整合,或在已训练好的偶数对模型推理时加入。两类任务均呈现两个阶段:第一阶段注意力层迅速扩张,将输入映射为可分向量;第二阶段注意力层趋于稳定,线性层以对数速度增长,方向趋近于最大间隔超平面,使输出正确分离正负样本,损失以 $O(1/t)$ 速率下降。实验验证了理论预测。
原文摘要 · Abstract (English)
Language recognition tasks are fundamental in natural language processing (NLP) and have been widely used to benchmark the performance of large language models (LLMs). These tasks also play a crucial role in explaining the working mechanisms of transformers. In this work, we focus on two representative tasks in the category of regular language recognition, known as `even pairs' and `parity check', the aim of which is to determine whether the occurrences of certain subsequences in a given sequence are even. Our goal is to explore how a one-layer transformer, consisting of an attention layer followed by a linear layer, learns to solve these tasks by theoretically analyzing its training dynamics under gradient descent. While even pairs can be solved directly by a one-layer transformer, parity check need to be solved by integrating Chain-of-Thought (CoT), either into the inference stage of a transformer well-trained for the even pairs task, or into the training of a one-layer transformer. For both problems, our analysis shows that the joint training of attention and linear layers exhibits two distinct phases. In the first phase, the attention layer grows rapidly, mapping data sequences into separable vectors. In the second phase, the attention layer becomes stable, while the linear layer grows logarithmically and approaches in direction to a max-margin hyperplane that correctly separates the attention layer outputs into positive and negative samples, and the loss decreases at a rate of $O(1/t)$. Our experiments validate those theoretical results.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。