Sub topic 1
Why Track Experiments
Model development produces many training runs. Each run uses different data, code, parameters and settings, and each gives different results. Without a record, you can't reliably say which run produced your best model or how to reproduce it. This chapter explains the problem that MLflow Tracking solves and previews how the rest of the tutorial fits together.
Prerequisites
This chapter assumes you have:
- Basic Python, including context managers and f-strings.
- Experience training a model with scikit-learn or a similar library.
- A basic idea of train/test splits and evaluation metrics.
- Access to a Databricks workspace and a notebook attached to compute. An ML runtime is convenient because it includes MLflow and scikit-learn.
The problem: results you can't trace
Consider a common workflow. You train a logistic regression on the Iris data set and get 0.93 accuracy. You change the regularization strength, retrain, and get 0.95. Then you change the train/test split and get 0.91. A week later a colleague asks which settings gave the best result. You check your notebook, but cells were re-run out of order and some outputs were overwritten.
Typical workarounds fail in predictable ways:
| Workaround | Why it breaks down |
|---|---|
| Notes in a notebook cell or markdown | Easy to forget or overwrite. Not searchable across runs. |
| Spreadsheet of results | Manual entry causes typos and gaps. Nothing links a row to the code or model file. |
| File names like `model_final_v2.pkl` | Names don't record parameters, data or metrics. |
| Git commits alone | Code is versioned, but metrics and model files are not tied to the commit. |
All of these depend on discipline, and discipline erodes as the number of experiments grows.
What tracking records
Experiment tracking means automatically saving, for every training run, the information needed to understand and repeat it. MLflow Tracking stores these kinds of information:
- Parameters: the inputs you chose, such as the regularization strength
C. - Metrics: numeric results, such as accuracy.
- Tags: free-form labels, such as
stage = exploration. - Artifacts: files produced by a run, such as plots or model files.
- Source and environment details: information that helps you reproduce the run. A later chapter covers these.
Each run's record is saved in one place, and you can then query and compare the records instead of reading notebooks.
What you gain
Tracking supports several concrete tasks:
- Comparison. Sort or filter runs by a metric to find the best one.
- Reproducibility. Look up the parameters and environment of any past run and repeat it.
- Collaboration. Teammates can see what has already been tried, so they avoid repeating work.
- Handoff to deployment. The model you register for use later links back to the run that produced it, so you can trace where it came from.
Why Databricks
MLflow is an open-source project. On Databricks, a managed tracking server is built in. You don't set up a server or storage. Runs from a notebook are saved to an experiment in your workspace, and the Databricks UI lets you browse and compare them. Databricks also integrates with Unity Catalog, where you can register models with governed access.
How the tutorial proceeds
The diagram shows the path through the remaining chapters. You start by logging a run, then add richer records, then compare and query results, and finally register the best model.
You will use these pieces of the MLflow Python API:
mlflow.start_runto open a run.mlflow.log_param,mlflow.log_metricandmlflow.set_tagto record details.mlflow.sklearn.autologto capture most details automatically.mlflow.search_runsto query runs.mlflow.register_modelto register a model.
A first look
You don't need to run anything yet. The next snippet shows how little code a tracked run needs. Later chapters explain each line.
import mlflow
with mlflow.start_run(run_name="baseline") as run:
mlflow.log_param("C", 1.0)
mlflow.log_metric("accuracy", 0.93)
mlflow.set_tag("stage", "exploration")
print(run.info.run_id)The with block opens a run and closes it when the block ends. Everything logged inside belongs to that run. The printed run ID identifies the record.
Common mistakes to avoid
- Tracking only the final model. Failed and weaker runs show what doesn't work, so log them too.
- Logging a metric without the parameters that produced it. A number without its settings can't be reproduced.
- Using inconsistent metric names across runs, such as
accin one andaccuracyin another. This breaks comparison.
Exercise
A teammate keeps results in a spreadsheet and names model files by date. Give two reasons this setup makes it hard to reproduce the best result, and name the MLflow concept that addresses each.
Show answer
First, the spreadsheet is entered by hand and isn't linked to the model file or the code, so a row can be wrong or incomplete. MLflow addresses this by saving parameters and metrics automatically in a run, with the model stored as an artifact of that same run. Second, a date in a file name doesn't record the parameters, data or environment that produced the model. MLflow addresses this by recording parameters, tags and environment details for each run, so you can look up exactly what was used.
Exercise
Which of these is a parameter and which is a metric: the regularization strength C, and the test accuracy?
Show answer
C is a parameter because it is an input you choose before training. Test accuracy is a metric because it is a numeric result measured from the run.