Sub topic 5
Registering a Model from a Run
Prerequisites and the registration flow
This chapter assumes that you know MLflow Tracking concepts such as experiments, runs, parameters, metrics, and artifacts. You should also be able to train a simple scikit-learn model and use a Python virtual environment.
A logged model is a model artifact stored as part of an MLflow run. A registered model is a named entry in the Model Registry. A model version is one registered copy of a logged model. The run remains the source of the artifact and its training context.
The relationship looks like this:
Registering a model does not copy the model's training metrics into the registry as a replacement for the run. The registry adds a stable name and version number so other systems can find and promote the artifact.
Register while logging the model
The most direct approach passes registered_model_name to the model logging function. MLflow creates the registered model if it does not exist. Each later registration creates another version.
The tracking server must use a registry-capable backend store, such as a database. For local development, start one with:
mlflow server \
--backend-store-uri sqlite:///mlflow.db \
--default-artifact-root ./mlartifacts \
--host 127.0.0.1 \
--port 5000Then train, log, and register the model in one run:
import mlflow
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-classifier")
X, y = load_iris(return_X_y=True)
model = LogisticRegression(max_iter=200).fit(X, y)
with mlflow.start_run() as run:
mlflow.sklearn.log_model(
model,
artifact_path="model",
signature=infer_signature(X, model.predict(X)),
registered_model_name="iris-classifier",
)
print(run.info.run_id)infer_signature records the expected input and output structure. This helps detect incompatible data later. The printed run ID identifies the training run that produced the registered version.
| Object | Purpose | Example |
|---|---|---|
| Run | Stores training metadata and artifacts | `run_id = "abc123"` |
| Logged model | Model artifact inside the run | `runs:/abc123/model` |
| Registered model | Stable registry name | `iris-classifier` |
| Model version | One registered revision | `iris-classifier` version `1` |
Register after logging
You can also register an existing logged model. This is useful when validation happens after training or when registration is a separate pipeline step.
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
run_id = "<run-id-from-the-previous-step>"
mv = mlflow.register_model(f"runs:/{run_id}/model", "iris-classifier")
client.set_model_version_tag(
"iris-classifier",
mv.version,
"validation_status",
"passed",
)
print(mv.name, mv.version)The runs:/ URI points to an artifact path inside a particular run. register_model creates the next version and returns a model-version object. Registering the same run URI again creates another version, so pipelines should avoid duplicate registration unless that behavior is intentional.
Exercise
Which URI identifies the logged model artifact produced by run abc123 under the artifact path model?
Show answer
runs:/abc123/model