This is a repository for identifying spoken math in sentence
This repository contains a script for training a BERT-based adapter model for recognizing mathematical expressions in spoken English using the MathBridge dataset.
- Uses AdapterFusion techniques with different adapter types:
- Pfeiffer (
seq_bn) - Houlsby (
double_seq_bn) - Parallel (
par_seq_bn)
- Pfeiffer (
- Token classification task (Named Entity Recognition, NER) with labels:
O: Outside mathematical expressionsB-MATH: Beginning of a math expressionI-MATH: Inside a math expression
- Supports training, validation, and testing
- Saves trained adapter and tokenizer
- Optionally uploads the model and adapter to Hugging Face Hub
Ensure you have Python installed and the necessary dependencies:
pip install torch transformers adapter-transformers datasets seqevalRun the script with default parameters:
python train_adapter.pyThe script supports multiple arguments to customize training:
| Argument | Description | Default |
|---|---|---|
--seed |
Random seed | 42 |
--batch_size |
Batch size | 32 |
--learning_rate |
Learning rate | 1e-4 |
--weight_decay |
Weight decay | 0.01 |
--max_seq_length |
Maximum sequence length | 128 |
--num_train_epochs |
Number of training epochs | 1 |
--adapter_type |
Adapter type (seq, double, par) |
seq |
--reduction_factor |
Adapter reduction factor | 16 |
Example with a different adapter type:
python train_adapter.py --adapter_type double --num_train_epochs 3The script uses the MathBridge dataset from Hugging Face:
load_dataset('Kyudan/MathBridge')This dataset provides contextual spoken English sentences with math expressions labeled accordingly.
- The script tokenizes input texts and assigns labels based on the position of mathematical expressions.
- It trains a BERT-based adapter model using AdapterFusion.
- Training progress and evaluation metrics (precision, recall, F1-score) are logged.
After training, the model is evaluated using:
- Accuracy
- Precision, Recall, and F1-score (via
seqeval)
The trained model and adapter are saved locally:
mathbridge-bert-adapter-seq/
mathbridge-bert-adapter-seq_adapter/Optionally, the model and adapter can be uploaded to Hugging Face Hub:
model.push_to_hub(model_name)
model.push_adapter_to_hub("mathbridge_adapter", "mathbridge_adapter", model_name)This work is based on the AdapterFusion framework and Hugging Face Transformers.