MLflow Series 4: MLflow model

English link: https://mlflow.org/docs/latest/models.html
This link: https://www.cnblogs.com/CheeseZH/p/11946260.html

MLflow model is a basic format packed machine learning models, the application can easily to different downstream tools, such as real-time or batch RESTful services reasoning Apache Spark. This format defines a set of specifications that allow your model can be used by different downstream tools.

Storage format

Each MLflow model is a directory containing various files, root directory contains a MLmode file for multiple flavors define the model (this is a key concept, do not know how to translate it using the original word).

Flavors is a powerful model to make MLflow key factor is the standardized set of deployment tools can understand, which makes it possible to develop a set of tools compatible with a variety of machine learning library. MLflow defines some basic flavors, so the built-in tools support the deployment, such as "Python function" flavor describes how to run a Python function by means of a model. Machine learning library may also be defined or other flavors, e.g. MLflow the mlflow.sklearnlibrary allows to model scikit-learn Pipeline object to load or function as Python be used by.

All of the flavors that a particular model supports are defined in its MLmodel file in YAML format. For example, mlflow.sklearn outputs models as follows:

A model supports all flavors are defined in its MLmodel file, this file is YAML format. E.g. mlflow.sklearn output model directory structure:

# Directory written by mlflow.sklearn.save_model(model, "my_model")
my_model/
├── MLmodel
└── model.pkl

It MLmodel document describes two flavors:

time_created: 2018-05-25T17:28:53.35

flavors:
  sklearn:
    sklearn_version: 0.19.1
    pickled_model: model.pkl
  python_function:
    loader_module: mlflow.sklearn

This model can be used by any tool that supports sklearn or python_function model of flavor. E.g. mlflow models servecommands able to serve with sklearnthe model of the flavor:

mlflow models serve my_model

mlflow sagemakerCommand-line tool can speak with a python_functionmodel flavor of packaged and deployed to the AWS SageMaker:

mlflow sagemaker deploy -m my_model [other options]

Fields in the MLmodel Format

In addition to flavors, MLmodel YAML file can contain the following information:

  • time_created
  • run_id, if the model is by MLflow Tracking saved, there will be a run id

Model API

You can save and load MLflow model in several ways. First, MLflow integrates many common libraries, e.g. mlflow.sklearn contains save_model, log_model, and load_model scikit-learn method for the model. Second, you can use the mlflow.models.Modelclass to create and save the model. This class has four key ways:

  • add_flavor: add a flavor for the model. Each flavor has a name and a string of key-value dictionary of attributes, value may be any that can be YAML serialized object.
  • save: Save the model to a local directory.
  • log: Use MLflow Tracking save the model for the current run of the product.
  • load: loaded model is loaded from a local directory or a run of the model from the previous product.

Built-in Model Flavors

Customization model

If MLflow does not support machine learning library you want to use, then you need to be customized models, including the Custom Python Models , and Custom Flavors .

Built-in deployment tools

MLflow provide deployment tools can be deployed to the local machine MLflow model or some of the production environment. Not all deployment methods are applicable to all models flavors.

MLflow deployment model

MLflow model deployment costs can be derived directly or REST API score file. MLflow model can be packaged to support the REST API Docker image. This image can be deployed in a variety of environments similar Kubernetes.

You can line to local deployment model from the command module, or generate mlflow.models Docker image.

REST API service receives a POST request to / invocations path following data formats:

  • .. JSON-serialized pandas DataFrames split orientation format e.g., data = pandas_df.to_json (orient = 'split') to specify the format Content-Type = application / json or application / json request header; format = pandas-split.
  • JSON-serialized pandas DataFrames records orientation format. Not recommended for use.
  • CSV-serialized pandas DataFrames. E.g. data = pandas_df.to_csv (). This format requires designation request header Content-Type = text / csv.
    E.g:
# split-oriented
curl http://127.0.0.1:5000/invocations -H 'Content-Type: application/json' -d '{
    "columns": ["a", "b", "c"],
    "data": [[1, 2, 3], [4, 5, 6]]
}'

# record-oriented (fine for vector rows, loses ordering for JSON records)
curl http://127.0.0.1:5000/invocations -H 'Content-Type: application/json; format=pandas-records' -d '[[1, 2, 3], [4, 5, 6]]'

Common Commands

  • serve: to model deployment costs REST API service
  • build_docker: The model is packaged into a mirror support REST API
  • predict: generating a model according to the prediction results CSV file or JSON

Detail View:

mlflow models --help
mlflow models serve --help
mlflow models predict --help
mlflow models build-docker --help

Python_function model to deploy Microsoft Azure ML

Python_function deployment model to Amazon SageMaker

Python_function model to deploy Apache Spark UDF

Guess you like

Origin www.cnblogs.com/CheeseZH/p/11946260.html