Skip to main content

Experiment Tracking with MLflow

MLflow is an open-source platform for tracking and managing machine learning experiments. It can record:
  • Parameters used during training
  • Metrics produced during training
  • Artifacts such as models, plots, and files
  • Runs representing individual training experiments
  • Experiments grouping related runs

MLflow workflow


1. Install MLflow

For the example below, also install scikit-learn:

2. Important MLflow Concepts

Experiment

An experiment groups related machine learning runs. Example:
Create or select an experiment using:

Run

A run represents one execution of a machine learning experiment. For example, changing the learning rate and training the model again creates another run.

3. Logging Parameters

Parameters are values configured before or during training. Examples:
Log a parameter:
Multiple parameters:

4. Logging Metrics

Metrics measure model performance. Examples:
Example:
Multiple metrics:

5. Logging Artifacts

Artifacts are files generated during an experiment. Examples:
  • Trained model
  • CSV files
  • Images
  • Confusion matrix
  • Training plots
  • Text reports
Example:

6. Starting an MLflow Run

Use:
Everything logged inside this block belongs to that run. Example:

7. Simple Training Example

The following example trains a Linear Regression model and tracks the experiment.

mlflow_code.py

Install dependencies

Run the program

Start MLflow UI

After the training run:
Then open the local MLflow address shown in the terminal.

Output:

The run will contain:

8. Step-by-Step Explanation

Step 1: Import MLflow

Provides MLflow functionality for experiment tracking.

Step 2: Load Dataset

Loads the built-in Diabetes regression dataset from scikit-learn.

Step 3: Separate Features and Target

X contains the input features. y contains the target values.

Step 4: Split the Dataset

80% of the data is used for training and 20% for testing.

Step 5: Create an Experiment

Creates or selects an MLflow experiment.

Step 6: Start a Run

Starts a new MLflow run.

Step 7: Create the Model

Creates a Linear Regression model.

Step 8: Log Parameters

Stores the configuration used for the experiment.

Step 9: Train the Model

Trains the model using the training data.

Step 10: Generate Predictions

Generates predictions for the test dataset.

Step 11: Calculate Metrics

Calculates model performance.

Step 12: Log Metrics

Stores the performance metrics in MLflow.

9. Run the Example

Example output:

10. Start MLflow UI

Run:
MLflow starts a local tracking server. The terminal will provide the local address. Open the displayed address in a browser. The MLflow UI provides information such as:

11. Comparing Runs

Suppose three experiments are performed:
MLflow makes it possible to compare these runs. This helps determine which configuration produced better results.

12. Parameters vs Metrics vs Artifacts


13. Important MLflow Components

MLflow commonly includes four major areas:

MLflow Tracking

Tracks:
  • Parameters
  • Metrics
  • Artifacts
  • Models
  • Runs

MLflow Projects

Packages machine learning code in a reproducible format.

MLflow Models

Provides a standard format for saving and serving models.

MLflow Model Registry

Manages model versions and lifecycle stages. Example:

14. MLflow Model Registry

The Model Registry can be used to manage models through their lifecycle. Typical workflow:
Important concepts include:
  • Model versions
  • Model aliases
  • Model metadata
  • Model lifecycle management

15. Why Experiment Tracking Is Important

Without experiment tracking:
It becomes difficult to remember which configuration produced a particular result. With MLflow:
This makes experiments reproducible and easier to compare.

16. Key Commands

Install:
Run training:
Start UI:
Start a run:
Log parameter:
Log metric:
Log artifact:
Set experiment:

17. Complete Experiment Tracking Flow