Ir para o conteúdo

Lição 7 · Autologging and Comparison

Autologging

Use MLflow autologging to capture parameters, metrics, signatures and models with one line, and learn what it records, what it misses and how to control it.

Writing every log_ call by hand is repetitive. Autologging hooks into supported libraries and records common information for you. It is a fast start, but you still need to know what it captured and what it did not.

One line to turn it on

Python
import mlflow
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

mlflow.autolog()

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

with mlflow.start_run(run_name="logreg"):
    model = LogisticRegression(max_iter=5000)
    model.fit(X_train, y_train)
    mlflow.log_metric("test_accuracy", model.score(X_test, y_test))

Call mlflow.autolog() before you train. The training call of the library is where logging happens.

What gets recorded

The details depend on the library. The documentation lists these kinds of information:

  • Parameters, including library defaults that you did not set.
  • A preselected set of metrics.
  • A model signature (the input and output schema).
  • Artifacts, such as checkpoints in some libraries.
  • The training dataset, where that applies.

Documented integrations include scikit-learn, XGBoost, LightGBM, Keras and TensorFlow, PyTorch (through PyTorch Lightning), Spark and Statsmodels, among others. The list grows, so check the autologging page for your library.

Logging defaults is useful. A library upgrade may change a default, and your run records what was actually used.

Control what autologging does

You can turn it off for one library, add tags, or switch off signature logging:

Python
mlflow.autolog(extra_tags={"team": "risk"})
mlflow.sklearn.autolog(disable=True)           # turn off for scikit-learn
mlflow.autolog(log_model_signatures=False)

There are more options than shown here, and library-specific functions such as mlflow.sklearn.autolog have their own arguments. Read the API reference for your version before relying on one.

Autologging may save a trained model and input details automatically. If your training data is sensitive, check what ends up in artifacts before you point it at a shared server.

What it does not do

  • It does not decide your business metric. Log that yourself.
  • It does not turn your test set into a trustworthy estimate. In the example we log test_accuracy explicitly, so we know what it means.
  • It does not record why you ran the experiment.
  • It does not guarantee reproducibility. Data versions and seeds are still your job.

Mixing automatic and manual logging is normal. Start the run yourself, let autologging fill the routine details, then add your own metrics and tags in the same run.

How the idea works

Autologging wraps the training method of a library. This stand-in shows the wrapping technique with a toy class. It imitates the idea only.

Python Stand-in: wrap fit() to record settings automatically

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that rounds was recorded even though you did not set it. Notice also that only the training error appears: nothing measured new data.

Autologging for LLM libraries

Autologging also exists for LLM and agent libraries, where it mainly creates traces of calls. For example, mlflow.anthropic.autolog() and mlflow.openai.autolog() capture requests and responses. The auto tracing lesson explains this.

A sensible workflow

  1. Turn on autologging at the top of your script.
  2. Run one small training job.
  3. Open the run and read every logged item once.
  4. Add missing items: test metrics, data version, notes.
  5. Disable anything you do not want, such as artifacts that are too large.

Review checklist for your first autologged run

Open the run and answer these questions:

Question Why it matters
Which parameters appear? Confirms the settings that were really used
Which data do the metrics come from? Training metrics can look far better than reality
Was a model saved, and how large is it? Affects storage cost
Is there an input example? It may contain real rows of data
What did I expect but not find? Shows what you must log by hand

The second question is the most important. A metric computed on training data tells you how well the model fits what it has seen. It says little about new data. Read the metric name and the documentation before you quote a number.

Autologging with searches and pipelines

Libraries such as scikit-learn let you combine preprocessing and a model in a pipeline, or search settings with a grid search. Autologging may create a parent run with child runs for a search, and the exact behavior and limits depend on the library and version. Try it on a tiny grid first and look at what appears. A grid with thousands of combinations can create a very large amount of tracking data.

When to log by hand instead

Manual logging is better when:

  • You use a library without an integration.
  • Your training loop is custom and the wrapper cannot see it.
  • You need strict control over what is stored, for example with sensitive data.
  • You want identical metric names across different libraries.

The last reason is often forgotten. Two libraries may name similar metrics differently. If you compare them in one experiment, log your own metric with one shared name, computed by one shared function. Then the comparison is fair, because the calculation is the same.

Reproducible habits

Autologging does not replace a seed, a data version or a code version. Add these as parameters or tags in the same run. Together with the automatic details, they give you a record you can actually use months later.

Common mistakes

Do not assume that autologging covers a library just because it covers a similar one. If nothing appears after training, check that the library and its version are supported, that autologging was enabled before the training call, and that you are looking at the right experiment.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. When should you call mlflow.autolog()?

    It must hook into the library before the training call happens.
  2. 2. Which item does autologging NOT decide for you?

    Only you know what success means for your product, so log that metric yourself.
  3. 3. How do you turn off autologging for scikit-learn only?

    The library-specific autolog function accepts disable=True.

Summary

  • mlflow.autolog() records routine details for supported libraries.
  • It logs library defaults, but not your goals or reasoning.
  • Add manual metrics and tags to the same run.
  • Review captured artifacts, especially with sensitive data.

Next: Compare Runs and Choose a Winner

Seu progresso fica salvo neste navegador. Não precisa de conta.