Overview
mySpellChecker provides two training pipelines:1. Semantic Model (MLM) Training
Trains a custom Masked Language Model for semantic validation:2. Neural Reranker Training
Trains a small MLP to re-rank spell checker suggestions using learned feature weights:Prerequisites
Install the training dependencies:torch- PyTorch for model trainingtransformers- HuggingFace Transformers for model architecturestokenizers- Fast tokenizer libraryonnx- ONNX export supportonnxruntime- ONNX inference runtime
Quick Start
The simplest way to train a model:Model Architectures
The training pipeline supports two transformer architectures:RoBERTa (Default)
RoBERTa (Robustly Optimized BERT Pretraining Approach) is recommended for most use cases:- Dynamic masking during training
- No Next Sentence Prediction (NSP) objective
- Larger batch sizes and more training data typically improve results
BERT
BERT (Bidirectional Encoder Representations from Transformers):- Static masking
- Includes NSP objective capability
- Well-suited for tasks requiring sentence-pair understanding
Configuration Options
TrainingConfig Parameters
Architecture Constraints
Thehidden_size must be divisible by num_heads. Valid combinations include:
- hidden_size=256, num_heads=4 (64 per head)
- hidden_size=256, num_heads=8 (32 per head)
- hidden_size=512, num_heads=8 (64 per head)
Learning Rate Scheduling
The training pipeline uses linear learning rate scheduling with warmup:- Starts at 0
- Linearly increases to
learning_rateoverwarmup_ratio * total_steps - Linearly decreases to 0 over remaining steps
Resume Training from Checkpoint
Training can be resumed from a checkpoint if interrupted:Training Metrics
Whensave_metrics=True (default), training metrics are saved to training_metrics.json:
step: Global training stepepoch: Current epoch (fractional)loss: Training losslearning_rate: Current learning rate
Low-Level API
For more control, useModelTrainer directly:
ONNX Export
Models are automatically exported to ONNX format with INT8 quantization:SemanticChecker for context-aware validation.
Output Files:
model.onnx- Quantized model (default)model.base.onnx- Original FP32 modeltokenizer.json- Copied for convenience
Using Trained Models
With SemanticChecker
Standalone Inference
CLI Usage
Train a model via CLI:Corpus Format
The training corpus should be a text file with one sentence per line:- UTF-8 encoding
- One sentence per line
- Minimum 100 lines (recommended: 10,000+ lines)
- Segmented text (spaces between words) works best
GPU Support
Training automatically uses GPU if available:Batch Size by GPU VRAM
For CPU-only training:
Model Size vs Quality
Best Practices
- Corpus Size: Use at least 10,000 sentences for meaningful results
- Batch Size: Larger batches (16-32) generally train faster on GPU
- Hidden Size: Start with 256 for small models, 512 for larger ones
- Epochs: 5-10 epochs is usually sufficient; monitor loss for overfitting
- Warmup: 10% warmup (0.1) helps training stability
- Checkpoints: Enable
keep_checkpoints=Truefor long training runs - Metrics: Always save metrics to monitor training progress
Troubleshooting
Memory Issues
Slow Training
Invalid hidden_size/num_heads
Neural Reranker Training
The neural reranker is a small MLP (Linear(19→64)→ReLU→Dropout→Linear(64→1), ~5K parameters) that learns to re-rank spell checker suggestions using 19 extracted features. It runs as the final step in the suggestion pipeline after N-gram and semantic reranking. See Neural Reranker for the full feature vector layout.Prerequisites
Requires:- A production SQLite database (built by the data pipeline)
- A segmented Arrow IPC corpus (produced during pipeline ingestion)
- PyTorch:
pip install myspellchecker[train]
Step 1: Generate Training Data
TheRerankerDataGenerator creates labeled training data by corrupting clean sentences and collecting spell checker candidates:
Step 2: Train the MLP
CLI alternative:
Step 3: Use the Trained Model
See Also
- Semantic Checking: Using trained models for context validation
- Semantic Algorithm: Deep dive into the MLM approach
- Suggestion Ranking: Neural reranker integration
- CLI Reference:
train-modelcommand details - Configuration Guide: SemanticConfig and NeuralRerankerConfig options