Author Login Chief Editor Login Reviewer Login Editor Login Remote Office

Computer Engineering

   

Handwritten Mathematical Expression Recognition Based on Multi-Granularity Rotary Position Embedding

  

  • Published:2026-11-24

基于多粒度旋转位置编码的手写数学公式识别

Abstract: Handwritten mathematical expression recognition (HMER) aims to convert two-dimensional formula images into LaTeX sequences. Mapping a formula to a one-dimensional sequence turns intuitive spatial relations, such as subscripts, superscripts, and fractions, into implicit patterns that depend on specific relative distances. This poses a great challenge to structural parsing in the decoder. Existing mainstream Transformer-based methods generally use absolute position encoding in decoder self-attention. These methods lack direct awareness of relative positional offsets within the sequence. As a result, they find it difficult to capture structural information, such as subscript attribution, fraction hierarchy, and nested boundaries, accurately. To solve these problems, this paper proposes a multi-granularity head-wise rotary position embedding (MG-RoPE). First, the method adopts hybrid position injection. It retains the sinusoidal absolute position encoding at the embedding layer and applies a rotation transformation to the queries and keys in self-attention. This design preserves the absolute order prior and lets relative positional offsets act directly on the attention inner product. Second, the strong absolute order prior of structural tokens in LaTeX sequences calls for a partial-dimension rotation strategy. Each attention head rotates only its leading feature dimensions. The method recalibrates the angular frequencies according to the dimension of the rotated sub-vector, so the rotated subspace keeps the same frequency coverage as full rotary encoding. This strategy prevents the absolute order prior from being diluted. Third, the method proposes a multi-granularity head-wise rotation ratio configuration. It retains a pure absolute-position head and assigns different rotation ratios to the other groups of heads. This configuration gives different attention heads different positional perception tendencies. These improvements act only on the queries and keys, introduce no additional learnable parameters, and apply directly to existing decoders. On the CROHME 2014, 2016, and 2019 datasets and the HME100K dataset, the expression recognition rates (ExpRate) reach 64.06%, 62.42%, 66.81%, and 69.37%, respectively. The method outperforms the baseline by 1.38, 1.39, 1.84, and 0.34 percentage points, respectively, and effectively alleviates structural parsing errors in complex nested formulas. On the subsets containing subscripts/superscripts, fractions and nested structures, the ExpRate improves by 3.50, 4.54 and 7.15 percentage points, respectively. Furthermore, the method transfers the multi-granularity head-wise rotation ratio configuration to the CoMER decoder without auxiliary structural supervision. It improves the ExpRate by 0.30, 1.04, and 2.00 percentage points on CROHME 2014, 2016, and 2019, respectively. These results show that the strategy applies across decoder architectures.

摘要: 手写数学公式识别旨在将二维公式图像转换为LaTeX序列,由于公式具有复杂的二维空间布局,将其映射为一维序列后,原本直观的空间从属关系(如上下标、分式)会转化为依赖特定相对距离的隐式序列模式,这给解码器的结构解析带来了巨大挑战。现有基于Transformer的主流方法在解码器自注意力层普遍采用绝对位置编码,缺乏对序列内部相对位置偏移的直接感知能力,导致上下标归属、分式层级及嵌套边界等结构信息难以被准确捕获。针对上述问题,本文提出多粒度头级旋转位置编码。首先,采用混合位置注入:保留嵌入层的正弦绝对位置编码,同时在自注意力的查询与键上施加旋转变换,使绝对顺序先验得以保留,相对位置偏移直接作用于注意力内积计算。在此基础上,针对LaTeX序列中结构token具有强绝对顺序先验的特点,引入部分维度旋转策略:每个注意力头仅旋转前部分特征维度,并按旋转子向量维度重新标定角频率,使旋转子空间保持与完整旋转编码一致的频率覆盖,避免绝对顺序先验被稀释。进一步提出多粒度头级旋转比例配置策略,通过保留纯绝对位置头并为其余各组分配差异化的旋转比例,为不同注意力头构造差异化的位置感知倾向。上述改进仅作用于查询与键,不引入额外可学习参数,可直接应用于现有解码器。在CROHME 2014、2016、2019与HME100K数据集上的公式级完全正确率分别达到64.06%、62.42%、66.81%和69.37%,较基线分别提升1.38、1.39、1.84与0.34个百分点,有效缓解了复杂嵌套公式中的结构性解析错误,在含上下标、分式与嵌套结构的复杂公式子集上分别提升3.50、4.54与7.15个百分点。进一步地,将多粒度头级旋转比例配置策略移植至不依赖辅助结构监督的CoMER解码器,在CROHME 2014/2016/2019基准上分别提升0.30、1.04和2.00个百分点,体现了该策略的跨解码器架构适用性。