Experiment Tracking with MLflow
An organized record of every experiment: the parameters, metrics, files and model.
What do we record?
| Type | Examples |
|---|---|
| Parameters | Learning rate, tree depth, seed |
| Metrics | Loss per epoch, AUROC, F1 |
| Files | Curves, confusion matrix, model |
| Tags | Data 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
-
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.
-
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
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate