Skip to main content

Command Palette

Search for a command to run...

πŸš€ Day 06 – Experiment Tracking & MLflow: Supercharge Your MLOps Workflow

Published
β€’4 min read
πŸš€ Day 06 – Experiment Tracking & MLflow: Supercharge Your MLOps Workflow
B

I am Bittu Sharma, a DevOps & AI Engineer with a keen interest in building intelligent, automated systems. My goal is to bridge the gap between software engineering and data science, ensuring scalable deployments and efficient model operations in production.! π—Ÿπ—²π˜'π˜€ π—–π—Όπ—»π—»π—²π—°π˜ I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.

Welcome back to the MLOps learning series!
In the previous blogs, we explored Docker, Git, and version control for ML workflows.
Today, we dive into one of the most essential tools in MLOps β€” MLflow, a powerful open-source platform for managing the end-to-end machine learning lifecycle.

Let’s understand how you can track, reproduce, and manage ML experiments efficiently using MLflow.


🧠 What is MLflow?

MLflow is an open-source platform designed to manage the complete lifecycle of a Machine Learning (ML) model β€” from experiment tracking to deployment.

It provides 4 major components:

  1. MLflow Tracking – Log parameters, metrics, and artifacts for each experiment.

  2. MLflow Projects – Package ML code in a reusable and reproducible format.

  3. MLflow Models – Manage and serve trained models.

  4. MLflow Registry – Version control and manage the lifecycle of models.


βš™οΈ Why You Need MLflow in MLOps

In real-world ML projects, you often train hundreds of models with different:

  • Hyperparameters

  • Feature sets

  • Algorithms

  • Data versions

Without tracking tools, comparing these models is chaotic.
MLflow helps you:
βœ… Keep a record of all experiments
βœ… Compare runs automatically
βœ… Log metrics, parameters, and artifacts
βœ… Reproduce any previous experiment easily


🧩 MLflow Components in Detail

1️⃣ MLflow Tracking

It helps log experiments β€” model parameters, metrics, and outputs.

You can log:

  • Parameters β†’ Learning rate, batch size, etc.

  • Metrics β†’ Accuracy, loss, F1-score

  • Artifacts β†’ Model files, plots, confusion matrices

  • Source code β†’ Code version for reproducibility


2️⃣ MLflow Projects

MLflow Projects help package your code in a standardized format using a simple MLproject file.

πŸ“ Example structure:

mlflow_project/
β”‚
β”œβ”€β”€ MLproject
β”œβ”€β”€ train.py
└── conda.yaml

πŸ“„ MLproject file example:

name: mlflow-demo-project
conda_env: conda.yaml
entry_points:
  main:
    parameters:
      alpha: {type: float, default: 0.5}
      l1_ratio: {type: float, default: 0.1}
    command: "python train.py --alpha {alpha} --l1_ratio {l1_ratio}"

3️⃣ MLflow Models

It helps manage models after training β€” you can save, load, and deploy models easily.

MLflow supports multiple model formats:

  • mlflow.sklearn

  • mlflow.tensorflow

  • mlflow.pytorch

  • mlflow.xgboost

Each saved model can be served through REST API or deployed to cloud services.


πŸ§ͺ Hands-on: Track ML Experiments using MLflow

Let’s now do a practical example of tracking your ML experiments.

βœ… Step 1: Install MLflow

pip install mlflow

βœ… Step 2: Import Required Libraries

import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

βœ… Step 3: Prepare Dataset

data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.2, random_state=42
)

βœ… Step 4: Initialize MLflow Experiment

mlflow.set_experiment("Iris_Classifier_Experiment")

βœ… Step 5: Train Model and Log Parameters

with mlflow.start_run():
    clf = RandomForestClassifier(n_estimators=100, max_depth=3, random_state=42)
    clf.fit(X_train, y_train)

    preds = clf.predict(X_test)
    acc = accuracy_score(y_test, preds)

    # Logging parameters and metrics
    mlflow.log_param("n_estimators", 100)
    mlflow.log_param("max_depth", 3)
    mlflow.log_metric("accuracy", acc)

    # Log model
    mlflow.sklearn.log_model(clf, "model")

    print("Model accuracy:", acc)

βœ… Step 6: Run MLflow UI

After running the script, start MLflow UI to visualize your experiments:

mlflow ui

Then open http://127.0.0.1:5000 in your browser.

You’ll see:

  • All experiment runs

  • Their accuracy, parameters, and metrics

  • Downloadable artifacts (like model.pkl)


🧰 Step 7: Compare Multiple Experiments

Change the parameters (like max_depth or n_estimators) and rerun the code.
Each run will be automatically tracked β€” you can compare them in the MLflow UI to choose the best model.


🧱 Step 8: Save and Load Model from MLflow

model_uri = "runs:/<run_id>/model"
loaded_model = mlflow.sklearn.load_model(model_uri)
predictions = loaded_model.predict(X_test)

You can get <run_id> from the MLflow UI.


🌍 Step 9: Deploy the Model

You can easily deploy the trained model as a REST API using:

mlflow models serve -m runs:/<run_id>/model -p 1234

Then send a request to test:

curl -X POST -H "Content-Type: application/json" \
    --data '{"dataframe_split": {"columns": [...], "data": [...]}}' \
    http://127.0.0.1:1234/invocations

πŸ“Š Key Takeaways

ConceptDescription
TrackingLogs experiments (metrics, parameters, models)
ProjectsMakes your ML code reproducible
ModelsSimplifies model management and deployment
RegistryVersion control for models

🧩 Real-World Use Cases of MLflow

  • Tracking 100s of experiments in production ML pipelines

  • Comparing model versions for A/B testing

  • Managing model lifecycle (train β†’ test β†’ deploy)

  • Collaborating among data science teams


🧠 Final Thoughts

MLflow is an essential MLOps tool that helps teams build scalable, reproducible, and traceable ML workflows.
Once you integrate MLflow in your pipeline, you’ll never lose track of your experiments again!

More from this blog