Modele personalizate

Introducere în MLflow

Weston Bassler

Senior MLOps Engineer

Exemple de utilizare

  • NLP - Tokenizator(e)

  • Clasificare - Encoder de etichete

  • Pre/Post-procesare

  • Nu este un flavor integrat

Diferit

1 unsplash.com
Introducere în MLflow

Modele Python personalizate

  • Flavor integrat - python_function

  • mlflow.pyfunc

    • save_model()
    • log_model()
    • load_model()
Introducere în MLflow

Clasa de model personalizată

  • Clasă de model personalizată

    • MyClass(mlflow.pyfunc.PythonModel)
  • Clasa PythonModel

    • load_context() - încarcă artefactele la apelarea mlflow.pyfunc.load_model()
    • predict() - primește date de intrare și efectuează evaluarea definită de utilizator
Introducere în MLflow

Clasă Python

# 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!"
Introducere în MLflow

Exemplu de clasă personalizată

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)
Introducere în MLflow

Salvarea și înregistrarea unui model personalizat

# 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())
Introducere în MLflow

Încărcarea modelelor personalizate

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

Evaluarea modelelor

  • mlflow.evaluate() - Performanță pe baza unui set de date

  • Modele de regresie și clasificare

Introducere în MLflow

Exemplu de evaluare

# 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" )
Introducere în MLflow

Interfața de urmărire

eval

shap

1 shap.readthedocs.io
Introducere în MLflow

Să exersăm!

Introducere în MLflow

Preparing Video For Download...