A PyTorch implementation of TabTransformer for binary classification on the MovieLens dataset. This project predicts whether a user will rate a movie highly (β₯4.0 stars) based on user, movie, and contextual features using transformer architecture designed for tabular data.
- TabTransformer Architecture: Attention-based model specifically designed for tabular data
- Multi-Modal Features: Handles categorical (users, movies, genres, tags) and continuous features
- Adaptive Thresholding: Automatically finds optimal classification thresholds
- Flexible Dataset Support: Works with different MovieLens dataset sizes
- GPU Acceleration: CUDA support with automatic fallback to CPU/MPS
- Comprehensive Evaluation: Multiple metrics with threshold optimization
- Python 3.11+
- CUDA-compatible GPU (optional, but recommended)
- uv package manager
uv venv --python 3.11Windows (PowerShell):
.venv\Scripts\Activate.ps1macOS/Linux:
source .venv/bin/activateuv syncThis will install all required dependencies including PyTorch with CUDA support on Windows/Linux, or CPU/MPS support on other platforms.
# Train model without tags (recommended for faster training)
python main.py --mode train
# Train model with tag features (slower but potentially more accurate)
python main.py --mode train --use-tags# Basic evaluation with standard 0.5 threshold
python main.py --mode evaluate
# Evaluation with adaptive threshold optimization
python main.py --mode evaluate --adaptive-threshold
# Optimize threshold for specific metric
python main.py --mode evaluate --adaptive-threshold --threshold-metric f1# 1. Train a model (downloads data automatically)
python main.py --mode train --use-tags
# 2. Evaluate with adaptive thresholding
python main.py --mode evaluate --use-tags --adaptive-threshold --threshold-metric accuracy# Different dataset sizes
python main.py --mode train --dataset ml-latest-small
python main.py --mode train --dataset ml-latest
# Custom rating threshold
python main.py --mode train --rating-threshold 3.5
# Enable tag features
python main.py --mode train --use-tags# Evaluate specific model
python main.py --mode evaluate --model-path path/to/model.pth
# Different threshold optimization metrics
python main.py --mode evaluate --adaptive-threshold --threshold-metric precision
python main.py --mode evaluate --adaptive-threshold --threshold-metric recalltab-transformer/
βββ src/ # Core implementation
β βββ __init__.py
β βββ config.py # Configuration settings
β βββ dataset.py # PyTorch Dataset implementation
β βββ download_and_preprocess.py # Data downloading and preprocessing
β βββ tabtransformer.py # TabTransformer model architecture
β βββ train_utils.py # Training utilities and metrics
β βββ trainer.py # Training loop implementation
βββ data/ # Dataset storage (created automatically)
βββ artifacts/ # Training artifacts and logs
βββ notebook/ # Jupyter notebooks for experimentation
β βββ bertclassifier.ipynb
βββ main.py # Main pipeline script
βββ pyproject.toml # Project dependencies and configuration
βββ python-version.txt # Python version specification
βββ uv.lock # Dependency lock file
main.py: Complete pipeline orchestration with CLI interfacesrc/tabtransformer.py: Core transformer architecture for tabular datasrc/dataset.py: Custom PyTorch dataset with feature engineeringsrc/trainer.py: Training loop with validation and checkpointingsrc/config.py: Centralized configuration managementsrc/train_utils.py: Metrics computation and adaptive thresholding
The TabTransformer combines the power of transformer attention with tabular data processing:
graph TD
A[User ID] --> E[User Embedding]
B[Movie ID] --> F[Movie Embedding]
C[Genre IDs] --> G[Genre Embeddings]
D[Tag IDs] --> H[Tag Embeddings]
I[Continuous Features] --> J[Linear Projection]
E --> K[Column-wise Contextual Embeddings]
F --> K
G --> K
H --> K
J --> K
K --> L[Multi-Head Self-Attention]
L --> M[Feed Forward Network]
M --> N[LayerNorm + Residual]
N --> O{More Layers?}
O -->|Yes| L
O -->|No| P[Global Pooling]
P --> Q[Classification Head]
Q --> R[Binary Prediction]
style E fill:#e1f5fe
style F fill:#e1f5fe
style G fill:#e1f5fe
style H fill:#e1f5fe
style J fill:#fff3e0
style L fill:#f3e5f5
style Q fill:#e8f5e8
- Categorical Embeddings: Users, movies, genres, and tags are embedded into dense vectors
- Column-aware Processing: Each feature type gets distinct column ID embeddings
- Multi-Head Attention: Captures complex feature interactions
- Continuous Feature Integration: Numerical features are projected and combined
- Adaptive Classification: Optional threshold optimization for deployment
Key configuration options in src/config.py:
MODEL_CONFIG = {
'd_model': 128, # Model dimension
'n_heads': 8, # Attention heads
'n_layers': 3, # Transformer layers
'd_ff': 256, # Feed-forward dimension
'dropout': 0.1 # Dropout rate
}
TRAINING_CONFIG = {
'num_epochs': 50, # Training epochs
'batch_size': 512, # Batch size
'learning_rate': 1e-3, # Learning rate
'weight_decay': 1e-4 # L2 regularization
}The model includes automatic threshold optimization:
- Metrics: Optimize for accuracy, F1-score, precision, or recall
- Validation-based: Uses validation set to find optimal threshold
- Multiple Thresholds: Tests 100 different threshold values
- Comprehensive Reporting: Shows improvement over standard 0.5 threshold
- GPU Acceleration: Automatic CUDA detection and usage
- Data Loading: Multi-worker data loading with memory pinning
- Mixed Precision: Optional for faster training on modern GPUs
- Checkpointing: Automatic model saving and configuration persistence
The evaluation pipeline provides comprehensive metrics:
EVALUATION RESULTS
==================================================
Test Samples: 10,000
π― ADAPTIVE THRESHOLD RESULTS (threshold = 0.6234):
Accuracy: 0.8756
Precision: 0.8934
Recall: 0.8123
F1 Score: 0.8512
AUC-ROC: 0.9234
Test Loss: 0.3421
π STANDARD THRESHOLD RESULTS (threshold = 0.5):
Accuracy: 0.8543
Precision: 0.8721
Recall: 0.7891
F1 Score: 0.8289
π IMPROVEMENT:
Accuracy improvement: +0.0213
F1 improvement: +0.0223
# Use larger dataset
python main.py --mode train --dataset ml-latest
# Custom rating threshold
python main.py --mode train --rating-threshold 3.5# Enable tag features for richer representations
python main.py --mode train --use-tags
# Evaluate with different optimization targets
python main.py --mode evaluate --adaptive-threshold --threshold-metric precision- CUDA Out of Memory: Reduce batch size in
src/config.py - Download Failures: Check internet connection and retry
- Missing Data: Run with
--mode trainfirst to download datasets - Model Not Found: Ensure training completed successfully
- Use
--use-tagsonly if you need maximum accuracy (slower training) - Enable GPU acceleration for faster training
- Use adaptive thresholding for production deployments
- Monitor training logs in the artifacts directory
This project is available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Happy modeling! π