Aangepaste modellen

Introductie tot MLflow

Weston Bassler

Senior MLOps Engineer

Voorbeelden van use-cases

  • NLP - Tokenizer(s)

  • Classificatie - Labelencoder

  • Pre-/postprocessing

  • Geen ingebouwde flavor

Anders

1 unsplash.com
Introductie tot MLflow

Aangepaste Python-modellen

  • Ingebouwde flavor - python_function

  • mlflow.pyfunc

    • save_model()
    • log_model()
    • load_model()
Introductie tot MLflow

Aangepaste modelclass

  • Aangepaste modelclass

    • MyClass(mlflow.pyfunc.PythonModel)
  • PythonModel-klasse

    • load_context() - laadt artifacts wanneer mlflow.pyfunc.load_model() wordt aangeroepen
    • predict() - neemt modelinput en voert door de gebruiker gedefinieerde evaluatie uit
Introductie tot MLflow

Python-class

# Class
class MyPythonClass:

# Function that prints Hello! def my_func(): print("Hello!")
# Create a new Object
x = MyPythonClass()

# Excute my_func function x.my_func()
"Hello!"
Introductie tot MLflow

Voorbeeld van een aangepaste 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)
Introductie tot MLflow

Aangepast model opslaan en loggen

# 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())
Introductie tot MLflow

Aangepaste modellen laden

# Load model from local filesystem
mlflow.pyfunc.load_model("local")
# Load model from MLflow Tracking
mlflow.pyfunc.load_model("runs:/run_id/tracking_path")
Introductie tot MLflow

Modelevaluatie

  • mlflow.evaluate() - Prestatie op basis van een dataset

  • Regressie- en classificatiemodellen

Introductie tot MLflow

Evaluatievoorbeeld

# Training Data
X_train, X_test, y_train, y_test = \
    train_test_split(X, y,
    train_size=0.7,random_state=0)

# Linear Regression model lr = LinearRegression() lr.fit(X_train, y_train)
# Dataset
eval_data = X_test
eval_data["test_label"] = y_test

# Evaluate model with Dataset mlflow.evaluate( "runs:/run_id/model", eval_data, targets="test_label", model_type="regressor" )
Introductie tot MLflow

Tracking-UI

eval

shap

1 shap.readthedocs.io
Introductie tot MLflow

Laten we oefenen!

Introductie tot MLflow

Preparing Video For Download...