Sub topic 5
Letting autologging do the work
This chapter assumes you can train a scikit-learn model, use a Python context manager, and run a notebook on Databricks attached to compute. You should also know what a run, a parameter, and a metric are from the earlier chapters.
What autologging does
Writing log_param and log_metric calls for every setting gets tedious. Autologging is an MLflow feature that hooks into a training library and records most of that information for you. You enable it with one call before you train.
For scikit-learn, the call is mlflow.sklearn.autolog(). When you then call fit, MLflow records:
- The estimator's parameters, such as
Cormax_iter. - Training metrics, such as training score.
- The fitted model as an artifact, with a signature and input example when it can infer them.
- Tags that identify the estimator class.
A worked example
The example below trains a logistic regression on the iris data. Autologging captures the fit. A manual call adds the test metric, which autologging does not compute for you.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
mlflow.sklearn.autolog()
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
with mlflow.start_run(run_name="autolog-logreg"):
model = LogisticRegression(max_iter=200).fit(X_train, y_train)
mlflow.log_metric("test_accuracy", model.score(X_test, y_test))Open the run in the experiment UI. You will see the parameters you did not log yourself, the training metrics, and a model artifact. Your test_accuracy metric sits beside them in the same run.
The with block matters. If a run is active when fit is called, autologging writes into it. If no run is active, autologging starts one and ends it when fit returns. In that case your manual log_metric call would need an active run to attach to, so wrap the fit and the extra logging in one start_run block.
How the flow fits together
Tuning what gets logged
autolog() accepts options. Two are common:
log_models=Falseskips saving the model artifact. Use it in large sweeps where you only need metrics.disable=Trueturns autologging off for that library.
Check the options available in your MLflow version, because they differ slightly between flavors.
When to add manual logging
Autologging covers what the library exposes. Add manual calls for anything it cannot see:
- Metrics computed on held-out data, as in the example.
- Business metrics, data version identifiers, or dataset sizes.
- Tags that describe your workflow, such as
stage. - Extra files, such as plots, saved as artifacts.
Common errors
- Enabling autologging after training. Autologging patches the library when you call
autolog(). Afitthat ran earlier is not recorded. - Expecting test metrics. For scikit-learn, autologging records training metrics. It also records metrics from
scorecalls made after fitting, but only under names it chooses. Log your own test metric explicitly under a name you control. - Stray runs. Calling
fitseveral times with no active run creates one run per call. Wrap related work in a singlestart_runblock, or use nested runs for sweeps. - Duplicate logging. Logging the same parameter manually that autologging already recorded raises an error if the value differs. Let one mechanism own each name.
Exercise
You call mlflow.sklearn.autolog(), then run LogisticRegression().fit(X_train, y_train) with no start_run block, and afterwards call mlflow.log_metric("test_accuracy", 0.93). Why might the metric not end up in the same run as the autologged parameters, and how do you fix it?
Show answer
With no active run, autologging starts its own run for the fit and ends it when fit returns. The later log_metric call then starts a different run, or fails to attach to the first. Fix it by wrapping both the fit call and the log_metric call in one with mlflow.start_run(): block, so they share the active run.