Unleashing the Magic of MLflow: An Enchanting Guide to Machine Learning and MLOps 🚀🔮

Ashwin N
6 min readJul 25, 2023

--

MLflow: Where Spells and Data Collide! 🧙‍♂️✨🔮

MLflow can help for collaboration and reproduction in a simplest way here!

Introduction

Welcome to the mystical realm of machine learning, where sorcery meets science, and data transforms into powerful spells! In this enchanting guide, we’ll embark on an exciting journey to discover the magic of MLflow. MLflow is an open-source platform that empowers machine learning practitioners to track experiments, share projects, and deploy models with ease. So, ready your wands (keyboards) and let’s begin our magical adventure.

The Three Enchanting Pillars of MLflow

Prepare to be enchanted by the three pillars of MLflow: Tracking, Projects, and Models. Each one plays a vital role in shaping the magical landscape of machine learning. Let’s explore these mystical components one by one:

1. MLflow Tracking: The Sorcerer’s Journal

At the heart of MLflow lies the powerful Tracking component — the sorcerer’s journal that diligently logs every aspect of our machine learning experiments. Think of it as your enchanted diary, keeping track of parameters, metrics, and artifacts with ease. With MLflow Tracking, we gain invaluable insights into the performance of different model spells and unlock the secrets of successful incantations!

Example:

import mlflow

# Start an MLflow tracking run
mlflow.start_run()

# Log parameters
mlflow.log_param("hidden_units", 128)
mlflow.log_param("learning_rate", 0.001)

# Train the model and log metrics
# ...

# Save the model as an artifact
# ...

# End the run
mlflow.end_run()

Learn more about MLflow Tracking: Official MLflow Tracking Documentation

2. MLflow Projects: The Gateway to Reproducibility

Step into the enchanted world of MLflow Projects — your gateway to reproducibility and collaboration. MLflow Projects helps us organize our magical code, data, and dependencies, ensuring that fellow enchanters can recreate our spells with ease. Together, we create a harmonious ecosystem of machine learning wonders!

Example:

hand_digits_project/
│ MLproject
│ conda.yaml
│ train.py

└───data
│ │ digits.csv
│ │ ...

└───models
│ ...

Learn more about MLflow Projects: Official MLflow Projects Documentation

3. MLflow Models: The Artifacts of Enchantment

The final pillar, MLflow Models, allows us to turn our machine learning models into precious artifacts. With versioning and deployment support, we ensure that our magical creations stand the test of time and bring wonders to the world beyond our spellcasting chamber!

Example:

import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier

# Load the enchanted dataset
# ...

# Create and train the model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Save the model as an MLflow artifact
with mlflow.start_run() as run:
mlflow.log_param("n_estimators", 100)
mlflow.sklearn.log_model(model, "hand_digits_model")

Learn more about MLflow Models: Official MLflow Models Documentation

Unveiling the Enchantment: Hand Digits Recognition with MLflow

Now, let’s immerse ourselves in the real enchantment of MLflow by creating a magical spell — hand digits recognition! Our mission is to train a model capable of deciphering handwritten digits, turning scribbles into meaningful predictions. Fear not; with MLflow by our side, we shall succeed!

Example:

import mlflow
import mlflow.tensorflow
import tensorflow as tf
import plotly.express as px
import matplotlib.pyplot as plt
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
import numpy as np

# Load the MNIST dataset and normalize the pixel values
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0

# Define the Deep Neural Network (DNN) model using TensorFlow
def create_model():
model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
return model

# Training the DNN model
def train(model):
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_data=(test_images, test_labels))
return history

# Save learning curve plot using Plotly
def save_learning_curve(history):
# Create a Plotly figure for the learning curve plot
fig = px.line(history.history, y=['accuracy', 'val_accuracy'], labels={'index': 'Epoch', 'value': 'Accuracy'},
title='Learning Curve - Accuracy')
# Save the plot as an HTML file
fig.write_html("learning_curve.html")
# Log the plot as an MLflow artifact
mlflow.log_artifact("learning_curve.html")

# Save the DNN model and its metrics using MLflow
def save_to_mlflow():
with mlflow.start_run() as run:
# Log the hyperparameters for the run
mlflow.log_param("epochs", 5)
mlflow.log_param("batch_size", 64)
mlflow.log_param("learning_rate", 0.001)

# Create and train the DNN model
model = create_model()
history = train(model)

# Log the learning curve plot using Plotly and save it as an artifact
save_learning_curve(history)

# Log the training and validation accuracy metrics
mlflow.log_metric("final_train_accuracy", history.history['accuracy'][-1])
mlflow.log_metric("final_val_accuracy", history.history['val_accuracy'][-1])

# Log the TensorFlow model
mlflow.tensorflow.log_model(model, "hand_digits_dnn_model")

# Save the DNN model, parameters, and metrics using MLflow and Plotly plots
save_to_mlflow()

TensorFlow Documentation: TensorFlow Official Documentation

MNIST Dataset Documentation: MNIST Dataset on TensorFlow

In this code, we’ve used TensorFlow to create the Deep Neural Network (DNN) model for hand digits recognition, using the popular MNIST dataset. The training and evaluation processes are performed using TensorFlow APIs. MLflow is used to log the training parameters, such as the number of epochs, batch size, and learning rate. Additionally, the test accuracy metric is recorded during the model evaluation. The trained DNN model is saved as an MLflow artifact, enabling easy access and deployment. MLflow’s integration with TensorFlow makes it effortless to track, manage, and share TensorFlow models and their associated metadata, enhancing the MLOps experience.

Tracking and Managing Experiments Remotely

MLflow enables us to manage our experiments not only locally but also remotely! With MLflow Tracking, we can easily log our experiments to a remote tracking server, making it accessible from anywhere. This allows for seamless collaboration with other enchanters or sharing progress with curious observers!

To run MLflow with remote tracking, set the MLFLOW_TRACKING_URI environment variable to the desired tracking server URL:

export MLFLOW_TRACKING_URI=http://your-remote-server:5000

Now, when you start your MLflow runs, they will be tracked on the remote server:

import mlflow

# Start an MLflow tracking run (tracked remotely)
mlflow.start_run()

# Log parameters and metrics
# ...

# End the run (results are now available on the remote server)
mlflow.end_run()

With this magical capability, you can collaborate with other enchanters across the mystical realm of machine learning and share the wonders of your experiments!

Parameters and Metrics got saved
Provides options for inferences
Learning Curve Accuracies

Learn more about MLflow Tracking with Remote Servers: Remote Server Setup in MLflow

Embracing MLOps with MLflow

MLOps, the art of managing machine learning operations, is an enchanting practice where MLflow truly shines. By integrating MLflow into your MLOps workflow, you can enchant your machine learning pipelines, enabling seamless collaboration, reproducibility, and model deployment.

1. Experiment Tracking in MLOps

With MLflow Tracking, you can monitor, compare, and evaluate your experiments during the MLOps lifecycle. Track model performance, hyperparameters, and artifacts to understand the effectiveness of your machine learning spells and make informed decisions.

2. Collaboration and Reproducibility

MLflow Projects empower enchanters to collaborate seamlessly by organizing code, data, and dependencies. Embrace version control and ensure reproducibility across different environments, ensuring consistent results throughout the MLOps journey.

3. Effortless Model Deployment

Enchant the world by deploying your machine learning models effortlessly with MLflow Models. Package your models as artifacts, version them, and deploy them to production or any other environment with a wave of your wand.

The Grand Finale: Unleashing the Power of MLflow and MLOps 🌟

Congratulations, fellow enchanters! You have now mastered the art of MLflow and experienced its magic firsthand. With MLOps at your side, you wield the power to streamline machine learning operations, collaborate effectively, and deploy models with ease. Armed with these enchanting powers, you are ready to conquer the mystical realm of machine learning and create wonders that will leave a lasting impact!

Unravel More Secrets of MLflow and MLOps

To continue your magical journey with MLflow and MLOps, visit the official website at https://mlflow.org/. There, you will find extensive resources, documentation, and tutorials to become a true sorcerer of MLOps.

MLflow GitHub Repository: MLflow on GitHub

MLOps Resources: MLOps Resources

MLflow YouTube Channel: MLflow YouTube Channel

May your machine learning endeavors be filled with endless enchantment and remarkable discoveries! Happy spell-casting and may your data forever be insightful and your models, truly magical! 🧙‍♂️✨

--

--

Ashwin N

Lead Data Scientist 🧙‍♂️ | Exploring the AI Wonderland 🔬 | Sharing Insights on Data Science 📊 | Join me in https://medium.com/@ashwinnaidu1991