Experimenting in Notebooks
This lesson explains where notebooks run, what they are good for, why their results can be hard to reproduce, and how proven code moves into jobs.
Learning outcomes
After completing this lesson, you will be able to:
- Describe where notebooks run in Azure Machine Learning.
- Explain how notebook runs are tracked.
- Identify the reproducibility limits of notebooks.
- Describe how notebook code becomes a job.
Where notebooks run
Azure Machine Learning notebooks run on a compute instance. They can be edited in the studio's notebook editor, in JupyterLab or Jupyter on the instance, or in VS Code on the desktop connected to the instance. The instance has kernels for Python with the SDK, MLflow and common frameworks, and notebooks are stored in the user's folder on the workspace file share.
Packages can be added for a session with %pip install, and a custom kernel can be created from a conda environment. Because the instance is assigned to one user, code in the notebook authenticates to Azure as that user, and access to data follows the user's permissions.
Exploration and prototyping
Notebooks suit the questions asked early in a project: what the data looks like, which features carry signal, whether a simple model can work at all. Data profiling, visualizations and quick experiments are faster interactively than as jobs.
- Load a sample of a data asset with
ml_client.data.get()and pandas or themltablelibrary. - Plot distributions, correlations and missing values to understand the data.
- Train small models quickly on a sample to compare approaches.
- Track each experiment with MLflow, so that promising results are recorded rather than lost when the kernel restarts.
import mlflow
mlflow.set_experiment("churn-exploration")
with mlflow.start_run(run_name="baseline-logreg"):
mlflow.sklearn.autolog()
model = LogisticRegression(max_iter=500).fit(X_train, y_train)On a compute instance, MLflow is already configured to track to the workspace, so runs started in the notebook appear in the studio next to job runs.
The limits of notebooks
Notebooks have properties that make them unsuitable for production training. Cells can be run out of order, so the result can depend on hidden state that the saved notebook does not reproduce. The notebook runs on one interactive node owned by one person. Notebook files are hard to review and test, and they mix code, output and prose.
From notebook to job
The MLOps workflow moves proven notebook code into scripts. Functions are extracted into Python modules under src/, parameters become command-line arguments, and the notebook calls the same functions or submits a job that runs them. The notebook remains useful for exploration and for submitting and inspecting jobs.
from azure.ai.ml import command, Input, Output
job = command(
code="../src", command="python train.py --data ${{inputs.data}} --model-output ${{outputs.model}}",
inputs={"data": Input(type="uri_file", path="azureml:churn-data:3")},
outputs={"model": Output(type="mlflow_model")},
environment="azureml:train-env:2", experiment_name="churn",
)
returned = ml_client.jobs.create_or_update(job)
print(returned.studio_url)Running training as a job from the notebook keeps the interactive convenience while gaining a recorded snapshot, a versioned environment and compute that scales.
Results nobody can reproduce
A data scientist reports an AUC of 0.91 from a notebook. A colleague runs the same notebook from the top and gets 0.84. The notebook has cells that were edited and rerun in different orders over several days, and it contains no MLflow tracking.
The result depended on hidden kernel state, such as a feature computed by an earlier version of a cell. The remedy is to move the preparation and training code into a script with explicit arguments, run it as a command job with a pinned data asset version and environment, and track it with MLflow. Each run then has a recorded snapshot, parameters and metrics that anyone can reproduce.
Restarting the kernel and rerunning the notebook would give a consistent result once, but not a recorded or reviewable one. Saving the notebook with outputs would record the 0.91 without making it reproducible. Moving to a larger compute instance does not affect cell order.
Why can rerunning a notebook from top to bottom give different results from those the author reported?
Show answer
Notebook cells can be run in any order, so the author's results may depend on hidden kernel state from cells run out of order or since changed. Scripts run as jobs remove that state.
Exam Tips
Remember for the exam
- Notebooks run on compute instances, in the studio, JupyterLab or VS Code.
- MLflow tracks notebook runs to the workspace automatically on compute instances.
- Hidden kernel state makes notebook results hard to reproduce.
- Move proven code into scripts and run it as jobs, submitted from the notebook if convenient.