Sub topic 4
Logging Parameters, Metrics and Tags
Assume you know basic Python, can train a scikit-learn model, and understand a train/test split. Also assume a local MLflow tracking server is running at http://127.0.0.1:5000. This chapter shows how to record the details that explain a training result.
A machine learning run should answer three questions:
- What settings did the training code use?
- How well did the model perform?
- How should a person find and understand this run later?
MLflow Tracking answers these questions with parameters, metrics, and tags.
Parameters, metrics, and tags
A parameter is an input to a run. Examples include the regularization value C, the test split size, or the number of trees in a random forest. Parameters describe choices made before or during training.
A metric is a numeric measurement produced by a run. Examples include accuracy, loss, precision, and training time. Metrics are usually used to compare runs.
A tag is descriptive metadata. Tags can identify an owner, branch, model stage, or purpose. Unlike a metric, a tag is not normally used as a numeric score.
| Item | Purpose | Example |
|---|---|---|
| Parameter | Records a training choice | `C = 1.0` |
| Metric | Records a measured result | `test_accuracy = 0.93` |
| Tag | Adds searchable context | `stage = baseline` |
The distinction matters. Do not store an accuracy value as a tag if you want to sort or filter runs by accuracy. Do not store a model stage as a metric because it is descriptive, not a measurement.
Log values inside a run
The following script trains a logistic regression model and records its inputs and result.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-baseline")
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
)
C = 1.0
with mlflow.start_run(run_name="logreg-c1"):
model = LogisticRegression(C=C, max_iter=200)
model.fit(X_train, y_train)
test_accuracy = model.score(X_test, y_test)
mlflow.log_param("C", C)
mlflow.log_param("max_iter", 200)
mlflow.log_param("random_state", 42)
mlflow.log_metric("test_accuracy", test_accuracy)
mlflow.set_tag("stage", "baseline")
mlflow.set_tag("dataset", "iris")
print(f"Test accuracy: {test_accuracy:.3f}")start_run() creates a run context. The logging calls inside that context attach data to the active run. When the block ends, MLflow finishes the run and stores its status.
Parameters should describe values that affect the run. Include random seeds and important preprocessing choices. If a setting can change the result, it usually belongs in the run record.
Metric values must be numeric. A metric can be logged after evaluation, and the same metric name can be logged more than once. MLflow then stores a metric history.
Record metrics over time
For iterative training, pass a step value to log_metric(). A step might represent an epoch, batch, or evaluation checkpoint.
import mlflow
with mlflow.start_run(run_name="stepwise-metrics"):
for epoch in range(5):
train_loss = 1.0 / (epoch + 1)
mlflow.log_metric("train_loss", train_loss, step=epoch)
mlflow.log_metric("validation_accuracy", 0.82, step=4)The MLflow UI can plot train_loss as a curve because each value has a step. Use a consistent meaning for steps within a metric. For example, do not use epochs for some values and batches for others under the same metric name.
Common errors
A frequent error is logging only the final score. A final score without the model settings cannot explain why the result occurred.
Another error is using inconsistent names. Choose one name such as test_accuracy and use it in every run. Names such as accuracy, test_acc, and holdout_score make comparisons harder.
Parameter values are intended to describe a run's configuration. Treat them as fixed after logging. If you need to record a changed value, use a new parameter name or create a new run.
Tags are useful for organization, but they should not replace a reproducible configuration. Record the actual learning rate as a parameter, even if a tag says stage=experiment.
Exercise
You run five models with different values of C. Which value should be a parameter, which value should be a metric, and which value could be a tag?
Show answer
The C value is a parameter. The resulting validation or test accuracy is a metric. A label such as stage=baseline or owner=alex could be a tag.