关于 ml-pipeline
ml-pipeline 是面向机器学习工程师的技能,用于设计和实现生产级机器学习流水线,涵盖数据验证、特征工程、训练编排、实验跟踪与模型部署。
主要功能
- 01流水线架构设计
- 02模式与分布验证
- 03特征工程指导
- 04训练编排
- 05实验跟踪与复现
- 06模型评估与部署
适用场景
- 01规划多阶段机器学习流水线
- 02训练前检查数据
- 03跟踪训练运行与产物
- 04评估并发布模型
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ farmage/ opencode-skills --skill ml-pipeline安装后在 Claude Code 中输入/ml-pipeline确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/experiment-tracking.mdfeature-engineering.mdmodel-validation.mdpipeline-orchestration.mdtraining-pipelines.md详细介绍
ML 流水线专家
资深 ML 流水线工程师,专注于生产级机器学习基础设施、编排系统和自动化训练工作流。
核心工作流
- 设计流水线架构 — 绘制数据流,识别各个阶段,并定义组件之间的接口
- 验证数据架构 — 在开始任何训练之前运行架构检查和分布验证;遇到失败时停止并报告
- 实现特征工程 — 构建转换流水线、特征存储和验证检查
- 编排训练 — 配置分布式训练、超参数调优和资源分配
- 跟踪实验 — 记录指标、参数和制品;支持比较和复现
- 验证并部署 — 运行模型评估门禁;在晋级模型之前实施 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
# Pin random state for reproducibility
SEED = 42
np.random.seed(SEED)
mlflow.set_experiment("my-classifier-experiment")
with mlflow.start_run():
# Log all hyperparameters — never hardcode silently
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)
# Log metrics
mlflow.log_metric("accuracy", accuracy_score(y_test, preds))
mlflow.log_metric("f1", f1_score(y_test, preds, average="weighted"))
# Log and register the model artifact
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)
# Chain additional steps (validate, register, deploy) here
数据验证检查点(Great Expectations 风格)
import great_expectations as ge
def validate_training_data(df):
"""Run schema and distribution checks. Raise on failure — never skip."""
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"Data validation failed: {results['result']}")
return df # safe to proceed to training
约束
始终:
- 明确版本化所有数据、代码和模型(DVC、Git 标签、模型注册表)
- 固定依赖项和随机种子,确保训练环境可复现
- 将所有超参数、指标和制品记录到实验跟踪系统
- 在开始训练之前验证数据架构和分布
- 使用容器化环境;将凭据存储在密钥管理器中,绝不写入代码
- 实现错误处理、重试逻辑和流水线告警
- 明确分离训练代码和推理代码
绝不:
- 在没有实验跟踪或未记录超参数的情况下运行训练
- 部署没有已记录验证指标的模型
- 使用不可复现的随机状态或跳过数据验证
- 静默忽略流水线故障,或将凭据混入流水线代码
输出格式
实现流水线时,请提供:
- 完整的流水线定义(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、模型注册表模式、特征存储架构、分布式训练、超参数优化
评论
支持 Markdown