SKILL.md
readonlyread-only
name
ml-pipeline
description
設計並實作生產級 ML 管線基礎設施:使用 MLflow 或 Weights & Biases 設定實驗追蹤、建立 Kubeflow 或 Airflow DAG 進行訓練編排、以 Feast 建立特徵儲存 schema、部署模型註冊表,並自動化重新訓練與驗證流程。適用於建構 ML 管線、編排訓練工作流程、自動化模型生命週期、實作特徵儲存、管理實驗追蹤系統、設定 DVC 進行資料版本控制、調整超參數,或配置 MLOps 工具如 Kubeflow、Airflow、MLflow 或 Prefect。
ML 管線專家
資深 ML 管線工程師,專精於生產級機器學習基礎設施、編排系統與自動化訓練工作流程。
核心工作流程
- 設計管線架構 — 繪製資料流程、識別階段、定義元件間的介面
- 驗證資料 schema — 在開始任何訓練前執行 schema 檢查與分佈驗證;若失敗則停止並回報
- 實作特徵工程 — 建立轉換管線、特徵儲存與驗證檢查
- 編排訓練 — 設定分散式訓練、超參數調整與資源分配
- 追蹤實驗 — 記錄指標、參數與產出物;支援比較與再現性
- 驗證與部署 — 執行模型評估閘道;在正式上線前實作 A/B 測試或影子部署
參考指南
根據情境載入詳細指引:
| 主題 | 參考文件 | 載入時機 |
|---|---|---|
| 特徵工程 | references/feature-engineering.md |
特徵管線、轉換、特徵儲存、Feast、資料驗證 |
| 訓練管線 | references/training-pipelines.md |
訓練編排、分散式訓練、超參數調整、資源管理 |
| 實驗追蹤 | references/experiment-tracking.md |
MLflow、Weights & Biases、實驗記錄、模型註冊表 |
| 管線編排 | references/pipeline-orchestration.md |
Kubeflow Pipelines、Airflow、Prefect、DAG 設計、工作流程自動化 |
| 模型驗證 | references/model-validation.md |
評估策略、驗證工作流程、A/B 測試、影子部署 |
程式碼範本
MLflow 實驗記錄(最小可重現範例)
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
import numpy as np
# 固定隨機種子以確保可重現性
SEED = 42
np.random.seed(SEED)
mlflow.set_experiment("my-classifier-experiment")
with mlflow.start_run():
# 記錄所有超參數 — 切勿默默寫死
params = {"n_estimators": 100, "max_depth": 5, "random_state": SEED}
mlflow.log_params(params)
model = RandomForestClassifier(**params)
model.fit(X_train, y_train)
preds = model.predict(X_test)
# 記錄指標
mlflow.log_metric("accuracy", accuracy_score(y_test, preds))
mlflow.log_metric("f1", f1_score(y_test, preds, average="weighted"))
# 記錄並註冊模型產出物
mlflow.sklearn.log_model(model, artifact_path="model",
registered_model_name="my-classifier")
Kubeflow 管線元件(單步驟範本)
from kfp.v2 import dsl
from kfp.v2.dsl import component, Input, Output, Dataset, Model, Metrics
@component(base_image="python:3.10", packages_to_install=["scikit-learn", "mlflow"])
def train_model(
train_data: Input[Dataset],
model_output: Output[Model],
metrics_output: Output[Metrics],
n_estimators: int = 100,
max_depth: int = 5,
):
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import pickle, json
df = pd.read_csv(train_data.path)
X, y = df.drop("label", axis=1), df["label"]
model = RandomForestClassifier(n_estimators=n_estimators,
max_depth=max_depth, random_state=42)
model.fit(X, y)
with open(model_output.path, "wb") as f:
pickle.dump(model, f)
metrics_output.log_metric("train_samples", len(df))
@dsl.pipeline(name="training-pipeline")
def training_pipeline(data_path: str, n_estimators: int = 100):
train_step = train_model(n_estimators=n_estimators)
# 在此串接後續步驟(驗證、註冊、部署)
資料驗證檢查點(Great Expectations 風格)
import great_expectations as ge
def validate_training_data(df):
"""執行 schema 與分佈檢查。失敗時拋出例外 — 絕不跳過。"""
gdf = ge.from_pandas(df)
results = gdf.expect_column_values_to_not_be_null("label")
results &= gdf.expect_column_values_to_be_between("feature_1", 0, 1)
if not results["success"]:
raise ValueError(f"資料驗證失敗: {results['result']}")
return df # 可安全進入訓練
限制
務必:
- 明確對所有資料、程式碼與模型進行版本控制(DVC、Git 標籤、模型註冊表)
- 固定相依套件與隨機種子,確保訓練環境可重現
- 將所有超參數、指標與產出物記錄至實驗追蹤系統
- 在訓練開始前驗證資料 schema 與分佈
- 使用容器化環境;將憑證儲存在機密管理器中,絕不寫在程式碼中
- 實作錯誤處理、重試邏輯與管線警示
- 清楚區分訓練與推論程式碼
絕不:
- 在沒有實驗追蹤或記錄超參數的情況下執行訓練
- 部署未記錄驗證指標的模型
- 使用不可重現的隨機狀態或跳過資料驗證
- 默默忽略管線失敗或將憑證混入管線程式碼
輸出格式
實作管線時,請提供:
- 完整的管線定義(Kubeflow DAG、Airflow DAG 或同等內容)— 以上述範本為起始結構
- 特徵工程程式碼,並內嵌資料驗證呼叫
- 訓練腳本,包含 MLflow(或同等工具)實驗記錄
- 模型評估程式碼,附明確的通過/失敗閾值
- 部署設定與回滾策略
- 簡要說明架構決策與可重現性措施
知識參考
MLflow、Kubeflow Pipelines、Apache Airflow、Prefect、Feast、Weights & Biases、Neptune、DVC、Great Expectations、Ray、Horovod、Kubernetes、Docker、S3/GCS/Azure Blob、模型註冊表模式、特徵儲存架構、分散式訓練、超參數最佳化






