关于 ml-pipeline
ml-pipeline 面向构建生产级机器学习流水线的工程师,涵盖数据验证、特征工程、编排、实验跟踪和模型部署检查。
主要功能
- 01数据模式与分布验证
- 02特征工程流水线指导
- 03训练编排与调参
- 04实验和产物跟踪
- 05评估门槛与部署策略
适用场景
- 01构建可复现的训练工作流
- 02在模型训练前验证数据
- 03比较已跟踪的训练实验
- 04根据评估结果控制模型部署
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jeffallan/ claude-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、模型注册表模式、特征存储架构、分布式训练、超参数优化
由 @jeffallan 维护,他是 Synergetic Solutions 的首席顾问
评论
支持 Markdown