Sub topic 1
What the Model Registry Is For
This chapter introduces the MLflow Model Registry, a central place where trained models get names, version numbers, and documentation. It covers the tracking server that hosts the registry, the objects the registry stores, and how a model gets its signature when it is logged.
Prerequisites
You should know basic Python and how to use a virtual environment. You should also be familiar with MLflow Tracking concepts such as experiments, runs, parameters, metrics, and artifacts. This chapter assumes you can train and save a simple scikit-learn model and use a terminal.
The Problem the Registry Solves
A tracking run saves a model as an artifact in a folder. Runs pile up, and nothing in the run says which model is ready for production. The Model Registry adds a named catalog on top of the artifacts. Each entry has a name, a history of versions, and room for notes.
Registered Models and Model Versions
A registered model is a named entry, such as iris-classifier. A model version is one numbered snapshot under that name. Each version points back to the run and artifact it came from. Version numbers start at 1 and increase by one each time you register a new model under the same name.
Running a Tracking Server That Supports the Registry
The registry needs a database backend. The default file store does not support it. Start a local server with a SQLite database:
mlflow server \
--backend-store-uri sqlite:///mlflow.db \
--default-artifact-root ./mlartifacts \
--host 127.0.0.1 \
--port 5000The SQLite file stores metadata. The artifact folder stores model files.
Logging a Model with a Signature
A signature records the input and output schema of a model. Serving code uses it to check requests. The example below logs a model with a signature inferred from sample data:
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)),
)
print(run.info.run_id)The printed run ID is the handle you need to register this model in the next step of the tutorial.
Exercise
Exercise
Why does the registry need a database backend instead of the default file store?
Show answer
The registry stores named models, numbered versions, and tags, which require a database to store and query. The default file store does not support the registry.