Flavours

Contents

Flavours#

Flavours in mlflow terminology are special modules that integrate mlflow with the most popular ML frameworks. This page describes features of various flavours and pyfunc the flavour that is supposed to be used in the general cases.

import numpy as np
from sklearn.linear_model import LinearRegression

import mlflow
import mlflow.models
import mlflow.sklearn

mlflow.set_tracking_uri("http://localhost:5000/")

Pyfunc#

Load other#

An interesting fact is that pyfunc is able to load models logged by other flavours. They would be loaded as a special kind of pyfunc.


The following cell fits a sklearn.LinearRegression model and logs it - as it should with sklearn flavour.

model_to_save = LinearRegression().fit(
    X=np.random.normal(0, 2, size=(100, 2)),
    y=np.random.normal(0, 20, size=100)
)
signature = mlflow.models.infer_signature(
    np.random.normal(0, 2, size=(100, 2)),
    np.random.normal(0, 20, size=100)
)

with mlflow.start_run(run_name="test_run"):
    model_info = mlflow.sklearn.log_model(
        model_to_save,
        signature=signature
    )
🏃 View run test_run at: http://localhost:5000/#/experiments/0/runs/48bdb3a7b3a6497cbb42b29d31935cf1
🧪 View experiment at: http://localhost:5000/#/experiments/0

The following cell loads the saved model using sklearn flavour.

sklearn_loaded = mlflow.sklearn.load_model(model_info.model_uri)
type(sklearn_loaded)
sklearn.linear_model._base.LinearRegression

The next code loads the model using mlflow.pyfunc.load_model.

pyfunk_loaded = mlflow.pyfunc.load_model(model_info.model_uri)
type(pyfunk_loaded)
mlflow.pyfunc.PyFuncModel

Note that the types of the gotten objects are different.

The following cell shows that all objects: original, loaded with pyfunc and loaded with sklearn; return the same predictions.

X = np.array([[1, 8]], dtype=np.float64)
(
    sklearn_loaded.predict(X),
    pyfunk_loaded.predict(X),
    model_to_save.predict(X)
)
(array([-0.08650699]), array([-0.08650699]), array([-0.08650699]))