โมเดลแบบกำหนดเอง

MLflow เบื้องต้น

Weston Bassler

Senior MLOps Engineer

ตัวอย่าง use case

  • NLP - Tokenizer(s)

  • Classification - Label encoder

  • การประมวลผลก่อน/หลัง

  • ไม่ใช่ flavor ในตัว

ความแตกต่าง

1 unsplash.com
MLflow เบื้องต้น

โมเดล Python แบบกำหนดเอง

  • Built in Flavor - python_function

  • mlflow.pyfunc

    • save_model()
    • log_model()
    • load_model()
MLflow เบื้องต้น

คลาสโมเดลแบบกำหนดเอง

  • คลาสโมเดลแบบกำหนดเอง

    • MyClass(mlflow.pyfunc.PythonModel)
  • คลาส PythonModel

    • load_context() - โหลด artifacts เมื่อเรียก mlflow.pyfunc.load_model()
    • predict() - รับ input ของโมเดลและประมวลผลตามที่กำหนด
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!"
MLflow เบื้องต้น

ตัวอย่างคลาสแบบกำหนดเอง

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)
MLflow เบื้องต้น

การบันทึกและ log โมเดลแบบกำหนดเอง

# 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())
MLflow เบื้องต้น

การโหลดโมเดลแบบกำหนดเอง

# Load model from local filesystem
mlflow.pyfunc.load_model("local")
# Load model from MLflow Tracking
mlflow.pyfunc.load_model("runs:/run_id/tracking_path")
MLflow เบื้องต้น

การประเมินโมเดล

  • mlflow.evaluate() - วัดประสิทธิภาพบนชุดข้อมูล

  • โมเดล Regression และ Classification

MLflow เบื้องต้น

ตัวอย่างการประเมินโมเดล

# 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" )
MLflow เบื้องต้น

Tracking UI

eval

shap

1 shap.readthedocs.io
MLflow เบื้องต้น

มาฝึกกันเถอะ!

MLflow เบื้องต้น

Preparing Video For Download...