用数学难题揭秘Transformer如何学算术,发现它靠猜循环长度而非直接计算。
Transformers know more than they can tell -- Learning the Collatz sequence
- 通过不同进制编码输入,模型学会按余数分类预测
- 在24、32进制下准确率达99.7%,其他进制低至25%
- 错误几乎都因循环长度判断失误,极少出现胡编乱造
我们研究了Transformer模型对长柯拉茨步的预测能力,该函数将奇数映射到其远距离后继。模型准确率随输入输出编码基底变化:在24和32进制下可达99.7%,在11和3进制下仅为37%和25%。所有模型均呈现一致学习模式:随着训练推进,逐步掌握共享相同模2^p余数的输入类别,对这些类别预测接近完美,其余输入准确率低于1%。这对应柯拉茨序列的数学性质——长步的循环长度可由输入的二进制表示推断。模型学习的是与递增循环长度相关的输入。故障分析显示,超过90%的错误源于循环长度估计错误,而非幻觉。结果表明,学习此类复杂算术的关键在于理解计算中的控制结构(循环长度),而非运算本身。该方法可推广至更多数学问题,以揭示、解释并改进语言模型。
原文摘要 · Abstract (English)
We investigate transformer prediction of long Collatz steps, a complex arithmetic function that maps odd integers to their distant successors in the Collatz sequence ( $u_{n+1}=u_n/2$ if $u_n$ is even, $u_{n+1}=(3u_n+1)/2$ if $u_n$ is odd). Model accuracy varies with the base used to encode input and output. It can be as high as $99.7\%$ for bases $24$ and $32$, and as low as $37$ and $25\%$ for bases $11$ and $3$. Yet, all models, no matter the base, follow a common learning pattern. As training proceeds, they learn a sequence of classes of inputs that share the same residual modulo $2^p$. Models achieve near-perfect accuracy on these classes, and less than $1\%$ for all other inputs. This maps to a mathematical property of Collatz sequences: the length of the loops involved in the computation of a long Collatz step can be deduced from the binary representation of its input. The learning pattern reflects the model learning to predict inputs associated with increasing loop lengths. An analysis of failure cases reveals that almost all model errors follow predictable patterns. Hallucination, a common feature of large language models, almost never happens. In over $90\%$ of failures, the model performs the correct calculation, but wrongly estimates loop lengths. Our observations give a full account of the algorithms learned by the models. They suggest that the difficulty of learning such complex arithmetic function lies in figuring the control structure of the computation -- the length of the loops. We believe that the approach outlined here, using mathematical problems as tools for understanding, explaining, and perhaps improving language models, can be applied to a broad range of problems and bear fruitful results.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。