Abstract: Large-scale pre-trained models like BERT have obtained great success in various Natural Language Processing (NLP) tasks, while it is still a challenge to adapt them to math-related tasks. Current pre-trained models neglect the structural features and the semantic correspondence between a formula and its context. To address these issues, we propose a novel pre-trained model, namely MathBERT, which is jointly trained with mathematical formulas and their corresponding contexts. In addition, in order to further capture the semantic-level structural features of formulas, a new pre-training task is designed to predict masked formula substructures extracted from the Operator Tree (OPT), which is the semantic structural representation of formulas. We conduct various experiments on three downstream tasks to evaluate the performance of MathBERT, including mathematical information retrieval, formula topic classification, and formula headline generation. Experimental results demonstrate that MathBERT significantly outperforms existing methods on all three tasks. To the best of our knowledge, MathBERT is the first pre-trained model for mathematical formula understanding.
Introduction
Standard NLP models like BERT typically treat mathematical formulas as flat sequences of text. This approach misses two critical components of mathematical understanding:
- Hierarchical Structure: Formulas are not just left-to-right text; they are trees of mathematical operations (e.g., fractions contain numerators and denominators, which themselves might contain exponents).
- Contextual Dependence: A formula's meaning is heavily dependent on its surrounding text, which defines the semantic roles of variables and constants.
Existing math-specific models usually focus purely on tree structures for retrieval or purely on context for classification, failing to jointly capture both. MathBERT bridges this gap by training a single model to understand both the math itself and the text around it.
Methodology
MathBERT builds on the standard BERT architecture but modifies it to accept three concatenated inputs: LaTeX tokens, surrounding text context, and nodes from the formula's Operator Tree (OPT).
To make the model "structure-aware," the authors introduce two major innovations:
- Tree-Constrained Attention: The Transformer's attention mask is modified so that OPT nodes only directly attend to their parent and child nodes in the tree, rather than dense all-to-all attention. This bakes mathematical syntax directly into the model's architecture.
- Masked Substructure Prediction (MSP): Alongside standard Masked Language Modeling (MLM) and Context Correspondence Prediction (CCP), the authors introduce MSP. During pre-training, 15% of the OPT nodes are masked and severed from their edges. The model must predict the parent and child nodes of the masked operator, forcing it to learn mathematical syntax.
The Operator Tree (OPT)
The OPT is the semantic structural representation of a formula. Unlike a symbol layout tree, which focuses on visual positioning, the OPT represents the hierarchy of mathematical operations.
Datasets and Evaluation
The model was pre-trained on a newly constructed dataset of 8.7 million formula-context pairs extracted from the LaTeX source code of arXiv.org scientific papers. Evaluation was conducted across three downstream tasks:
- NTCIR-12 MathIR: A standard benchmark for mathematical information retrieval containing over 590,000 formulas from Wikipedia.
- TopicMath-100K: A newly introduced dataset containing ~132,000 formula-context pairs categorized into 10 scientific topics (e.g., Astrophysics, Quantum Physics) for topic classification.
- EXEQ-300K: An existing dataset used for generating concise math headlines from detailed questions.
Key Results and Insights
- Structure vs. Context: Ablation studies revealed that the Operator Tree (OPT) input is crucial for retrieval tasks (which rely on matching formula structures), whereas the surrounding text context is the primary driver of performance in topic classification.
- Semantic Equivalence: Vanilla BERT struggles with math because it relies on surface symbol overlap. MathBERT, however, recognizes structural equivalence, correctly grouping semantically identical formulas like $\frac{a+b}{c+d}$ and $(a+b) \div (c+d)$.
- Synergistic Training: By forcing the model to predict masked math operators using the surrounding text, MathBERT learns that specific textual phrases (like "Pythagorean theorem") map directly to specific structural sub-trees (like $a^2 + b^2 = c^2$).
- Performance: MathBERT achieved state-of-the-art results on NTCIR-12 with a 73.77 H-Mean score (vs. vanilla BERT's 63.85) and reached a BLEU-4 score of 49.40 on EXEQ-300K, outperforming BERT-fused baselines.
Conclusion
MathBERT demonstrates that integrating structural information and textual context is essential for mathematical formula understanding. While the model is currently limited by input sequence lengths and the accuracy of external LaTeX-to-OPT parsers, it sets a new benchmark for mathematical reasoning in NLP. Future work will explore handling longer, multi-line mathematical proofs and broader reasoning tasks.
Summary updated