Machine Learning Workflow Using MLflow: A Beginner‘s Guide

Machine learning powers many applications we use every day, from movie recommendations to fraud detection. However, developing and deploying successful ML models involves far more than just choosing an algorithm and training it on data. Data scientists need to track experiments, compare results, reproduce work, and collaborate with teammates. This is where MLflow comes in.

MLflow is an open source platform to manage the complete machine learning lifecycle, from tracking experiments to deploying models in production. Developed by Databricks, it has seen rapid adoption in the data science community since its launch in 2018. According to the 2020 Kaggle Machine Learning & Data Science Survey, 19.4% of data scientists and ML engineers reported using MLflow, making it one of the most popular ML lifecycle tools.

In this guide, we‘ll introduce MLflow and demonstrate how you can use it to track experiments, package projects, and manage models. Whether you‘re a beginner or an experienced practitioner, MLflow can help you be more productive and collaborate more effectively.

The Challenge of Reproducibility in ML

Reproducing machine learning experiments is notoriously difficult. A 2016 survey in Nature found that more than 70% of researchers have tried and failed to reproduce another scientist‘s experiments. In the fast-moving field of ML, this problem is compounded by the complexity of experiments, with many moving parts including:

  • Data preprocessing steps
  • Feature engineering
  • Model hyperparameters
  • Software dependencies
  • Evaluation metrics
  • Trained model artifacts

Manually keeping track of all these components is a major challenge. Experiments are often run in interactive notebooks where parameters and metrics may not be systematically recorded. Code and datasets are not always properly versioned. Results may be scattered across different machines. All of this makes it hard to reproduce past work, compare experiments, and collaborate with teammates.

MLflow aims to tackle these challenges by providing a standard format for packaging reusable ML code and a central platform to track experiments and share models. Let‘s dive in and see how it works.

Key Concepts in MLflow Tracking

The foundation of MLflow is experiment tracking. MLflow Tracking is an API and UI for logging parameters, code versions, metrics, and output files when running your machine learning code and for later visualizing the results.

Here are the key concepts to understand:

  • Experiment: An experiment is a named collection of runs (e.g. "fraud_detection_2023"). You can create experiments through the MLflow UI or via the mlflow.create_experiment() function.

  • Run: A run corresponds to a single execution of data science code. Each run records the following information:

    • Parameters: Key-value input parameters. Both keys and values are strings. Example: mlflow.log_param(‘learning_rate‘, ‘0.01‘)

    • Metrics: Key-value metrics, where the value is numeric. Each metric can be updated throughout the course of the run. Example: mlflow.log_metric(‘AUC‘, 0.95)

    • Tags: Key-value string tags that provide metadata for the run. Example: mlflow.set_tag(‘model_version‘, ‘v2.3‘)

    • Artifacts: Output files in any format. Example: mlflow.log_artifact(‘confusion_matrix.png‘)

  • Model: An MLflow Model is a standard format for packaging a model so that it can be used in various downstream tools. Each model is saved as a directory containing arbitrary files and a descriptor file that lists several "flavors" the model can be used in (e.g. a Python function flavor, an R function flavor).

  • Model Registry: The Model Registry is a centralized model store, set of APIs, and UI, to collaboratively manage the lifecycle of an MLflow Model. It provides model lineage (which MLflow Experiment trained the model), model versioning, stage transitions (e.g. from staging to production), and annotations.

Hands-On Example: Tracking a Scikit-Learn Model

Let‘s see MLflow in action with a simple example. We‘ll train a random forest model on the Iris dataset and log parameters, metrics, artifacts, and the trained model using MLflow Tracking.

First, install MLflow:

pip install mlflow

Next, train the model and log results to MLflow:

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

# Load data 
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Start MLflow experiment
mlflow.set_experiment(‘iris_classification‘)

with mlflow.start_run(run_name=‘random_forest‘):

    # Train model
    n_estimators = 100
    max_depth = 5
    model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth)
    model.fit(X_train, y_train)

    # Make predictions    
    y_pred = model.predict(X_test)

    # Log params, metrics, and model
    mlflow.log_param(‘n_estimators‘, n_estimators)
    mlflow.log_param(‘max_depth‘, max_depth)   
    mlflow.log_metric(‘accuracy‘, accuracy_score(y_test, y_pred))
    mlflow.sklearn.log_model(model, ‘model‘)

    # Log artifact (confusion matrix plot)
    from sklearn.metrics import plot_confusion_matrix
    fig = plot_confusion_matrix(model, X_test, y_test, cmap=‘Blues‘)
    mlflow.log_figure(fig, ‘confusion_matrix.png‘)

This code does the following:

  1. Loads the Iris dataset and splits it into train and test sets
  2. Creates a new MLflow experiment named "iris_classification"
  3. Starts a new run within this experiment
  4. Trains a random forest model, logging the hyperparameters n_estimators and max_depth
  5. Makes predictions on the test set and logs the accuracy metric
  6. Saves the trained model as an artifact in MLflow‘s format
  7. Creates a confusion matrix plot and logs it as an artifact

You can view the results in the MLflow UI by running:

mlflow ui

This will launch a local web server at http://localhost:5000 where you can browse and compare runs:

Querying and Comparing Runs

A key benefit of MLflow is the ability to query and compare runs using either the API or UI. For example, to print the accuracy of the best run so far:

from mlflow.tracking import MlflowClient

client = MlflowClient()
runs = client.search_runs(experiment_ids=‘1‘, order_by=[‘metrics.accuracy DESC‘], max_results=1)
print(f"Run ID: {runs[0].info.run_id}, Accuracy: {runs[0].data.metrics[‘accuracy‘]:.3f}")
Run ID: c5d4eb64615a484b82cec66d79c4e488, Accuracy: 0.967

Or to plot accuracy vs n_estimators for all runs:

import matplotlib.pyplot as plt

runs = client.search_runs(experiment_ids=‘1‘, order_by=[‘params.n_estimators ASC‘])
x = [int(run.data.params[‘n_estimators‘]) for run in runs]
y = [run.data.metrics[‘accuracy‘] for run in runs]

plt.plot(x, y)
plt.xlabel(‘Number of Trees‘)
plt.ylabel(‘Accuracy‘)
plt.title(‘Accuracy vs Number of Trees‘)
plt.show()

Logging and Comparing Multiple Models

MLflow makes it easy to log and compare multiple models trained on the same dataset. Continuing our Iris example, let‘s train models with different algorithms:

from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier

models = [
    {‘name‘: ‘random_forest‘, ‘model‘: RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)},
    {‘name‘: ‘adaboost‘, ‘model‘: AdaBoostClassifier(n_estimators=100, random_state=42)}, 
    {‘name‘: ‘logistic_regression‘, ‘model‘: LogisticRegression(random_state=42)},
    {‘name‘: ‘knn‘, ‘model‘: KNeighborsClassifier(n_neighbors=5)}
]

with mlflow.start_run(run_name=‘model_comparison‘):
    for model_dict in models:
        with mlflow.start_run(run_name=model_dict[‘name‘], nested=True):
            model = model_dict[‘model‘]
            model.fit(X_train, y_train)
            y_pred = model.predict(X_test)
            mlflow.log_metric(‘accuracy‘, accuracy_score(y_test, y_pred))
            mlflow.sklearn.log_model(model, ‘model‘)

This trains four models within a parent run named "model_comparison". We can then compare the models in the MLflow UI:

As an ML expert, I‘ve found this ability to easily track and compare experiments invaluable. It allows me to systematically test different algorithms, features, and hyperparameters, and to choose the best approach based on the evidence.

Managing Models with the Model Registry

Once you‘ve found a model you‘re happy with, you‘ll want to transition it to production. This is where the MLflow Model Registry comes in. The Model Registry is a centralized model store to collaboratively manage the lifecycle of MLflow Models.

To log a model to the registry:

mlflow.sklearn.log_model(
        sk_model=model,
        artifact_path="iris_rf",
        registered_model_name="iris_classifier")

This logs the model under the registered model name "iris_classifier". You can then transition the model to different stages (e.g. Staging, Production) via the UI or API:

client = MlflowClient()
model_version = client.get_latest_versions("iris_classifier", stages=["None"])[0].version
client.transition_model_version_stage("iris_classifier", model_version, stage="Production")

The Model Registry enables teams to:

  • Discover registered models, see which experiments train the models, and compare their performance
  • Transition models between stages as they go from development to staging to production
  • Annotate models and track their dependencies
  • Deploy different versions of the model

Using MLflow with Cloud Platforms

MLflow integrates well with cloud ML platforms like AWS SageMaker and Azure ML, making it easy to train and deploy models in the cloud.

For example, to train a model on SageMaker:

import mlflow
import mlflow.sagemaker as mfs

mlflow.set_experiment("sagemaker_test")

with mlflow.start_run() as run:
    estimator = mfs.deploy_model(
        model_uri=model_uri, 
        role_arn="arn:aws:iam::123456789012:role/SageMakerRole", 
        instance_type="ml.m5.xlarge", 
        instance_count=1)

    model_name = "test-model"
    mfs.save_model(role_arn="arn:aws:iam::123456789012:role/SageMakerRole", 
                   model_uri=model_uri,
                   model_name=model_name)

This deploys the model trained in the previous step to a SageMaker endpoint for real-time inference. The model and its parameters are also logged to MLflow Tracking.

Similarly, you can use MLflow to track experiments run on Azure ML:

from azureml.core import Workspace
import mlflow

ws = Workspace.from_config()
mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())

experiment_name = ‘azure-experiment‘
mlflow.set_experiment(experiment_name)

with mlflow.start_run():
    model = LogisticRegression()
    model.fit(X_train, y_train)

    mlflow.log_metric("accuracy", model.score(X_test, y_test))
    mlflow.sklearn.log_model(model, "model")

This logs metrics and artifacts to your Azure ML workspace. You can view the results in either the MLflow UI or the Azure ML studio.

Real-World MLflow Usage

Many companies are using MLflow to manage their production ML workflows. For example:

  • Stylight, a fashion ecommerce company, uses MLflow to track and compare over 100 models that provide personalized product recommendations. Since adopting MLflow, they‘ve seen a 2.4% increase in revenue per session.

  • Databricks uses MLflow internally to track over 1000 experiments per month across 200 users. They‘ve found that MLflow‘s Model Registry helps them deploy models 6x faster.

  • Brandless, an ecommerce startup, uses MLflow to track hundreds of models and ensure reproducibility. Their lead data scientist says, "MLflow is the control center of our machine learning platform. It lets us track every detail of a model and enables seamless collaboration between data scientists and engineers."

These case studies demonstrate the impact MLflow can have on an organization‘s ML productivity and results.

Conclusion

In this guide, we‘ve seen how MLflow helps data scientists track experiments, package projects, and manage models. By providing a standard format for packaging ML code and a central platform to track and share results, MLflow makes it easier to reproduce work and collaborate with teammates.

We walked through a hands-on example of using MLflow to train and compare Scikit-Learn models on the Iris dataset. We also discussed key features like the Model Registry and MLflow‘s integration with cloud platforms like AWS SageMaker and Azure ML.

Whether you‘re working on a personal project or an enterprise ML platform, MLflow can help you be more productive and effective. I encourage you to try it out and see how it can fit into your own workflow.

To learn more, check out these resources:

You can also find the complete code examples from this guide in this GitHub repo.

Happy experimenting!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts