Sub topic 3
Where runs go on Databricks: notebook and workspace experiments
This chapter assumes you can write basic Python, have trained a scikit-learn model, and can run a notebook attached to compute in a Databricks workspace. It also assumes you know that an MLflow run is one logged execution of your training code, and that an experiment is a named group of runs.
Two kinds of experiment
Every run belongs to exactly one experiment. On Databricks, an experiment is one of two types.
- A notebook experiment is created for you the first time a notebook logs a run. It is tied to that notebook and has the same name and path.
- A workspace experiment is one you create yourself. It is a separate object in the workspace file tree, and it is not tied to any one notebook.
| Question | Notebook experiment | Workspace experiment |
|---|---|---|
| Who creates it? | Databricks, automatically | You, in the UI or in code |
| Tied to a notebook? | Yes | No |
| Can several notebooks log to it? | Not by design | Yes |
| Best for | Quick exploration | Shared or long-running projects |
If you do nothing, runs go to the notebook experiment. This is convenient for a first try. It becomes a problem when you copy a notebook, move it, or want a teammate's notebook to log into the same place. Runs then end up scattered across several experiments.
Choosing where runs go
Call mlflow.set_experiment before you start a run. Pass an absolute workspace path. If no experiment exists at that path, MLflow creates one. If one exists, MLflow reuses it.
import mlflow
mlflow.set_experiment("/Users/<your-email>/iris-tracking")
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)Replace <your-email> with the email address you use to sign in. The path starts with /, and the parent folder must already exist. A path under /Users/<your-email>/ is a safe choice because that folder is yours.
The run logs one parameter, one metric and one tag. The next chapter covers these in detail. Here, the point is where the run lands: in the iris-tracking experiment, and not in a notebook experiment.
You can also create a workspace experiment in the UI. Open the workspace file browser, choose Create, then choose Experiment. This is useful when you want to set permissions or pick a folder before any code runs.
Common errors
- A relative name. Passing
"iris-tracking"instead of a full path can fail on Databricks, because the name must be an absolute path. Always begin with/. - A missing folder. If
/Users/<your-email>/projects/does not exist,set_experimentfails. Create the folder first. - Calling
set_experimentafterstart_run. The active run is already attached to its experiment. The new setting only affects later runs. - Forgetting to set it in a second notebook. Each notebook session needs its own
set_experimentcall. Without it, that notebook logs to its own notebook experiment.
Finding your experiment
After a run finishes, open the Experiments page from the sidebar, or follow the link shown under the notebook cell. Your workspace experiment is listed under its path. Each row is a run, which you can open to see what it logged.
Exercise
You run mlflow.set_experiment("/Users/[email protected]/churn") in notebook A, then start a run. A teammate runs a different notebook B with no set_experiment call and also starts a run. Where does each run go?
Show answer
Notebook A's run goes to the workspace experiment /Users/[email protected]/churn. Notebook B's run goes to B's own notebook experiment, because B never called set_experiment. For both to share one experiment, B must call set_experiment with the same path before starting its run.