모델 API

MLflow 소개

Weston Bassler

Senior MLOps Engineer

MLflow REST API

api

1 istock.com
MLflow 소개

모델 API

  • 저장

  • 로깅

  • 로드

Scikit-Learn

1 wikipedia.org
MLflow 소개

모델 API 함수

# Save a model to the local filesystem
mlflow.sklearn.save_model(model, path)
# Log a model as an artifact to MLflow Tracking.
mlflow.sklearn.log_model(model, artifact_path)
# Load a model from local filesystem or from MLflow Tracking.
mlflow.sklearn.load_model(model_uri)
MLflow 소개

모델 로드

  • 로컬 파일시스템 - relative/path/to/local/model 또는 /Users/me/path/to/local/model

  • MLflow 트래킹 - runs:/<mlflow_run_id>/run-relative/path/to/model

  • S3 지원 - s3://my_bucket/path/to/model

MLflow 소개

모델 저장

# Model
lr = LogisticRegression()
lr.fit(X, y)

# Save model locall mlflow.sklearn.save_model(lr, "local_path")
ls local_path/
MLmodel            model.pkl        requirements.txt        python_env.yaml
MLflow 소개

로컬 모델 로드

# Load model from local path
model = mlflow.sklearn.load_model("local_path")

# Show model model
LogisticRegression()
MLflow 소개

모델 로깅

# Model
lr = LogisticRegression(n_jobs=n_jobs)
lr.fit(X, y)

# Log model mlflow.sklearn.log_model(lr, "tracking_path")
MLflow 소개

트래킹 UI

트래킹 UI

MLflow 소개

마지막 활성 실행

# Format for runs
runs:/<mlflow_run_id>/run-relative/path/to/model
# Get last active run
run = mlflow.last_active_run()

run
<Run: data=<RunData: metrics={}, params={}, 
tags={'mlflow.runName': 'run_name'}>, 
 info=<RunInfo: artifact_uri='uri', end_time='end_time', 
 experiment_id='0', lifecycle_stage='active', run_id='run_id', 
 run_name='name', run_uuid='run_uuid', start_time=start_time, 
 status='FINISHED', user_id='user_id'>>
MLflow 소개

마지막 활성 run id

# Get last active run
run = mlflow.last_active_run()
# Show run_id of last run
run.info.run_id
'8c2061731caf447e805a2ac65630e70c'
MLflow 소개

run id 설정

# Get last active run
run = mlflow.last_active_run()

# Set run_id variable run_id = run.info.run_id
run_id
'8c2061731caf447e805a2ac65630e70c'
MLflow 소개

MLflow 트래킹에서 모델 로드

# Pass run_id as f-string literal
model = mlflow.sklearn.load_model(f"runs:/{run_id}/tracking_path")

# Show model model
LogisticRegression()
MLflow 소개

Let's Practice

MLflow 소개

Preparing Video For Download...