Experiment Tracking with MLflow and W&B
610 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Experiment Tracking with MLflow and W&B ## 🎯 Learning Objectives - Set up MLflow/W&B for experiment logging - Log parameters, metrics, and artifacts - Implement hyperparameter sweeps - Compare experiments and reproduce results ## 📋 Prerequisites - Python ML workflow - Basic understanding of model training * * *...

Experiment Tracking with MLflow and W&B
🎯 Learning Objectives
- Set up MLflow/W&B for experiment logging
- Log parameters, metrics, and artifacts
- Implement hyperparameter sweeps
- Compare experiments and reproduce results
📋 Prerequisites
- Python ML workflow
- Basic understanding of model training
1. 📖 Core Content
1.1 Why Experiment Tracking?
Without tracking: "Which hyperparameters gave me 89% accuracy?" "What data was this model trained on?"
Tracking provides:
- Reproducibility: Exact record of all experiment details
- Comparison: Side-by-side metric comparison
- Organization: Search/filter through hundreds of runs
- Collaboration: Share results with team
- Documentation: Automatic experiment history
1.2 MLflow Tracking
python# runnable # MLflow tracking example import mlflow def train_with_mlflow(): mlflow.set_experiment("my-experiment") with mlflow.start_run(): # Log parameters mlflow.log_param("learning_rate", 1e-3) mlflow.log_param("batch_size", 64) mlflow.log_param("optimizer", "Adam") mlflow.log_param("model_type", "ResNet-50") # Training loop for epoch in range(10): train_loss = train_epoch() # Log metrics (per epoch) mlflow.log_metric("train_loss", train_loss, step=epoch) if epoch % 5 == 0: val_accuracy = validate() mlflow.log_metric("val_accuracy", val_accuracy, step=epoch) # Log artifacts mlflow.log_artifact("model.pt") mlflow.log_artifact("confusion_matrix.png") # Log model mlflow.pytorch.log_model(model, "model")
1.3 Key Concepts
| Concept | MLflow | W&B |
|---|---|---|
| Run | One execution of code | One training run |
| Experiment | Group of related runs | Project |
| Parameter | Input configuration | Config |
| Metric | Output value (accuracy, loss) | Metric |
| Artifact | File output (model, plots) | Files |
| Tag | Key-value annotation | Tag |
1.4 Hyperparameter Sweeps
python# MLflow sweep configuration sweep_config = { "method": "bayesian", # or "grid", "random" "metric": {"name": "val_accuracy", "goal": "maximize"}, "parameters": { "learning_rate": {"min": 1e-4, "max": 1e-2}, "batch_size": {"values": [32, 64, 128]}, "dropout": {"min": 0.1, "max": 0.5}, "hidden_dims": {"values": [128, 256, 512]} } }
1.5 Best Practices
- Log everything: Every hyperparameter, metric, and version
- Use consistent naming: Project → experiment → run hierarchy
- Log environment: Python version, package versions, GPU info
- Log the data: Data version, split sizes, preprocessing
- Tag important runs: "production", "baseline", "ablation"
📝 Practice Questions
</details> * * * ## 🔗 Cross-References - **Next**: [Data Versioning](/notes/04-degree-electives-bsda5014-mlops-week03-03-data-versioning-dvc) - **Video**: BSDA5014 Week 2 transcripts [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**MLOps Lifecycle**](/notes/04-degree-electives-bsda5014-mlops-week01-01-mlops-lifecycle)[Next**Data Versioning**](/notes/04-degree-electives-bsda5014-mlops-week03-03-data-versioning-dvc)Q1<strong>Q1</strong>: Why log the Git commit hash with each experiment?The Git commit hash uniquely identifies the exact code version used for the experiment. This enables:
- Reproducibility: Revert to the exact code version and rerun
- Debugging: If a change causes performance drop, trace back to the commit
- Collaboration: Team members know which code produced which results
- Auditing: Track who changed what and when
Without it, even with all hyperparameters logged, different code versions may produce different results. Q2<strong>Q2<strong>Q2</strong>: You have 100 training runs and need to find the best configuration. How would MLflow/W&B help?Using the tracking UI:
- Filter: Filter by experiment, tags, or parameters
- Sort: Sort by val_accuracy descending
- Compare: Select top runs and view side-by-side parameters
- Parallel coordinates: Visualize which parameter combinations work best
- Download: Export results as CSV for further analysis
Without a tracking tool, this would require manually collecting results from log files. Q3<strong>Q3<strong>Q3<strong>Q3</strong>: A colleague can't reproduce your model's accuracy. What tracking information would help?
- Exact data version: Which dataset version was used?
- Code version: Git commit hash
- Environment: Python packages + versions (requirements.txt)
- Hyperparameters: All training parameters logged
- Random seed: So random operations are deterministic
- GPU: Model may behave differently on different GPU architectures
- Preprocessing: Any normalization/augmentation applied
Without this information, reproducing ML results is often impossible — this is the reproducibility crisis in ML.