| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
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
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
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:
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 precisionThis project is available under the MIT License.
Happy modeling! 🚀
| Back | FazBrowse Home | New Git URL |