Mô hình tùy chỉnh

Giới thiệu về MLflow

Weston Bassler

Senior MLOps Engineer

Trường hợp sử dụng ví dụ

  • NLP - Tokenizer

  • Phân loại - Label encoder

  • Tiền/Xử lý hậu kỳ

  • Không phải flavor tích hợp sẵn

Khác nhau

1 unsplash.com
Giới thiệu về MLflow

Mô hình Python tùy chỉnh

  • Flavor tích hợp - python_function

  • mlflow.pyfunc

    • save_model()
    • log_model()
    • load_model()
Giới thiệu về MLflow

Lớp mô hình tùy chỉnh

  • Lớp mô hình tùy chỉnh

    • MyClass(mlflow.pyfunc.PythonModel)
  • Lớp PythonModel

    • load_context() - nạp artifacts khi gọi mlflow.pyfunc.load_model()
    • predict() - nhận đầu vào và thực hiện suy luận do người dùng định nghĩa
Giới thiệu về MLflow

Lớp 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!"
Giới thiệu về MLflow

Ví dụ lớp tùy chỉnh

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)
Giới thiệu về MLflow

Lưu và ghi log mô hình tùy chỉnh

# 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())
Giới thiệu về MLflow

Tải mô hình tùy chỉnh

# Load model from local filesystem
mlflow.pyfunc.load_model("local")
# Load model from MLflow Tracking
mlflow.pyfunc.load_model("runs:/run_id/tracking_path")
Giới thiệu về MLflow

Đánh giá mô hình

  • mlflow.evaluate() - Đánh giá hiệu năng trên một tập dữ liệu

  • Mô hình hồi quy và phân loại

Giới thiệu về MLflow

Ví dụ đánh giá

# 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" )
Giới thiệu về MLflow

Giao diện Tracking

đánh giá

shap

1 shap.readthedocs.io
Giới thiệu về MLflow

Ayo berlatih!

Giới thiệu về MLflow

Preparing Video For Download...