提出可扩展的序列位置编码,让Transformer更好处理长序列和多维输入。
SeqPE: Transformer with Sequential Position Encoding
- 将位置索引转为符号序列,用轻量编码器端到端学习位置嵌入。
- 在长序列任务中,困惑度、准确率等指标优于现有方法,尤其外推时表现更佳。
- 无需修改结构即可适配图像等多维数据,适合需要灵活扩展的场景。
由于Transformer中的自注意力层天生具有置换不变性,必须显式引入位置编码以实现空间理解。然而,传统可学习位置编码采用固定大小的查找表,限制了超出预训练序列长度的外推能力。专家设计的方法如ALiBi和RoPE虽缓解此问题,但需大量修改才能适配新模态,暴露出适应性和可扩展性的根本挑战。本文提出SeqPE,一种统一且全可学习的位置编码框架:将每个n维位置索引表示为符号序列,并使用轻量级序列位置编码器端到端学习其嵌入。为正则化嵌入空间,引入两个互补目标:对比损失使嵌入距离与预定义位置距离函数对齐;知识蒸馏损失将分布外位置嵌入锚定到分布内教师表示,进一步提升外推性能。在语言建模、长上下文问答和2D图像分类任务上的实验表明,SeqPE不仅在困惑度、精确匹配(EM)和准确率上超越强基线,尤其在上下文长度外推时表现突出,还无需手动架构重设计即可无缝推广至多维输入。代码、数据及检查点已开源。
原文摘要 · Abstract (English)
Since self-attention layers in Transformers are permutation invariant by design, positional encodings must be explicitly incorporated to enable spatial understanding. However, fixed-size lookup tables used in traditional learnable position embeddings (PEs) limit extrapolation capabilities beyond pre-trained sequence lengths. Expert-designed methods such as ALiBi and RoPE, mitigate this limitation but demand extensive modifications for adapting to new modalities, underscoring fundamental challenges in adaptability and scalability. In this work, we present SeqPE, a unified and fully learnable position encoding framework that represents each $n$-dimensional position index as a symbolic sequence and employs a lightweight sequential position encoder to learn their embeddings in an end-to-end manner. To regularize SeqPE's embedding space, we introduce two complementary objectives: a contrastive objective that aligns embedding distances with a predefined position-distance function, and a knowledge distillation loss that anchors out-of-distribution position embeddings to in-distribution teacher representations, further enhancing extrapolation performance. Experiments across language modeling, long-context question answering, and 2D image classification demonstrate that SeqPE not only surpasses strong baselines in perplexity, exact match (EM), and accuracy--particularly under context length extrapolation--but also enables seamless generalization to multi-dimensional inputs without requiring manual architectural redesign. We release our code, data, and checkpoints at https://github.com/ghrua/seqpe.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。