Sub topic 1
Why Track Experiments
Model development is a loop of many small trials. You change a hyperparameter, a feature, a data split or a library version, then train again and look at a number. Tracking is the practice of recording each trial as it happens, so that you can compare trials, reproduce the good ones and explain them later. This chapter explains why that matters. It also sets up the vocabulary the rest of the tutorial uses.
Prerequisites
This chapter assumes you already have the following:
- Basic Python, including context managers (
withblocks) and f-strings. - Experience training a model with scikit-learn or a similar library.
- A basic idea of train/test splits and evaluation metrics such as accuracy.
- Access to a Databricks workspace and a notebook attached to compute. An ML runtime is convenient, because it ships with MLflow and common libraries preinstalled.
You do not need to know MLflow yet. This chapter only introduces the ideas. Later chapters cover the API.
Model development is a search
A trained model is the product of several inputs:
- The training data and how you split it.
- The features you derive from it.
- The algorithm and its hyperparameters.
- The random seeds.
- The code and library versions that ran the training.
Change any one input and the result can change. A typical project tries dozens or hundreds of combinations before one is good enough to ship. Here is a typical untracked sweep:
import json
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
results = []
for c in [0.01, 0.1, 1.0, 10.0]:
model = LogisticRegression(C=c, max_iter=200).fit(X_train, y_train)
results.append({"C": c, "accuracy": model.score(X_test, y_test)})
print(json.dumps(results, indent=2))This works for four trials in one cell. It stops working when the project grows, and the next section shows how.
What goes wrong without tracking
Ad hoc habits include printing numbers, pasting them into a spreadsheet, or encoding settings in file names such as model_v3_final_C10.pkl. They fail in predictable ways.
- Lost results. Output in a notebook cell disappears when you rerun the cell or clear outputs. The best score you saw yesterday may no longer be recoverable.
- Unknown provenance. You hold a model file and a metric, but you can't say which data, code or parameters produced them. The sweep above prints
Cand accuracy, but it does not record the split seed, themax_itervalue or the library versions. - Unfair comparisons. Two numbers are only comparable if they were measured the same way. If one trial used a different split or metric definition, comparing them misleads you, and nothing in a spreadsheet warns you.
- Irreproducible wins. A teammate asks you to rerun "the good model" from last month. If notebook code changed since then, you may be unable to get the same score again.
- Duplicated work. Without a shared record, colleagues repeat trials that someone already ran and discarded.
- No audit trail. When a deployed model behaves badly, you need to trace it back to the training run that created it. File names and memory are not a reliable way to do that.
None of these failures are about model quality. They are bookkeeping failures, and they get more expensive as the number of trials and people grows.
What a good record contains
A useful record of a trial answers the question "what happened, and could I do it again?" That requires several kinds of information, and MLflow gives each a name you will use throughout this tutorial.
| Kind of information | MLflow term | Example |
|---|---|---|
| Settings you chose before training | Parameter | `C = 1.0` |
| Numbers measured about the result | Metric | `accuracy = 0.93` |
| Free-form labels for organizing | Tag | `stage = exploration` |
| Files produced by the trial | Artifact | A confusion-matrix image, a serialized model |
| One execution of your training code | Run | The trial that used `C = 1.0` |
| A named group of related runs | Experiment | All iris tracking trials |
Parameters, metrics and tags are small, structured values that you can filter and sort. Artifacts are files of any size. A run holds all of these for one trial. An experiment holds the runs you want to compare. Later chapters cover each term in detail, starting with experiments and runs.
Two distinctions are worth fixing now:
- A parameter is an input and a metric is an output. Tracking them separately lets you ask "which inputs led to the best outputs?" That question is the core of experiment analysis.
- A tag differs from a parameter because it does not describe how the model was trained. Tags describe the run itself, for example who started it or what stage of the project it belongs to.
The tracking loop
Tracking turns the development loop into one where each iteration leaves a durable record. Each new trial can then start from the best earlier one instead of from memory.
The important edge is the one from comparison back to the next idea. Without logged runs, that edge depends on memory. With them, it depends on a query over recorded data.
How MLflow Tracking fits
MLflow is an open-source platform for managing the machine-learning lifecycle. This tutorial uses its Tracking component, which provides an API for writing records and a store and UI for reading them. A typical interaction has three parts:
- Select an experiment to hold your runs.
- Start a run inside a
withblock, so it closes automatically, even if training raises an exception. - Call logging functions such as
mlflow.log_paramandmlflow.log_metricwhile the run is active.
Tracking code is deliberately light. It sits next to your existing training code and does not change how you train. For common libraries, MLflow can even do the logging for you through autologging, covered in a later chapter.
What Databricks adds
MLflow is open source, and you can run a tracking server yourself. Running one brings chores such as hosting, storage, authentication and backups. On Databricks, those are handled for you. The platform provides:
- A hosted tracking service. Every workspace has one. You do not configure a server address, because notebooks log to it by default.
- Experiments in the workspace. Runs are grouped into experiments that appear in the workspace UI, with a built-in experiment page for sorting, filtering and charting runs.
- Access control. Experiments have permissions, so you decide who can view or edit them. A later chapter covers this.
- Unity Catalog integration. Models you log can be registered in Unity Catalog, which gives them governed three-level names (catalog, schema and model) and aliases such as
champion. - Reproducibility context. Runs launched from notebooks can capture details such as the notebook that produced them, which supports tracing a result back to code.
Because the hosted service is an MLflow tracking server, code that you write against the MLflow API runs on Databricks and elsewhere with little change. Where Databricks differs, mainly in where experiments live and how models are registered, the later chapters point it out.
When tracking pays off
Tracking has a small cost: a few extra lines of code and some storage. The cost is easy to justify in these cases:
- You are tuning hyperparameters or comparing algorithms.
- More than one person works on the same problem.
- A model might be deployed, audited or retrained later.
- Training takes long enough that rerunning to recover a lost result is expensive.
For a throwaway calculation that you will never revisit, tracking can be overkill. A good default is to track anything you might need to explain or repeat in a month. Starting early is cheap, but retrofitting is impossible, because a result you did not log cannot be recovered.
Tracking also has limits. It records what you ask it to record, so a run is only reproducible if you log the seed, data version and other inputs that matter. Autologging captures many of these for supported libraries, but it does not know about choices outside the library call, such as how you built the data split. A later chapter on reproducibility returns to this point.
Where this tutorial goes
The rest of the tutorial follows one iris-classification example through the full lifecycle:
- Core model: experiments and runs, and where they live on Databricks.
- Logging: parameters, metrics, tags, autologging and artifacts.
- Models: logging a model with a signature and an input example.
- Analysis: comparing runs in the UI, querying them with
mlflow.search_runsand grouping tuning trials as nested runs. - Promotion: registering the model in Unity Catalog, assigning an alias and loading it by alias.
- Housekeeping: what a run records for reproducibility, and how permissions control access.
Exercise
A teammate says, "I keep my results in a spreadsheet, so I already track my experiments." Name two pieces of information from this chapter that a spreadsheet of metric values usually misses, and explain why each matters.
Show answer
Two examples are the code or library versions that produced the result, and the artifacts such as the trained model file. Without the code and environment, you cannot reproduce a good score. Without a link from the row to the actual model, you may find a great score but have no model to deploy. Unrecorded parameters, seeds or data splits are also valid answers, because they make comparisons unfair or impossible to repeat.
Exercise
Classify each item as a parameter, a metric, a tag or an artifact: (a) max_iter = 200, (b) a PNG of a confusion matrix, (c) test_accuracy = 0.96, (d) owner = "dana".
Show answer
(a) is a parameter, because it is a setting chosen before training. (b) is an artifact, because it is a file produced by the run. (c) is a metric, because it is a number measured on the result. (d) is a tag, because it is a label that describes the run and not how the model was trained.