Skip to content

hammadmajeed/mlflow_complete_tutorial

Repository files navigation

MLflow Complete Tutorial

A comprehensive, modular tutorial demonstrating all four core components of MLflow:

  1. MLflow Tracking - Experiment tracking and logging
  2. MLflow Projects - Reproducible runs
  3. MLflow Models - Model packaging and deployment
  4. MLflow Model Registry - Model versioning and lifecycle management

Project Structure

mlflow_complete_tutorial/
├── README.md                    # This file
├── requirements.txt             # Python dependencies
├── .gitignore                   # Git ignore file
├── MLproject                    # MLflow project configuration
├── utils.py                     # Utility functions
├── data/                        # Data directory (auto-generated)
├── 1_tracking.py               # Component 1: MLflow Tracking
├── 2_projects.py               # Component 2: MLflow Projects
├── 3_models.py                 # Component 3: MLflow Models
├── 4_model_registry.py         # Component 4: Model Registry
└── run_all.py                  # Run all tutorials in sequence

Setup

1. Install Dependencies

pip install -r requirements.txt

2. Start MLflow UI (Optional but Recommended)

Open a separate terminal and run:

mlflow ui

Then visit http://localhost:5000 to view the MLflow UI.

Running the Tutorial

Option 1: Run All Components in Sequence

python run_all.py

Option 2: Run Individual Components

Run each component separately to understand them better:

# Component 1: MLflow Tracking
python 1_tracking.py

# Component 2: MLflow Projects
python 2_projects.py

# Component 3: MLflow Models
python 3_models.py

# Component 4: Model Registry
python 4_model_registry.py

Component Details

1. MLflow Tracking (1_tracking.py)

Demonstrates:

  • Creating and managing experiments
  • Logging parameters, metrics, and tags
  • Logging artifacts (files, plots, models)
  • Nested runs for hyperparameter tuning
  • Comparing runs

2. MLflow Projects (2_projects.py)

Demonstrates:

  • Project structure with MLproject file
  • Parameterized runs
  • Reproducible execution environments
  • Running projects with different parameters

3. MLflow Models (3_models.py)

Demonstrates:

  • Saving models in different flavors (sklearn, pytorch, etc.)
  • Loading and using saved models
  • Model signatures and input examples
  • Serving models locally
  • Batch and real-time inference

4. MLflow Model Registry (4_model_registry.py)

Demonstrates:

  • Registering models
  • Model versioning
  • Stage transitions (Staging, Production, Archived)
  • Model annotations and descriptions
  • Loading models from registry

Key Concepts

Tracking Server

  • Backend Store: Stores experiment and run metadata
  • Artifact Store: Stores artifacts like models, plots, data files

Experiments

  • Logical grouping of related runs
  • Each run belongs to an experiment

Runs

  • Individual execution of ML code
  • Records parameters, metrics, artifacts, and metadata

Models

  • Packaged ML models with metadata
  • Supports multiple flavors for different frameworks

Model Registry

  • Centralized model store
  • Version control for models
  • Lifecycle stage management

Best Practices

  1. Use Experiments to organize related runs
  2. Log Everything - parameters, metrics, artifacts
  3. Use Tags for better organization and filtering
  4. Version Your Models through the Model Registry
  5. Document Models with descriptions and tags
  6. Use Model Stages to manage deployment lifecycle

Additional Resources

Troubleshooting

Issue: MLflow UI not accessible

  • Ensure MLflow is installed: pip install mlflow
  • Check if port 5000 is available
  • Try a different port: mlflow ui --port 5001

Issue: Permission errors

  • Check write permissions in the working directory
  • Ensure mlruns/ and mlartifacts/ directories are writable

Issue: Model not found in registry

  • Ensure you've registered the model first
  • Check the tracking URI is correctly configured
  • Verify the model name is correct

License

This tutorial is provided for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published