Sub topic 3
Where runs go on Databricks: notebook and workspace experiments
Every MLflow run belongs to exactly one experiment. On Databricks, the experiment you choose decides where the run is stored, who can see it, and whether a second notebook can log into the same place. This chapter covers the two kinds of experiment, how MLflow picks one, and how to set it explicitly.
This chapter assumes you can run Python in a Databricks notebook and that you know what an MLflow run is. If not, read the previous chapter on experiments and runs first. You don't need any permissions knowledge yet. Access control comes in a later chapter.
The tracking server is already there
In a Databricks notebook, you don't start or configure a tracking server. The MLflow tracking URI is preset to databricks, which means "the MLflow service of this workspace". Calls such as mlflow.start_run() and mlflow.log_metric() send data there, and the Experiments page in the workspace UI shows the result.
You can check the setting:
import mlflow
print(mlflow.get_tracking_uri())Inside a notebook this prints databricks. If you run the same code from your laptop, you would call mlflow.set_tracking_uri("databricks") and supply workspace credentials yourself. This chapter assumes you work in a notebook.
Two kinds of experiment
Databricks has two kinds of experiment.
- A notebook experiment is tied to one notebook. Databricks creates it for you when the notebook first logs a run without naming an experiment.
- A workspace experiment is an independent object that lives in the workspace folder tree, like a notebook or a folder does. You create it, you name it, and any notebook or job can log to it.
| Property | Notebook experiment | Workspace experiment |
|---|---|---|
| How it is created | Automatically, on first run in the notebook | In the UI, or by `set_experiment` / `create_experiment` with a path |
| Name and location | Same path as the notebook | Any path you choose, such as `/Users/<you>/iris-tracking` or `/Shared/team-models` |
| Who logs to it | That notebook | Any notebook, job, or script that sets it |
| Lifecycle | Follows the notebook | Independent of any notebook |
| Best for | Quick exploration and throwaway work | Shared projects, jobs, and anything you will revisit |
How MLflow picks an experiment
When code starts a run, MLflow resolves the target experiment in this order:
- If an active run exists, a nested run goes to the same experiment as its parent.
- If you called
mlflow.set_experiment(...)earlier in the session, MLflow uses that experiment. - Otherwise, in a notebook, MLflow uses the notebook experiment, creating it if needed.
The notebook experiment
If you run the following in a fresh notebook, with no set_experiment call, the run lands in that notebook's experiment:
import mlflow
with mlflow.start_run(run_name="first-try") as run:
mlflow.log_param("C", 1.0)
mlflow.log_metric("accuracy", 0.91)
print(run.info.experiment_id)The notebook's right sidebar has an Experiment icon. It lists the runs logged from this notebook and links to the full experiment page. The experiment carries the same path as the notebook.
The notebook experiment is convenient because it needs no setup. It has three drawbacks:
- It is bound to one notebook. A training job that calls code from several notebooks, or a teammate who copies your notebook, ends up with separate experiments and separate run lists.
- Its identity follows the notebook. Renaming or deleting the notebook affects where you find the experiment, so it is a poor place for results you want to keep.
- It gives you no stable name to put in documentation, a
search_runscall, or a job definition.
The workspace experiment
A workspace experiment is created in either of two ways.
In the UI. Use Create and choose Experiment, or use the Experiments page. Pick a name and a folder.
In code. Call mlflow.set_experiment with an absolute path. If no experiment exists at that path, MLflow creates one. If one exists, MLflow reuses it.
import mlflow
user = spark.sql("SELECT current_user()").first()[0]
experiment_path = f"/Users/{user}/iris-tracking"
experiment = mlflow.set_experiment(experiment_path)
print(experiment.experiment_id, experiment.name)
print(experiment.artifact_location)
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)set_experiment returns an Experiment object. Its experiment_id is the stable numeric identifier. Its artifact_location shows where files logged with the run will be stored, which becomes important in the artifacts chapter.
Run this cell twice. The second time, the experiment already exists, so both runs appear in the same list. This is the main practical benefit of a workspace experiment. Five notebooks can call set_experiment with the same path and all log to one place.
Rules for the experiment path
- The name must be an absolute workspace path that starts with
/. A bare name such as"iris-tracking"is rejected on Databricks. This differs from open-source MLflow, where plain names work. - The parent folder must already exist and you must be able to write to it.
set_experimentcreates the experiment, not the folders above it. - The path must not collide with an existing notebook or other workspace object. A notebook already has an experiment at its own path, so choose a different name for a workspace experiment.
- Use
/Users/<your-email>/...for personal work. Use a folder such as/Shared/...for work that a team owns.
Selecting by ID, and looking experiments up
You can also select an existing experiment by its numeric ID. This is useful in jobs where the ID is passed in as a parameter:
import mlflow
exp = mlflow.get_experiment_by_name(experiment_path)
print(exp.experiment_id, exp.lifecycle_stage)
mlflow.set_experiment(experiment_id=exp.experiment_id)get_experiment_by_name returns None when no experiment has that name, so check the result before using it. Pass either experiment_name (the first positional argument) or experiment_id to set_experiment, not both.
Gotchas
Call set_experiment before the run starts. The call changes where new runs go. It does not move a run that is already active.
import mlflow
with mlflow.start_run(run_name="early") as run:
# This run is already attached to the notebook experiment.
mlflow.set_experiment(experiment_path) # affects runs started after this point
mlflow.log_metric("accuracy", 0.9) # still logged to the "early" runAfter this cell, early sits in the notebook experiment, and the next start_run goes to the workspace experiment. This mistake produces runs that seem to vanish, because you look in the wrong experiment.
Autologging follows the same rule. mlflow.sklearn.autolog() doesn't choose an experiment. It logs to whatever experiment is active when the training call runs. Put set_experiment at the top of the notebook, above any autolog or training code.
A rerun in the same session reuses your choice. set_experiment stays in effect until you call it again or the Python process restarts. After detaching and reattaching a notebook, the setting is gone and logging falls back to the notebook experiment. Keep the set_experiment line in the first cell so it runs every time.
Jobs and widgets. When a notebook runs as a job, hard-coding /Users/<your-email>/... ties the job to one person's folder. Read the path from a job parameter or use a /Shared/... path. Also check that the job's run-as identity can write to that folder.
Choosing between them
Use the notebook experiment when you are exploring, the notebook is the whole project, and you don't mind losing the results with it. Use a workspace experiment in these cases:
- More than one notebook or job logs to the same project.
- You want a path that you can write down in code, in a job, or in a document.
- The results need to outlive any one notebook.
- A team needs to share the results (you will see how access is controlled in a later chapter).
In practice, most teams move to a workspace experiment as soon as a project has a second notebook or a scheduled job.
Exercise
A teammate runs mlflow.set_experiment("churn-model") in a Databricks notebook and gets an error. Name two things that could be wrong, and write the corrected call for a personal experiment owned by [email protected].
Show answer
The name is not an absolute workspace path, which Databricks requires. A second possible cause is that the path points at a folder that doesn't exist or that the user can't write to, or it collides with an existing notebook. The corrected call is mlflow.set_experiment("/Users/[email protected]/churn-model").
Exercise
A notebook has been logging runs to its notebook experiment. You add mlflow.set_experiment("/Shared/churn") inside an open with mlflow.start_run(): block and log a metric right after it. Where does that metric go, and where does the next run go?
Show answer
The metric goes to the already active run, which is still in the notebook experiment. The next run started after the block goes to /Shared/churn, because set_experiment affects only runs started after it is called.