Experiment tracking with MLflow and Weights & Biases: Worked Example — Introductory MLOps Practices (NVIDIA-Certified Associate: Accelerated Data Science)
Experiment Tracking with MLflow and Weights & Biases: A Worked Example In the context of MLOps practices , experiment tracking is essential for...
Experiment Tracking with MLflow and Weights & Biases: A Worked Example
In the context of MLOps practices, experiment tracking is essential for managing machine learning workflows effectively. This ensures reproducibility, comparison of model versions, and streamlined collaboration. Two popular tools for experiment tracking are MLflow and Weights & Biases (W&B). This worked example demonstrates how to apply these tools in a realistic scenario of training a GPU-accelerated classification model.
Scenario Overview
You are developing a model to classify images into multiple categories using a GPU-accelerated environment. Multiple experiments with different hyperparameters and architectures will be run. The goal is to track these experiments systematically, compare results, and select the best model for deployment.
Step 1: Setting Up MLflow for Experiment Tracking
- Install MLflow in your Python environment:pip install mlflow
- Initialize an MLflow experiment to group related runs:mlflow.set_experiment("image_classification_experiment")
- Start an MLflow run to track a single experiment iteration:with mlflow.start_run():
- Log parameters such as learning rate, batch size, and architecture:mlflow.log_param("learning_rate", 0.001)
- Log metrics like accuracy and loss after training:mlflow.log_metric("accuracy", accuracy_value)
- Save the trained model for later loading and deployment:mlflow.pytorch.log_model(model, "model")
MLflow Code Snippet
import mlflow import mlflow.pytorch
mlflow.set_experiment("image_classification_experiment")
with mlflow.start_run(): mlflow.log_param("learning_rate", 0.001) mlflow.log_param("batch_size", 64)
Train your model here
model = train_model(learning_rate=0.001, batch_size=64) accuracy = evaluate_model(model)
mlflow.log_metric("accuracy", accuracy) mlflow.pytorch.log_model(model, "model")
Step 2: Using Weights & Biases for Experiment Tracking
- Install W&B:pip install wandb
- Initialize a W&B run with a project name:import wandbwandb.init(project="image_classification")
- Log hyperparameters to W&B:wandb.config.update({"learning_rate": 0.001, "batch_size": 64})
- Log metrics during training to monitor progress:wandb.log({"accuracy": accuracy_value, "loss": loss_value})
- Save and upload the model artifact for version control:torch.save(model.state_dict(), "model.pth")wandb.save("model.pth")
Weights & Biases Code Snippet
import wandb import torch
wandb.init(project="image_classification") wandb.config.update({"learning_rate": 0.001, "batch_size": 64})
Train your model here
model = train_model(learning_rate=0.001, batch_size=64) accuracy = evaluate_model(model)
wandb.log({"accuracy": accuracy}) torch.save(model.state_dict(), "model.pth") wandb.save("model.pth")
Step 3: Comparing and Selecting Models
Both MLflow and W&B provide dashboards to visualize experiment runs, compare metrics, and inspect parameters. Use these tools to:
- Identify the best-performing hyperparameter combinations.
- Analyze trends in model accuracy or loss across runs.
- Manage model versions and facilitate reproducibility.
Step 4: Integration with GPU-Accelerated Workflows
When training models on NVIDIA GPUs, experiment tracking tools seamlessly integrate with frameworks like PyTorch or TensorFlow. This allows logging of GPU-specific metrics (e.g., training time, memory usage) alongside model parameters and metrics, enabling comprehensive performance monitoring.
Summary
This worked example illustrates how to implement experiment tracking using MLflow and Weights & Biases in an accelerated data science workflow. By systematically logging parameters, metrics, and models, data scientists can optimize ML pipelines, improve collaboration, and ensure reproducibility—key introductory MLOps practices validated in the NVIDIA-Certified Associate: Accelerated Data Science exam.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →