API mô hình

Giới thiệu về MLflow

Weston Bassler

Senior MLOps Engineer

MLflow REST API

api

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

API mô hình

  • Lưu

  • Ghi log

  • Tải

Scikit-Learn

1 wikipedia.org
Giới thiệu về MLflow

Hàm API mô hình

# Lưu mô hình vào hệ thống tệp cục bộ
mlflow.sklearn.save_model(model, path)
# Ghi log mô hình như artifact vào MLflow Tracking.
mlflow.sklearn.log_model(model, artifact_path)
# Tải mô hình từ hệ thống tệp cục bộ hoặc MLflow Tracking.
mlflow.sklearn.load_model(model_uri)
Giới thiệu về MLflow

Tải mô hình

  • Hệ thống tệp cục bộ - relative/path/to/local/model hoặc /Users/me/path/to/local/model

  • MLflow Tracking - runs:/<mlflow_run_id>/run-relative/path/to/model

  • Hỗ trợ S3 - s3://my_bucket/path/to/model

Giới thiệu về MLflow

Lưu mô hình

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

# Lưu mô hình cục bộ mlflow.sklearn.save_model(lr, "local_path")
ls local_path/
MLmodel            model.pkl        requirements.txt        python_env.yaml
Giới thiệu về MLflow

Tải mô hình cục bộ

# Tải mô hình từ đường dẫn cục bộ
model = mlflow.sklearn.load_model("local_path")

# Hiển thị mô hình model
LogisticRegression()
Giới thiệu về MLflow

Ghi log mô hình

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

# Ghi log mô hình mlflow.sklearn.log_model(lr, "tracking_path")
Giới thiệu về MLflow

Giao diện Tracking

Giao diện Tracking

Giới thiệu về MLflow

Run hoạt động gần nhất

# Định dạng cho runs
runs:/<mlflow_run_id>/run-relative/path/to/model
# Lấy run hoạt động gần nhất
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'>>
Giới thiệu về MLflow

ID run hoạt động gần nhất

# Lấy run hoạt động gần nhất
run = mlflow.last_active_run()
# Hiển thị run_id của run gần nhất
run.info.run_id
'8c2061731caf447e805a2ac65630e70c'
Giới thiệu về MLflow

Thiết lập run_id

# Lấy run hoạt động gần nhất
run = mlflow.last_active_run()

# Gán biến run_id run_id = run.info.run_id
run_id
'8c2061731caf447e805a2ac65630e70c'
Giới thiệu về MLflow

Tải mô hình từ MLflow Tracking

# Truyền run_id bằng f-string
model = mlflow.sklearn.load_model(f"runs:/{run_id}/tracking_path")

# Hiển thị mô hình model
LogisticRegression()
Giới thiệu về MLflow

Ayo berlatih!

Giới thiệu về MLflow

Preparing Video For Download...