Sub topic 7
Framework integrations and autologging
This chapter assumes basic Python and command-line use, experience training a model, and familiarity with hyperparameters, metrics, and train/validation splits. You should also understand that a tracking client sends data to a tracking service or storage system.
What framework integrations do
A framework integration connects a tracker to a machine learning library such as scikit-learn, PyTorch, TensorFlow, or Keras. It knows where that library exposes useful events:
- A model is created with particular parameters.
- Training begins and ends.
- A batch or epoch produces a metric.
- A model is fitted and can be saved.
- An evaluation function produces a score.
Without an integration, your training code calls the tracker directly. With an integration, the tracker adds hooks around framework operations and records some information automatically. This is called autologging.
Autologging usually captures parameters, metrics, timing information, and sometimes models or dataset metadata. The exact fields depend on the framework, tracker, and library version.
MLflow autologging
MLflow provides framework-specific functions such as mlflow.sklearn.autolog() and mlflow.pytorch.autolog(). The integration observes supported framework calls and logs the results to the active MLflow run.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
mlflow.set_tracking_uri("file:./mlruns")
mlflow.set_experiment("iris-autologging")
mlflow.sklearn.autolog(log_models=False)
features, labels = load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = train_test_split(
features,
labels,
test_size=0.25,
random_state=42,
)
with mlflow.start_run(run_name="logistic-regression"):
model = LogisticRegression(max_iter=200)
model.fit(x_train, y_train)
predictions = model.predict(x_test)
test_accuracy = accuracy_score(y_test, predictions)
mlflow.log_metric("test_accuracy", test_accuracy)
print("Training complete")The scikit-learn integration logs supported estimator parameters and training metrics. The explicit test_accuracy call is still useful because it records a metric calculated outside the estimator's own training method.
MLflow autologging is opt-in for each framework. You can enable several integrations, but broad activation can make it harder to understand which integration produced a field.
Weights & Biases integrations
Weights & Biases, often called W&B, commonly uses an explicit run created with wandb.init(). Framework-specific integrations then connect training events to that run. For example, a Keras training job can use a W&B callback to record epoch metrics.
import wandb
from wandb.integration.keras import WandbMetricsLogger
run = wandb.init(
project="iris-models",
name="keras-baseline",
config={"epochs": 10, "batch_size": 32},
)
# Example for a compiled Keras model named `model`:
# model.fit(
# x_train,
# y_train,
# epochs=run.config["epochs"],
# batch_size=run.config["batch_size"],
# callbacks=[WandbMetricsLogger()],
# )
run.log({"test_accuracy": 0.93})
run.finish()The callback records metrics emitted by Keras. The run remains explicit, so the training script controls its name, project, configuration, and lifecycle. W&B also provides integrations for other frameworks, but their installation paths and captured fields vary. Check the integration documentation for the framework version used by your project.
Common autologging errors
Autologging reduces boilerplate, but it does not remove the need for design decisions.
- Duplicate metrics: A callback may log
val_loss, while application code logs the same value under another name. Choose one naming convention. - Unexpected runs: An integration may create a run when no active run exists. Start runs explicitly when run boundaries matter.
- Incomplete evaluation: Training integrations usually observe
fitor the training loop. They may not capture a later test-set evaluation unless you log it yourself. - Version incompatibility: Hooks depend on framework internals. Pin compatible versions and test upgrades.
- Sensitive data: Parameters, file paths, environment values, and dataset examples can contain secrets or private information. Inspect what is logged before enabling autologging in a shared environment.
- Unclear steps: A metric needs a meaningful step, such as epoch, batch, or global training step. Mixing step definitions makes charts misleading.
Choosing between automatic and explicit logging
Automatic logging is useful during exploration because it gives fast visibility with little code. Explicit logging is better for stable pipelines where the team needs a documented schema.
A practical pattern is to combine both:
- Enable the framework integration for standard parameters and training metrics.
- Explicitly log the dataset version and split definition.
- Explicitly log business or test-set metrics.
- Disable automatic model logging when models are too large or are stored elsewhere.
- Review one run before scaling to many experiments.
Exercise
Your tracker shows two validation-loss curves for every epoch, and one curve has no clear step values. What should you inspect first?
Show answer
Inspect whether both an automatic callback and explicit application code log validation loss, then give the remaining metric one consistent step definition such as epoch number.