记录、加载和部署 MLflow 模型 Log, load, and deploy MLflow Models

MLflow 模型是一种用于将机器学习模型打包的标准格式,可在多种不同下游工具(例如 Apache Spark 上的批量推理或通过 REST API 提供实时服务)中使用。An MLflow Model is a standard format for packaging machine learning models that can be used in a variety of downstream tools—for example, batch inference on Apache Spark or real-time serving through a REST API. 该格式定义了一种约定,利用这种约定,能够以不同的模型服务和推理平台可以理解的不同风格(python-function、pytorch、sklearn,等等)保存模型。The format defines a convention that lets you save a model in different flavors (python-function, pytorch, sklearn, and so on), that can be understood by different model serving and inference platforms.

  • 若要将模型记录到 MLflow 跟踪服务器,请使用 mlflow.<model-type>.log_model(model, ...)To log a model to the MLflow tracking server, use mlflow.<model-type>.log_model(model, ...).
  • 若要加载模型,请使用 mlflow.<model-type>.load_model(modelpath)To load a model, use mlflow.<model-type>.load_model(modelpath).
  • 若要部署模型,请使用 mlflow.<model-type>.deploy()To deploy a model, use mlflow.<model-type>.deploy().

可以在 MLflow 模型注册表中注册模型,该注册表是一个集中的模型存储,它提供了 UI 和一组 API 来管理 MLflow 模型的完整生命周期。You can register models in the MLflow Model Registry, a centralized model store that provides a UI and set of APIs to manage the full lifecycle of MLflow Models.

有关记录模型的示例,请参阅跟踪机器学习训练运行示例;有关加载和部署模型的示例,请参阅本文中的笔记本。See Track machine learning training runs examples for examples of logging models, and see the notebooks in this article for examples of loading and deploying models.

模型也可以保存在本地。You can also save models locally.

  • 若要在本地保存模型,请使用 mlflow.<model-type>.save_model(model, modelpath)To save a model locally, use mlflow.<model-type>.save_model(model, modelpath). modelpath 必须是 DBFS 路径。modelpath must be a DBFS path. 例如,如果使用 DBFS 位置 dbfs:/my_project_models 来存储项目工作,则必须使用模型路径 /dbfs/my_project_modelsFor example, if you use a DBFS location dbfs:/my_project_models to store your project work, you must use the model path /dbfs/my_project_models:

    modelpath = "/dbfs/my_project_models/model-%f-%f" % (alpha, l1_ratio)
    mlflow.sklearn.save_model(lr, modelpath)
    

示例Examples