Introduction to MLflow Models

Introduction to MLflow

Weston Bassler

Senior MLOps Engineer

MLflow Models

  • Simplify ML library integration

  • Simplify Deployment

  • Convention called "Flavors"

flavors

1 unsplash.com
Introduction to MLflow

Built-In Flavors

IMAGES OF FLAVORS

  • Write custom tools from ML libraries

  • Flavors simplify the new for custom code

# Import flavor from mlflow module
import mlflow.FLAVOR
1 mlflow.org
Introduction to MLflow

Autolog

# Automatically log model and metrics
mlflow.FLAVOR.autolog()
# Scikit-learn built-in flavor
mlflow.sklearn.autolog()
Introduction to MLflow

Scikit-learn Flavor

# Import scikit-learn
import mlflow
from sklearn.linear_model import \
    LinearRegression

# Using auto-logging mlflow.sklearn.autolog()
# Train the model
lr = LinearRegression()
lr.fit(X, y)

Model will be logged automatically on model.fit()

Introduction to MLflow

Common Metrics

  • Regression
    • mean squared error
    • root mean squared error
    • mean absolute error
    • r2 score
  • Classification
    • precision score
    • recall score
    • f1 score
    • accuracy score

Common Parameters

MODEL.get_params()
Introduction to MLflow

Common parameters

# Train the model
lr = LinearRegression()
lr.fit(X, y)

# Get params params = lr.get_params(deep=True)
params
{'copy_X': True, 'fit_intercept': True, 'n_jobs': None, 
    'normalize': 'deprecated', 'positive': False}
Introduction to MLflow

Autolog parameters

parameters

# Model
lr = LinearRegression()
lr.fit(X, y)
Introduction to MLflow

Autolog metrics

metrics

# Model
lr = LinearRegression()
lr.fit(X, y)
Introduction to MLflow

Storage format

Directory structure for a model:

model/
    MLmodel
    model.pkl
    python_env.yaml
    requirements.txt
# Autolog
mlflow.sklearn.autolog()

Artifacts

Introduction to MLflow

Contents of MLmodel

artifact_path: model
flavors:
  python_function:
    env:
      virtualenv: python_env.yaml
    loader_module: mlflow.sklearn
    model_path: model.pkl
    predict_fn: predict
    python_version: 3.10.8
  sklearn:
    code: null
    pickled_model: model.pkl
    serialization_format: cloudpickle
    sklearn_version: 1.1.3
Introduction to MLflow

MLmodel

MLmodel

Introduction to MLflow

Let's practice!

Introduction to MLflow

Preparing Video For Download...