设计可复现实验

面向生产环境的机器学习模型开发

Sinan Ozdemir

Data Scientist and Author

可复现实验

  • 确保准确性与可靠性。
  • 更易复现实验结果。
  • 促进协作。
  • 降低模型偏差风险。
  • 提升研究过程的完整性。
面向生产环境的机器学习模型开发

MLflow

一个用于跟踪与管理机器学习实验的开源平台。MLflow 可用于:

  • 创建可复现的 ML 流水线
  • 追踪与管理:
    • 包依赖
    • 代码版本
    • 实验设置
  • 允许多人访问实验

mlflow

1 https://www.databricks.com/blog/2018/06/05/introducing-mlflow-an-open-source-machine-learning-platform.html
面向生产环境的机器学习模型开发

MLflow 使用示例

# standard scikit-learn imports
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# new imports from MLflow
import mlflow
import mlflow.sklearn
面向生产环境的机器学习模型开发

MLflow 使用示例(续)

with mlflow.start_run():  # Start an MLflow run assuming we have data prepared

    # Build and train model
    rf = RandomForestClassifier()
    rf.fit(X_train, y_train)

    # Log parameters and model information
    mlflow.log_param("n_estimators", rf.n_estimators)
    mlflow.sklearn.log_model(rf, "model")

    y_pred = rf.predict(X_test)  # Evaluate model
    accuracy = accuracy_score(y_test, y_pred)
    mlflow.log_metric("accuracy", accuracy)  # log the test accuracy metric
面向生产环境的机器学习模型开发

代码追踪

  • 使用 MLflow 记录代码版本与变更
  • 比较不同代码版本
  • 标识生成给定结果所用的代码版本
  • 轻松复现实验
  • 便于调试与排障
面向生产环境的机器学习模型开发

模型注册表

  • 集中存储模型及其元数据
  • 可用 MLflow 记录、存储并比较模型版本
  • 复现完整 ML 流水线
  • 用于模型对比
  • 确保模型准确性与可靠性

存储

面向生产环境的机器学习模型开发

实验可复现性

  • 追踪并记录输入数据、代码与设置
  • 复现完整 ML 流水线
  • 建立对模型结果的信任
  • 便于他人验证与复用
  • 确保不同运行间结果一致

面向生产环境的机器学习模型开发

回顾文档编制

  • 文档对可复现 ML 至关重要,应包含:
    • 模型输入数据。
    • 构建模型所用代码。
    • 实验使用的任何设置。
    • 实验结果(选用的模型等)
  • 文档应易于访问
  • 作为实验的完整记录
面向生产环境的机器学习模型开发

¡Vamos a practicar!

面向生产环境的机器学习模型开发

Preparing Video For Download...