0% found this document useful (0 votes)
4 views

MLflow Présentation

Uploaded by

Brunet Nathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MLflow Présentation

Uploaded by

Brunet Nathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Introduction to

MLflow Models
INTRODUCTION TO MLFLOW

Weston Bassler
Senior MLOps Engineer
MLflow Models
Simplify ML library integration
Simplify Deployment

Convention called "Flavors"

1 unsplash.com

INTRODUCTION TO MLFLOW
Built-In 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 # Train the model
import mlflow lr = LinearRegression()
from sklearn.linear_model import \ lr.fit(X, y)
LinearRegression
Model will be logged automatically on
# Using auto-logging model.fit()
mlflow.sklearn.autolog()

INTRODUCTION TO MLFLOW
Common Metrics Common Parameters
Regression
MODEL.get_params()
mean squared error

root mean squared error

mean absolute error

r2 score

Classification
precision score

recall score

f1 score

accuracy score

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
# Model
lr = LinearRegression()
lr.fit(X, y)

INTRODUCTION TO MLFLOW
Autolog metrics
# Model
lr = LinearRegression()
lr.fit(X, y)

INTRODUCTION TO MLFLOW
Storage format
Directory structure for a model:
# Autolog
mlflow.sklearn.autolog()
model/
MLmodel
model.pkl
python_env.yaml
requirements.txt

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

INTRODUCTION TO MLFLOW
Let's practice!
INTRODUCTION TO MLFLOW
Model API
INTRODUCTION TO MLFLOW

Weston Bassler
Senior MLOps Engineer
MLflow REST API

1 istock.com

INTRODUCTION TO MLFLOW
The Model API
Save
Log

Load

1 wikipedia.org

INTRODUCTION TO MLFLOW
Model API functions
# Save a model to the local filesystem
mlflow.sklearn.save_model(model, path)

# Log a model as an artifact to MLflow Tracking.


mlflow.sklearn.log_model(model, artifact_path)

# Load a model from local filesystem or from MLflow Tracking.


mlflow.sklearn.load_model(model_uri)

INTRODUCTION TO MLFLOW
Load model
Local Filesystem - relative/path/to/local/model or /Users/me/path/to/local/model
MLflow Tracking - runs:/<mlflow_run_id>/run-relative/path/to/model

S3 Support - s3://my_bucket/path/to/model

INTRODUCTION TO MLFLOW
Save model
# Model
lr = LogisticRegression()
lr.fit(X, y)

# Save model locall


mlflow.sklearn.save_model(lr, "local_path")

ls local_path/

MLmodel model.pkl requirements.txt python_env.yaml

INTRODUCTION TO MLFLOW
Load local model
# Load model from local path
model = mlflow.sklearn.load_model("local_path")

# Show model
model

LogisticRegression()

INTRODUCTION TO MLFLOW
Log model
# Model
lr = LogisticRegression(n_jobs=n_jobs)
lr.fit(X, y)

# Log model
mlflow.sklearn.log_model(lr, "tracking_path")

INTRODUCTION TO MLFLOW
Tracking UI

INTRODUCTION TO MLFLOW
Last active run
# Format for runs
runs:/<mlflow_run_id>/run-relative/path/to/model

# Get last active run


run = mlflow.last_active_run()
run

<Run: data=<RunData: metrics={}, params={},


tags={'mlflow.runName': 'run_name'}>,
info=<RunInfo: artifact_uri='uri', end_time='end_time',
experiment_id='0', lifecycle_stage='active', run_id='run_id',
run_name='name', run_uuid='run_uuid', start_time=start_time,
status='FINISHED', user_id='user_id'>>

INTRODUCTION TO MLFLOW
Last active run id
# Get last active run
run = mlflow.last_active_run()

# Show run_id of last run


run.info.run_id

'8c2061731caf447e805a2ac65630e70c'

INTRODUCTION TO MLFLOW
Setting the run id
# Get last active run
run = mlflow.last_active_run()
# Set run_id variable
run_id = run.info.run_id
run_id

'8c2061731caf447e805a2ac65630e70c'

INTRODUCTION TO MLFLOW
Load model from MLflow Tracking
# Pass run_id as f-string literal
model = mlflow.sklearn.load_model(f"runs:/{run_id}/tracking_path")
# Show model
model

LogisticRegression()

INTRODUCTION TO MLFLOW
Let's Practice
INTRODUCTION TO MLFLOW
Custom models
INTRODUCTION TO MLFLOW

Weston Bassler
Senior MLOps Engineer
Example use cases
NLP - Tokenizer(s)
Classification - Label encoder

Pre/Post processing

Not a built-in flavor

1 unsplash.com

INTRODUCTION TO MLFLOW
Custom Python models
Built in Flavor - python_function
mlflow.pyfunc
save_model()

log_model()

load_model()

INTRODUCTION TO MLFLOW
Custom model class
Custom model class
MyClass(mlflow.pyfunc.PythonModel)

PythonModel class
load_context() - loads artifacts when mlflow.pyfunc.load_model() is called

predict() - takes model input and performs user defined evaluation

INTRODUCTION TO MLFLOW
Python class
# Class # Create a new Object
class MyPythonClass: x = MyPythonClass()
# Function that prints Hello! # Excute my_func function
def my_func(): x.my_func
print("Hello!")

"Hello!"

INTRODUCTION TO MLFLOW
Example custom Class
import mlflow.pyfunc

# Define the model class


class CustomPredict(mlflow.pyfunc.PythonModel):
# Load artifacts
def load_context(self, context):
self.model = mlflow.sklearn.load_model(context.artifacts["custom_model"])
# Evaluate input using custom_function()
def predict(self, context, model_input):
prediction = self.model.predict(model_input)
return custom_function(prediction)

INTRODUCTION TO MLFLOW
Saving and logging a custom model
# Save model to local filesystem
mlflow.pyfunc.save_model(path="custom_model", python_model=CustomPredict())

# Log model to MLflow Tracking


mlflow.pyfunc.log_model(artifact_path="custom_model", python_model=CustomPredict())

INTRODUCTION TO MLFLOW
Loading custom models
# Load model from local filesystem
mlflow.pyfunc.load_model("local")

# Load model from MLflow Tracking


mlflow.pyfunc.load_model("runs:/run_id/tracking_path")

INTRODUCTION TO MLFLOW
Model Evaluation
mlflow.evaluate() - Performance based on a dataset

Regression and Classification models

INTRODUCTION TO MLFLOW
Evaluation Example
# Training Data # Dataset
X_train, X_test, y_train, y_test = \ eval_data = X_test
train_test_split(X, y, eval_data["test_label"] = y_test
train_size=0.7,random_state=0)
# Evaluate model with Dataset
# Linear Regression model mlflow.evaluate(
lr = LinearRegression() "runs:/run_id/model",
lr.fit(X_train, y_train) eval_data,
targets="test_label",
model_type="regressor"
)

INTRODUCTION TO MLFLOW
Tracking UI

1 shap.readthedocs.io

INTRODUCTION TO MLFLOW
Let's practice!
INTRODUCTION TO MLFLOW
Model serving
INTRODUCTION TO MLFLOW

Weston Bassler
Senior MLOps Engineer
MLflow Models
Standardize model packaging
Log models

Model Evaluation

INTRODUCTION TO MLFLOW
Model Deployment

1 datacamp.com

INTRODUCTION TO MLFLOW
REST API
/ping - for health checks

/health - for health checks

/version - for getting the version of MLflow

/invocations - for model scoring

Port 5000

INTRODUCTION TO MLFLOW
Invocations endpoint
/invocations
{
"1": {
No,Name,Subject
"No": "1",
1,Bill Johnson,English
"Name": "Bill Johnson",
2,Gary Valentine,Mathematics
"Subject": "English"
},
Content-Type : application/json or
"2": {
application/csv
"No": "2",
"Name": "Gary Valentine",
"Subject": "Mathematics"
}
}

INTRODUCTION TO MLFLOW
CSV format JSON format
Pandas Dataframe dataframe_split - pandas DataFrame in
split orientation
pandas_df.to_csv()
dataframe_records - pandas DataFrame
in records orientation

INTRODUCTION TO MLFLOW
DataFrame split
# Dataframe split orientation
{
"dataframe_split": {
"columns": ["sex", "age", "weight"],
"data": [["male", 23, 160], ["female", 33, 120]]
}
}

INTRODUCTION TO MLFLOW
Serving Models
# MLflow serve command
mlflow models serve --help
Usage: mlflow models serve [OPTIONS]

INTRODUCTION TO MLFLOW
Serve uri
# Local Filesystem
mlflow models serve -m relative/path/to/local/model

# Run ID
mlflow models serve -m runs:/<mlflow_run_id>/artifacts/model

# AWS S3
mlflow models serve -m s3://my_bucket/path/to/model

INTRODUCTION TO MLFLOW
Serve example
# Serve model from run
mlflow models serve -m runs:/e84a122920de4bdeaedb54146deeb429/artifacts/model

2023/03/12 16:28:28 INFO mlflow.models.flavor_backend_registry:


Selected backend for flavor 'python_function'
2023/03/12 16:28:28 INFO mlflow.pyfunc.backend: === Running command
'exec gunicorn --timeout=60 -b 127.0.0.1:5000 -w 1 ${GUNICORN_CMD_ARGS} --
mlflow.pyfunc.scoring_server.wsgi:app'
[2023-03-12 16:28:29 -0400] [48431] [INFO] Starting gunicorn 20.1.0
[2023-03-12 16:28:29 -0400] [48431] [INFO] Listening at: http://127.0.0.1:5000
(48431)
[2023-03-12 16:28:29 -0400] [48431] [INFO] Using worker: sync
[2023-03-12 16:28:29 -0400] [48432] [INFO] Booting worker with pid: 48432

INTRODUCTION TO MLFLOW
Invocations Request
# Send dataframe_split orientation payload to MLflow
curl http://127.0.0.1:5000/invocations -H 'Content-Type: application/json' -d '{
"dataframe_split": {
"columns": ["sex", "age", "weight"],
"data": [["male", 23, 160], ["female", 33, 120]]
}
}'

{"predictions": [1, 0]}

INTRODUCTION TO MLFLOW
Let's practice!
INTRODUCTION TO MLFLOW

You might also like