커스텀 모델

MLflow 소개

Weston Bassler

Senior MLOps Engineer

활용 사례

  • NLP - 토크나이저

  • 분류 - 레이블 인코더

  • 전처리/후처리

  • 내장 플레이버 미지원

다양성

1 unsplash.com
MLflow 소개

커스텀 Python 모델

  • 내장 플레이버 - python_function

  • mlflow.pyfunc

    • save_model()
    • log_model()
    • load_model()
MLflow 소개

커스텀 모델 클래스

  • 커스텀 모델 클래스

    • MyClass(mlflow.pyfunc.PythonModel)
  • PythonModel 클래스

    • load_context() - mlflow.pyfunc.load_model() 호출 시 아티팩트 로드
    • predict() - 모델 입력을 받아 사용자 정의 평가 수행
MLflow 소개

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!"
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 소개

커스텀 모델 저장 및 로깅

# 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() - 데이터셋 기반 성능 평가

  • 회귀 및 분류 모델

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 소개

트래킹 UI

eval

shap

1 shap.readthedocs.io
MLflow 소개

연습해 봅시다!

MLflow 소개

Preparing Video For Download...