writing-skills
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
register-ml-modelRegister trained models in MLflow Model Registry with version control, implement stage transitions (Staging, Production, Archived) with approval workflows, and manage model lineage with comprehensive metadata and deployment tracking. Use when promoting a trained model from experimentation to production, managing multiple model versions across development st…
Independent GitHub snapshot · Automated classification · Original documentation governs
完整配置文件與模板見 Extended Examples。
實施 MLflow Model Registry,作系統化之模型版本化、階段管理與部署治理。
以資料庫後端設置 MLflow Model Registry(生產環境不建議文件式登記簿)。
# Start MLflow server with Model Registry support
mlflow server \
--backend-store-uri postgresql://user:pass@localhost:5432/mlflow \
--default-artifact-root s3://mlflow-artifacts/models \
--host 0.0.0.0 \
--port 5000
Python 配置:
# model_registry_config.py
import mlflow
from mlflow.tracking import MlflowClient
# Set tracking URI (must support Model Registry)
MLFLOW_TRACKING_URI = "http://mlflow-server.company.com:5000"
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
# ... (see EXAMPLES.md for complete implementation)
預期: Model Registry UI 標籤現於 MLflow 中,search_registered_models() 成功返回(即便為空),資料庫含 registered_models 表。
失敗時: 確認 MLflow 版本 ≥1.2(Model Registry 自 1.2 引入),檢查資料庫後端(SQLite 對 Model Registry 支援不全),確保 --backend-store-uri 指向資料庫(非 file://),確認資料庫用戶具 CREATE TABLE 權限,查 MLflow 伺服器日誌之遷移錯誤。
將已記錄之模型以全面元資料註冊至 Model Registry。
# register_model.py
import mlflow
from mlflow.tracking import MlflowClient
from model_registry_config import MLFLOW_TRACKING_URI
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
client = MlflowClient()
# ... (see EXAMPLES.md for complete implementation)
預期: 新模型版本現於 Model Registry UI,版本含描述與標籤,模型製品可透過 models:/<model-name>/<version> URI 取用,模型簽章與輸入例皆保留。
失敗時: 確認 run_id 存在且已完成(client.get_run(run_id)),檢查模型製品路徑與所記製品相符(mlflow.search_runs() 以察),確保模型以正確之框架風味記錄(mlflow.sklearn.log_model 而非 mlflow.log_artifact),確認模型名無特殊字元(用連字符勿用底線),檢查製品儲存可達。
帶驗證檢查地將模型版本經各階段(None → Staging → Production → Archived)。
# stage_management.py
import mlflow
from mlflow.tracking import MlflowClient
from datetime import datetime
client = MlflowClient()
class ModelStageManager:
# ... (see EXAMPLES.md for complete implementation)
預期: 模型版本之階段於登記簿更新,舊版本自動歸檔,轉換時戳記於標籤,回滾恢復前生產版本。
失敗時: 檢查版本存在且於預期階段,確認 archive_existing_versions 旗標行為(若僅一版恐不歸檔),確保資料庫支援階段更新之並發交易,檢查階段轉換鎖(每版本同時只一轉換),確認核准工作流整合。
用模型別名作穩定之部署引用(MLflow ≥2.0)。
# model_aliases.py
from mlflow.tracking import MlflowClient
client = MlflowClient()
def set_model_alias(model_name, version, alias):
"""
Set an alias for a model version (MLflow 2.0+).
# ... (see EXAMPLES.md for complete implementation)
預期: 別名現於 Model Registry UI,以別名載入模型可行(models:/name@alias),更新別名立即影響新載入,A/B 測試基礎建置可運作。
失敗時: 升級 MLflow 至 ≥2.0 以支援原生別名,舊版本用標籤式回退,確認別名命名(僅字母數字與連字符),檢查別名衝突(每模型版本一別名)。
以全面元資料自資料追蹤至部署之完整譜系。
# model_lineage.py
import mlflow
from mlflow.tracking import MlflowClient
import json
client = MlflowClient()
def enrich_model_metadata(model_name, version, lineage_data):
# ... (see EXAMPLES.md for complete implementation)
預期: 模型版本標籤含全面譜系資訊,get_model_lineage() 返完整歷史,JSON 報告含資料源、訓練細節與部署資訊。
失敗時: 確認標籤值為字串(將 dict 轉為 JSON),檢查標籤鍵名(無空格或特殊字元),確保訓練時已捕獲譜系資料,確認 run_id 有效可達。
將模型註冊整合入 CI/CD 管線以自動擢升。
# .github/workflows/model_promotion.yml
name: Model Promotion Pipeline
on:
workflow_dispatch:
inputs:
model_name:
description: 'Model name to promote'
# ... (see EXAMPLES.md for complete implementation)
Python 自動化腳本:
# scripts/promote_model.py
import argparse
from stage_management import ModelStageManager
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--model-name", required=True)
parser.add_argument("--version", type=int, required=True)
# ... (see EXAMPLES.md for complete implementation)
預期: GitHub Actions 工作流於手動派發時觸發,驗證測試通過,模型擢升至目標階段,Slack 通知已發,部署管線自動觸發。
失敗時: 檢查 MLFLOW_TRACKING_URI 之 GitHub secrets 配置,確認 GitHub Actions 至 MLflow 伺服器之網路存取(恐需 VPN 或 IP 允許清單),確保驗證腳本之指標閾值正確,檢查 Slack webhook 配置,確認 Python 腳本之執行權限。
archive_existing_versions=True 自動歸檔track-ml-experiments — 註冊前先將模型記錄至 MLflowdeploy-ml-model-serving — 將已註冊之模型部署至服務基礎建置run-ab-test-models — 用登記簿別名 A/B 測試模型orchestrate-ml-pipeline — 自動化模型訓練與註冊version-ml-data — 為模型譜系版本化訓練資料