Bird
Raised Fist0
MLOpsdevops~7 mins

MLflow Model Registry in MLOps - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
When you train machine learning models, you need a way to save, organize, and track different versions. MLflow Model Registry helps you manage these models so you can easily find, update, and deploy them without confusion.
When you want to keep track of multiple versions of a machine learning model.
When you need to share models with your team in a clear and organized way.
When you want to move a model from testing to production safely.
When you want to add descriptions or tags to models for better understanding.
When you want to automate deployment by linking model stages like 'Staging' and 'Production'.
Commands
This command starts a local server to serve version 1 of the registered model named 'my-model'. It lets you test the model by sending data to port 1234.
Terminal
mlflow models serve -m models:/my-model/1 --no-conda -p 1234
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.pyfunc.scoring_server: Starting server with command: ['mlflow', 'models', 'serve', '-m', 'models:/my-model/1', '--no-conda', '-p', '1234'] 2024/06/01 12:00:01 INFO mlflow.pyfunc.scoring_server: Listening on port 1234
-m - Specifies the model URI to serve
--no-conda - Avoids creating a new conda environment for serving
-p - Sets the port number for the server
This command lists all registered models in the MLflow Model Registry so you can see what models are available.
Terminal
mlflow models list
Expected OutputExpected
Name my-model another-model
This command moves version 1 of 'my-model' to the 'Production' stage, marking it as ready for real use.
Terminal
mlflow models transition-stage --model-name my-model --version 1 --stage Production
Expected OutputExpected
Model version '1' of 'my-model' transitioned to stage 'Production'.
--model-name - Specifies the name of the model
--version - Specifies the version number of the model
--stage - Specifies the new stage for the model version
This command deletes version 1 of 'my-model' from the registry if it is no longer needed.
Terminal
mlflow models delete --model-name my-model --version 1
Expected OutputExpected
Model version '1' of 'my-model' deleted.
--model-name - Specifies the name of the model
--version - Specifies the version number to delete
Key Concept

If you remember nothing else from MLflow Model Registry, remember: it helps you organize and control your machine learning models by version and stage for safe and easy deployment.

Code Example
MLOps
import mlflow
from mlflow.tracking import MlflowClient

# Initialize client
client = MlflowClient()

# Register a new model version
model_uri = "runs:/1234567890abcdef/model"
model_name = "my-model"

# Create a new model version
version = client.create_model_version(name=model_name, source=model_uri, run_id=None)
print(f"Registered model version: {version.version}")

# Transition model version to staging
client.transition_model_version_stage(name=model_name, version=version.version, stage="Staging")
print(f"Model version {version.version} moved to Staging")

# Add a description
client.update_model_version(name=model_name, version=version.version, description="Initial staging version")
print("Description added to model version")
OutputSuccess
Common Mistakes
Trying to serve a model version that does not exist in the registry.
The server will fail because the model version is not found.
Use 'mlflow models list' to check available models and versions before serving.
Transitioning a model version to 'Production' without testing it first.
This can cause unstable or untested models to be used in real applications.
Test the model in 'Staging' before moving it to 'Production'.
Deleting a model version that is currently in use or needed for rollback.
You lose the ability to revert to that model version if problems occur.
Only delete model versions after confirming they are no longer needed.
Summary
Use MLflow Model Registry to save and organize different versions of machine learning models.
Use commands to list models, serve specific versions, and move models between stages like Staging and Production.
Always test models before moving them to Production and avoid deleting versions that might be needed later.

Practice

(1/5)
1. What is the primary purpose of the MLflow Model Registry?
easy
A. To train machine learning models automatically
B. To visualize data for machine learning
C. To organize, track, and manage machine learning models and their versions
D. To store raw datasets for training

Solution

  1. Step 1: Understand the role of MLflow Model Registry

    The Model Registry is designed to keep track of models, their versions, and lifecycle stages.
  2. Step 2: Compare with other options

    Training models, visualizing data, and storing raw data are not functions of the Model Registry.
  3. Final Answer:

    To organize, track, and manage machine learning models and their versions -> Option C
  4. Quick Check:

    Model Registry purpose = Organize and track models [OK]
Hint: Model Registry = model tracking and version control [OK]
Common Mistakes:
  • Confusing Model Registry with training or data storage
  • Thinking it handles data visualization
  • Assuming it automatically trains models
2. Which of the following is the correct command to register a model named my_model with MLflow Model Registry using the Python API?
easy
A. mlflow.create_model(name='my_model')
B. mlflow.register_model(model_uri='runs:/1234/my_model', name='my_model')
C. mlflow.add_model_version('my_model')
D. mlflow.model.register('my_model')

Solution

  1. Step 1: Recall the MLflow Python API for registering models

    The correct function is mlflow.register_model() with parameters model_uri and name.
  2. Step 2: Check the options for syntax correctness

    Only mlflow.register_model(model_uri='runs:/1234/my_model', name='my_model') uses the correct function and parameters. Others are invalid or do not exist.
  3. Final Answer:

    mlflow.register_model(model_uri='runs:/1234/my_model', name='my_model') -> Option B
  4. Quick Check:

    Register model = mlflow.register_model() [OK]
Hint: Use mlflow.register_model() with model_uri and name [OK]
Common Mistakes:
  • Using non-existent functions like create_model or add_model_version
  • Incorrect function names or missing parameters
  • Confusing model registration with model creation
3. Given the following Python code snippet using MLflow Model Registry, what will be the output?
from mlflow import MlflowClient
client = MlflowClient()
model_versions = client.get_latest_versions(name='my_model', stages=['Production'])
print(len(model_versions))
medium
A. Zero, because the method returns an empty list always
B. The total number of all model versions regardless of stage
C. An error because get_latest_versions requires no parameters
D. The number of model versions currently in the Production stage

Solution

  1. Step 1: Understand the method get_latest_versions

    This method returns the latest versions of a model filtered by the specified stages.
  2. Step 2: Analyze the code behavior

    The code filters for versions in the 'Production' stage and prints how many such versions exist.
  3. Final Answer:

    The number of model versions currently in the Production stage -> Option D
  4. Quick Check:

    get_latest_versions with stages filters versions [OK]
Hint: get_latest_versions(name, stages) filters by stage [OK]
Common Mistakes:
  • Assuming it returns all versions without filtering
  • Thinking the method takes no parameters
  • Believing it always returns zero
4. You try to transition a model version to the 'Staging' stage using this code:
client.transition_model_version_stage(name='my_model', version='2', stage='Staging')
But you get a TypeError. What is the likely cause?
medium
A. The version parameter should be an integer, not a string
B. The stage name 'Staging' is invalid
C. The method name is incorrect; it should be change_stage
D. The client object is not initialized

Solution

  1. Step 1: Check the method signature for transition_model_version_stage

    The version parameter must be an integer representing the model version number.
  2. Step 2: Identify the error cause

    Passing version='2' as a string causes a TypeError; it should be version=2 as an integer.
  3. Final Answer:

    The version parameter should be an integer, not a string -> Option A
  4. Quick Check:

    Version parameter type = int [OK]
Hint: Pass version as int, not string, to avoid TypeError [OK]
Common Mistakes:
  • Passing version as string instead of integer
  • Using wrong method name
  • Assuming stage names are invalid
  • Not initializing the client object
5. You want to automate deployment by moving the latest model version from 'Staging' to 'Production' only if its accuracy metric is above 0.9. Which MLflow Model Registry workflow correctly implements this logic?
hard
A. Retrieve latest 'Staging' version, check accuracy metric, then transition to 'Production' if > 0.9
B. Transition all versions to 'Production' without checking metrics
C. Delete all 'Staging' versions and register a new model in 'Production'
D. Manually download the model and deploy without using Model Registry stages

Solution

  1. Step 1: Identify the correct workflow for conditional deployment

    You must get the latest model version in 'Staging' and check its accuracy metric before promoting.
  2. Step 2: Understand why other options are incorrect

    Transitioning all versions blindly ignores quality checks; deleting and manual deployment bypasses registry benefits.
  3. Final Answer:

    Retrieve latest 'Staging' version, check accuracy metric, then transition to 'Production' if > 0.9 -> Option A
  4. Quick Check:

    Conditional stage transition based on metric = correct workflow [OK]
Hint: Check metric before stage transition to automate deployment [OK]
Common Mistakes:
  • Skipping metric check before promotion
  • Promoting all versions blindly
  • Deleting versions unnecessarily
  • Ignoring Model Registry stages