Aller au contenu

Leçon 13 · Model Registry

Model Registry Basics

Register models in the MLflow Model Registry, understand registered models and versions, load them by URI, and record where each version came from.

Tracking helps you find a good run. The Model Registry answers the next question: which model should other people and systems use? It is a shared, named catalog of models and their versions.

The vocabulary

Term Meaning
Registered model A unique name, such as churn-classifier, with versions, aliases and tags
Model version A numbered snapshot: version 1, 2, 3 and so on
Alias A movable pointer to one version, such as champion
Tag A key and value label on a model or a version

The open source registry needs a database-backed tracking setup, such as SQLite or a server database.

Register a model

You can register while logging:

Python
mlflow.sklearn.log_model(
    model,
    name="churn",
    input_example=X_train.head(3),
    registered_model_name="churn-classifier",
)

Or register a model that you already logged, using its URI:

Python
result = mlflow.register_model(model_uri=info.model_uri, name="churn-classifier")
print(result.version)

The first registration under a name creates version 1. Each later one creates the next number. A version is a snapshot: to change the model, register a new version.

Load by URI

Python
by_number = mlflow.pyfunc.load_model("models:/churn-classifier/3")
by_alias = mlflow.pyfunc.load_model("models:/churn-classifier@champion")

Note the two shapes: /3 selects a number, while @champion selects an alias. Loading by number is exact. Loading by alias follows whatever the alias points to today. The next lesson covers aliases.

Describe versions

Add information so that people can decide without guessing:

Python
from mlflow import MlflowClient

client = MlflowClient()
client.update_model_version(
    name="churn-classifier",
    version="3",
    description="Retrained on November data. Better recall for new users.",
)
client.set_model_version_tag("churn-classifier", "3", "validation_status", "pending")

for mv in client.search_model_versions("name='churn-classifier'"):
    print(mv.version, mv.tags, mv.run_id)

A version keeps a link to where it came from, such as a run or a logged model, so you can trace a production model back to its parameters, metrics and data. A model registered from a source without a tracking record loses much of that value.

How a registry behaves

This stand-in shows version numbering and lookups. It is not the MLflow API.

Python Stand-in: a tiny registry

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.

Each name has its own numbering, and each version remembers its source run.

What belongs in the registry

Register candidates that you might actually use. If you register every tuning trial, the list becomes noise. A good moment to register is after a run passes your first quality checks.

Choose names for the task, not for a person or an algorithm: churn-classifier is better than forest-v2-final. The algorithm may change between versions while the job stays the same.

A model in the registry is not automatically safe or approved. Registration only records it. Define your own review steps, and limit who can register models or move aliases on a shared server.

Managed registries

If your platform offers its own registry service, features such as permissions or naming rules can differ. For example, you can point MLflow at Databricks Unity Catalog with mlflow.set_registry_uri("databricks-uc"). Unity Catalog model versions require a signature. Follow the platform documentation for its naming rules.

Version descriptions that help

A description should help someone decide whether to use a version. A good one answers:

  1. What changed compared with the previous version?
  2. Which data was it trained and evaluated on?
  3. What are the main results and the known weaknesses?
  4. Who can answer questions?

Avoid descriptions such as "improved model". They sound fine today and mean nothing in three months.

Find versions in code

The search function accepts a filter on the name. You can then sort or filter the results in Python, for example by a tag:

Python
versions = client.search_model_versions("name='churn-classifier'")
approved = [v for v in versions
            if v.tags.get("validation_status") == "approved"]
latest = max(approved, key=lambda v: int(v.version))
print(latest.version)

Notice the conversion with int. Version numbers may arrive as text, and text order would put "10" before "9". This mistake is easy to miss until your tenth version.

Do not let production code pick "the highest approved version" on its own, though. An explicit alias is clearer: a person or a pipeline decides what is live, and you can see that decision.

Delete with care

You can delete a version or a whole registered model. The documentation warns that deletion cannot be undone. Before deleting, check whether any service loads that version or alias. A safer habit is to tag old versions as retired, remove aliases from them, and delete only after a waiting period.

Registry and tracking together

The registry does not replace experiment tracking. Tracking holds the full history of experiments, including failures. The registry holds a curated set of versions that matter. When something goes wrong with a live model, you start in the registry, follow the link to the source, and then read the tracking data for parameters, metrics and data versions. Both halves are needed, so keep the source runs even after the version is released.

A common mistake

Do not register a model before you know which data and code produced it. A version with no clear history is hard to trust and hard to replace. If you must register an outside model, record its origin, license and known limits in the description and tags, and test it like any other candidate before anyone uses it.

Also check who may create new registered names. Without a naming rule, a shared registry soon fills with near duplicates, and people cannot tell which entry is the real one.

What to check after registering

Open the new version and confirm its source link, its signature and its tags. Load it once by number and predict on a few rows. A one-minute check catches a wrong source URI before anyone else depends on it.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What number does the first version of a new registered model get?

    Version numbers start at 1 and increase with each registration.
  2. 2. Which URI loads version 3 exactly?

    A slash followed by a number selects that version.
  3. 3. Why is a link back to the source run valuable?

    Lineage helps with debugging, audits and reproducing results.

Summary

  • A registered model has a name and numbered versions.
  • Register while logging or with mlflow.register_model.
  • Load by version number or by alias.
  • Keep descriptions, tags and lineage, and register only serious candidates.

Next: Aliases, Tags and Promotion

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.