Model Registry¶
In this notebook, we will see how to register a model in MLflow. Registering a model is a way to keep track of the different versions of a model and its metadata. It is also a way to share the model with other people. When a model is registered a new version is created. The first version of a model is always version 1.
🔍 Search a Model in a Run¶
We are going to register one of the model. We will use the search_runs
method to find the run we want to register.
In [14]:
Copied!
import mlflow
EXPERIMENT_NAME = "mlflow-demo" # โ make sure this experiment exists
# get experiment id
experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME)
experiment_id = experiment.experiment_id
# get all runs taged with model=linear-regression in this experiment
runs = mlflow.search_runs(
experiment_ids=experiment_id,
filter_string="tags.model = 'linear-regression'",
)
# get the first retrieved run
run_id = runs.iloc[0].run_id
print(f"โ
Using run_id '{run_id}'!")
import mlflow
EXPERIMENT_NAME = "mlflow-demo" # โ make sure this experiment exists
# get experiment id
experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME)
experiment_id = experiment.experiment_id
# get all runs taged with model=linear-regression in this experiment
runs = mlflow.search_runs(
experiment_ids=experiment_id,
filter_string="tags.model = 'linear-regression'",
)
# get the first retrieved run
run_id = runs.iloc[0].run_id
print(f"โ
Using run_id '{run_id}'!")
โ Using run_id 'f1d8e8acaf354ef98561e5d8707161d0'!
🚩 Register the Model¶
In [15]:
Copied!
# register the model for this run
MODEL_NAME = "demo-linear-regression" # change this to your model name
# Compute model path: models stored in a run follow this convention
model_path = f"runs:/{run_id}/model"
# register the model
result = mlflow.register_model(model_path, MODEL_NAME)
print(f"โ
Registered model version: {result.version}!")
# register the model for this run
MODEL_NAME = "demo-linear-regression" # change this to your model name
# Compute model path: models stored in a run follow this convention
model_path = f"runs:/{run_id}/model"
# register the model
result = mlflow.register_model(model_path, MODEL_NAME)
print(f"โ
Registered model version: {result.version}!")
Registered model 'demo-linear-regression' already exists. Creating a new version of this model... 2023/10/17 22:23:02 INFO mlflow.tracking._model_registry.client: Waiting up to 300 seconds for model version to finish creation. Model name: demo-linear-regression, version 4
โ Registered model version: 4!
Created version '4' of model 'demo-linear-regression'.