使用 MLflow 註冊及提供模型

已完成

模型註冊可讓 MLflow 和 Azure Databricks 追蹤模型;基於兩個原因很重要:

  • 註冊模型讓您能夠提供模型以進行即時、串流或批次推斷。 註冊讓使用已定型模型的程序變得簡單,因為資料科學家現在不需要開發應用程式程式碼;服務程序會建置該包裝函式,並公開 REST API 或方法來進行自動批次評分。
  • 註冊模型可讓您在一段時間內建立該模型的新版本;讓您有機會追蹤模型變更,甚至執行不同歷史模型版本之間的比較。

註冊模型

當您執行實驗來定型模型時,您可以將模型本身記錄為實驗執行的一部分,如下所示:

with mlflow.start_run():
    # code to train model goes here

    # log the model itself (and the environment it needs to be used)
    unique_model_name = "my_model-" + str(time.time())
    mlflow.spark.log_model(spark_model = model,
                           artifact_path=unique_model_name,
                           conda_env=mlflow.spark.get_default_conda_env())

當您檢閱實驗執行時 (包括指出模型預測程度的記錄計量),模型會包含在執行成品中。 然後,您可以選取選項,在實驗檢視器中使用使用者介面來註冊模型。

或者,如果您想要註冊模型而不檢閱執行中的計量,您可以在 log_model 方法中包含 registered_model_name 參數;在此情況下,會在實驗執行期間自動註冊模型。

with mlflow.start_run():
    # code to train model goes here

    # log the model itself (and the environment it needs to be used)
    unique_model_name = "my_model-" + str(time.time())
    mlflow.spark.log_model(spark_model=model,
                           artifact_path=unique_model_name
                           conda_env=mlflow.spark.get_default_conda_env(),
                           registered_model_name="my_model")

您可以註冊多個版本的模型,讓您在將所有用戶端應用程式移至最佳效能版本之前,先比較模型版本的效能。

使用模型進行推斷

使用模型從新特徵資料預測標籤的程序稱為「推斷」。 您可以在 Azure Databricks 中使用 MLflow,以下列方式讓模型可供推斷:

  • 使用用戶端應用程式可提出 REST 要求的 HTTP 端點,將模型裝載為即時服務。
  • 使用模型,根據特徵的差異資料表執行標籤的永久串流推斷,並將結果寫入至輸出資料表。
  • 使用模型根據差異資料表進行批次推斷,並將每個批次作業的結果寫入至特定資料夾。

您可以在 Azure Databricks 入口網站的 [模型] 區段中,部署模型以供推斷,如下所示:

Screenshot of the Set up model inference dialog box in the Azure Databricks portal.