AI Academy

Structured tracks from the basics to generative AI and agents, with sequential lessons and quizzes.

201lessons

MLOps: Deploying and Operating ML Models Beginner 12 minutes

Experiment Tracking with MLflow

An organized record of every experiment: the parameters, metrics, files and model.

Your progress in the track0%

What do we record?

TypeExamples
ParametersLearning rate, tree depth, seed
MetricsLoss per epoch, AUROC, F1
FilesCurves, confusion matrix, model
TagsData version, researcher name, purpose

An example with MLflow

# pip install mlflow scikit-learn
import mlflow, mlflow.sklearn
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)
mlflow.set_experiment("breast-cancer-rf")
for depth in (3, 6, None):
    with mlflow.start_run(run_name=f"depth={depth}"):
        clf = RandomForestClassifier(max_depth=depth, n_estimators=300, random_state=0).fit(X_tr, y_tr)
        auc = roc_auc_score(y_te, clf.predict_proba(X_te)[:, 1])
        mlflow.log_param("max_depth", depth)
        mlflow.log_metric("val_auc", auc)
        mlflow.sklearn.log_model(clf, "model")

Then run the comparison interface from the terminal in the project folder: mlflow ui.

In this example the test set was used for comparison for simplicity; in practice, compare on a validation set and keep the test set for the end.

Good organization

  • An experiment name for each research question.
  • Tag each run with the data version and the commit hash.
  • Do not delete failed runs; they are part of the scientific record.

Lesson terms

Track experiments
Systematically recording the parameters, metrics and files of each training run to make comparison and reproduction easier.
The model registry
A central repository of model versions with their metadata and status, such as candidate or in production.

Abstract

Experiment tracking records, for each run, its parameters, metrics, output files and code version, so runs can be compared and any model can be recovered. MLflow is a popular open-source tool for this, and it also includes a model registry. Common alternatives: Weights & Biases, TensorBoard for metrics, and other tools. The principle matters more than the tool: no experiment without a record.

Review questions

  1. What do you record for each run?

    The parameters, the metrics, the code and data versions, and output files such as plots and the model.

  2. What is the benefit of a model registry?

    It identifies which version is approved, and it makes promotion, rollback and auditing easier.

Sources and references

  1. MLflow Documentation
  2. TensorBoard
  3. Weights & Biases Documentation

Complete the lesson