API Model

Pengantar MLflow

Weston Bassler

Senior MLOps Engineer

MLflow REST API

api

1 istock.com
Pengantar MLflow

API Model

  • Simpan

  • Log

  • Muat

Scikit-Learn

1 wikipedia.org
Pengantar MLflow

Fungsi API Model

# Simpan model ke sistem berkas lokal
mlflow.sklearn.save_model(model, path)
# Log model sebagai artefak ke MLflow Tracking.
mlflow.sklearn.log_model(model, artifact_path)
# Muat model dari sistem berkas lokal atau MLflow Tracking.
mlflow.sklearn.load_model(model_uri)
Pengantar MLflow

Muat model

  • Sistem berkas lokal - relative/path/to/local/model atau /Users/me/path/to/local/model

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

  • Dukungan S3 - s3://my_bucket/path/to/model

Pengantar MLflow

Simpan model

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

# Simpan model lokal mlflow.sklearn.save_model(lr, "local_path")
ls local_path/
MLmodel            model.pkl        requirements.txt        python_env.yaml
Pengantar MLflow

Muat model lokal

# Muat model dari path lokal
model = mlflow.sklearn.load_model("local_path")

# Tampilkan model model
LogisticRegression()
Pengantar MLflow

Log model

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

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

UI Pelacakan

UI Pelacakan

Pengantar MLflow

Run aktif terakhir

# Format untuk runs
runs:/<mlflow_run_id>/run-relative/path/to/model
# Ambil run aktif terakhir
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'>>
Pengantar MLflow

ID run aktif terakhir

# Ambil run aktif terakhir
run = mlflow.last_active_run()
# Tampilkan run_id dari run terakhir
run.info.run_id
'8c2061731caf447e805a2ac65630e70c'
Pengantar MLflow

Mengatur run id

# Ambil run aktif terakhir
run = mlflow.last_active_run()

# Set variabel run_id run_id = run.info.run_id
run_id
'8c2061731caf447e805a2ac65630e70c'
Pengantar MLflow

Muat model dari MLflow Tracking

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

# Tampilkan model model
LogisticRegression()
Pengantar MLflow

Ayo berlatih!

Pengantar MLflow

Preparing Video For Download...