Logging and loading a model
The Model API provides a way to interact with our models by logging and loading them directly from MLflow Tracking in a standardized manner. Being able to interact with models is crucial during the ML lifecycle for the Model Engineering and Model Evaluation steps.
In this exercise you will create a Linear Regression model from scikit-learn using the Unicorn
dataset. This model will be logged to MLflow Tracking and then loaded using the run_id used to log the artifact.
First, you will log the model using the scikit-learn library from the MLflow module. Then you will load the model from MLflow Tracking using the run_id
.
The model will be trained and have the name lr_model
.
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
The mlflow
module will be imported.
This exercise is part of the course
Introduction to MLflow
Exercise instructions
- Log the model to MLflow Tracking under the artifact path of
"lr_tracking"
. - Create a variable called
run
that is set to the last run. - Create another variable called
run_id
that is set to therun_id
of therun
variable. - Load the model using the
run_id
and the artifact path used to log the model.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Log model to MLflow Tracking
____.____.____(____, "____")
# Get the last run
run = ____.____
# Get the run_id of the above run
run_id = ____.___.____
# Load model from MLflow Tracking
model = ____.____.____(f"runs:/{____}/____")