Sub topic 2
Registered Models and Model Versions
Assume you know MLflow Tracking, including experiments, runs, parameters, metrics, and artifacts. You should also be comfortable training and saving a basic scikit-learn model.
A training run produces a model artifact. An artifact is a file or directory saved by MLflow, such as a serialized model and its environment information. The Model Registry gives that artifact a stable name, version number, and metadata so other systems can use it reliably.
The three related objects
A logged model artifact is the model output stored inside a run. The run records how the model was created, including its parameters, metrics, source code, and artifact location.
A registered model is a named entry in the registry. It represents a logical model, such as iris-classifier, across its entire lifetime. It is not one particular trained model.
A model version is one registered copy of a model artifact. Each registration creates a new integer version under a registered model. Version 1 and version 2 can come from different runs and can contain different model files.
The relationship is one-way: a version points to the artifact produced by a run. Registering the artifact does not retrain the model or copy the training history into a new run.
| Object | Identifies | Typical examples |
|---|---|---|
| Run | One training or evaluation attempt | Run ID, metrics, parameters |
| Logged model | Model files produced by a run | `runs:/run-id/model` |
| Registered model | A stable model name | `iris-classifier` |
| Model version | One registered artifact revision | Version `1`, version `2` |
Creating versions while logging
You can create a registered model version during model logging. The registered_model_name argument tells MLflow to place the logged model in that registered model. If the name does not exist, MLflow creates it. If it already exists, MLflow creates the next version.
A model signature describes the expected input columns and output shape. Recording it helps detect incompatible inputs before deployment.
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:
model_info = 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)
print(model_info.model_uri)The first successful registration might create iris-classifier version 1. A later run might create version 2. Version numbers identify registry entries, not model quality. Version 2 is not automatically better than version 1.
Registering an existing logged model
You can also register an artifact after the run finishes. Use a run URI that identifies both the run and the artifact path.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
run_id = "replace-with-a-real-run-id"
model_version = mlflow.register_model(
f"runs:/{run_id}/model",
"iris-classifier",
)
print(model_version.name)
print(model_version.version)The URI runs:/... is a reference to the artifact location, not a copy of the run itself. Keep the run and its artifacts available to the registry backend. A registry entry without accessible artifacts cannot be loaded successfully.
In later chapters, aliases will provide names such as champion that point to selected versions. For now, focus on the distinction: the registered model is the container, and each model version is a specific artifact from a specific run.
Exercise
A team trains three models in three separate runs and registers all of them as "fraud-detector". How many registered models and model versions should exist?
Show answer
One registered model, "fraud-detector", with three model versions. Each version points to the artifact from one training run.