This project investigates using 4D context transformers for spreadsheet analysis. The primary task is supervised learning to predict whether cells are bold or non-bold based on surrounding context and cell content. The architecture uses specialized BERT-based models adapted for grid-structured data with custom positional encodings.
classes/models/: Model architecturesBertGrid.py: BERT-based model with custom spatial encoding for grid dataBertPreTinyNew.py: Optimized BERT variant with pretrained weights and spatial awarenessRnn2d.py,SimpleGeluEmbed.py: Alternative model architectures (older models)
classes/: Core dataset classesLoader.py: Custom PyTorch DataLoader optimized for spreadsheet processingVocab.py: Legacy vocabulary class for non-BERT approaches
utils/: Helper functionstrainutil.py: Training loop and optimization functionsinferutil.py: Inference, evaluation and visualization toolsparseutil.py: Spreadsheet parsing and preprocessing utilitiessetuputil.py: Environment and configuration setup
data/: Dataset directories with pre-processed spreadsheets- Organized by size (50, 100, 250, 500, 1k, 2k, all) and split (train, val, test)
runscr/: Training and execution notebookscBertGrid_50.ipynb: Complete pipeline for BertGrid model trainingcBertPreTiny_50.ipynb,cBertPreTiny_1k.ipynb: BertPreTinyNew model trainingtest_devModels.ipynb: Experimentation notebook for model development
testscr/: Testing and verification notebooksmodels/: Saved model checkpoints with timestamp-based naming
- Data Loading:
Loader.pyclasses handle spreadsheet parsing through:LoaderBert: For BERT-based tokenization with HuggingFace tokenizersLoaderSimple: For vocabulary-based tokenization (legacy)
- Preprocessing:
parseutil.pyextracts text and metadata (including bold formatting)- Padding/truncating to fixed dimensions (default: 100x100 cells, 32 tokens per cell)
- Parallel processing for efficiency using joblib
- Input Representation:
- Each cell: Token IDs (x_tok) and attention masks (x_masks)
- Target labels: Metadata matrix with bold info at index 6 (y_tok[:,:,:,6])
-
BertGrid:
- Two-stage BERT architecture applying pooled cell encodings to a spatial encoder
- Custom positional encoding for grid data (row/column awareness)
- Final binary classifier to predict bold status
-
BertPreTinyNew:
- Uses pretrained BERT weights for efficiency
- Projects to architecture-specific embedding dimensions
- Enhanced positional encoding optimized for grid structure
- Run BertGrid training:
jupyter notebook runscr/cBertGrid_50.ipynb - Run BertPreTiny training:
- Small dataset:
jupyter notebook runscr/cBertPreTiny_50.ipynb - Larger dataset:
jupyter notebook runscr/cBertPreTiny_1k.ipynb
- Small dataset:
- Model development testing:
jupyter notebook runscr/test_devModels.ipynb
- Single example:
infer_one()in inferutil.py - displays prediction details for a single spreadsheet - Full evaluation:
infer_full()in inferutil.py - processes multiple examples with metrics - Confusion matrices and cell visualizations are auto-generated
- Metrics: Accuracy, Precision, Recall, F1-score with special focus on bold cells
- Imports: Group by category (standard library β third-party β project modules)
- Model definitions:
- Initialize with config dict containing hyperparameters
- Document internal components with inline comments
- Use descriptive variable names (esp. for tensors)
- Comments: Major functional sections with header comments
- Error handling: Use try/except blocks with specific exception types
- Function clarity: Prefer multiple short functions over monolithic implementations
- Tensor operations: Optimize for memory efficiency, especially when using GPU
Configuration follows a hierarchical approach using dictionaries:
config = {
# Environment settings
"env": "colab", # or "local"
"approach": "bert", # or "simple"
# Model parameters
"model_name": "BertGrid", # or "BertPreTinyNew"
"hidden_size": 128,
"num_hidden_layers": 2,
# Data parameters
"rows": 100,
"cols": 100,
"tokens": 32,
# Training parameters
"batch_size": 8,
"lr": 5e-5,
"patience": 5
}- Dataset typically has extreme imbalance (many non-bold cells, few bold cells)
BCEWithLogitsLosswith positive weighting based on dataset imbalance ratio- Evaluation metrics prioritize precision/recall over simple accuracy
- Use
get_imbalance()method from Loader class to compute class weighting
- CUDA memory issues: Reduce batch size or model complexity
- Preprocessing errors: Check logs for file paths in
failed_fileslist - Poor performance: Examine distribution of bold cells in dataset
- Out-of-distribution data: Run inferutil's infer_one with disp_sig=True for detailed analysis