使用 Application Insights 監視您的 Azure Machine Learning 模型Monitor your Azure Machine Learning models with Application Insights

在本文中,您將瞭解如何設定 Azure Machine Learning 的 Azure 應用程式深入解析。In this article, you learn how to set up Azure Application Insights for Azure Machine Learning. Application Insights 可讓您監視:Application Insights gives you the opportunity to monitor:

  • 要求速率、回應時間和失敗率。Request rates, response times, and failure rates.
  • 相依性速率、回應時間和失敗率。Dependency rates, response times, and failure rates.
  • 例外狀況。Exceptions.

深入了解 Application InsightsLearn more about Application Insights.

必要條件Prerequisites

  • 如果您沒有 Azure 訂用帳戶,請在開始前先建立一個免費帳戶。If you don’t have an Azure subscription, create a free account before you begin. 立即試用免費或付費版本的 Azure Machine LearningTry the free or paid version of Azure Machine Learning today.

  • 已安裝 Azure Machine Learning 工作區、包含指令碼的本機目錄,以及適用於 Python 的 Azure Machine Learning SDK。An Azure Machine Learning workspace, a local directory that contains your scripts, and the Azure Machine Learning SDK for Python installed. 若要了解如何取得這些必要條件,請參閱如何設定開發環境To learn how to get these prerequisites, see How to configure a development environment.

  • 要部署至 Azure Kubernetes Service (AKS) 或 Azure Container 執行個體 (ACI) 的已訓練機器學習模型。A trained machine learning model to be deployed to Azure Kubernetes Service (AKS) or Azure Container Instance (ACI). 如果您沒有模型,請參閱將影像分類模型定型教學課程。If you don't have one, see the Train image classification model tutorial.

使用 SDK 設定Use SDK to configure

更新已部署的服務Update a deployed service

  1. 識別您工作區中的服務。Identify the service in your workspace. ws 的值是工作區的名稱。The value for ws is the name of your workspace.

    from azureml.core.webservice import Webservice
    aks_service= Webservice(ws, "my-service-name")
    
  2. 更新您的服務,並啟用 Application Insights。Update your service and enable Application Insights.

    aks_service.update(enable_app_insights=True)
    

記錄您服務中的自訂追蹤Log custom traces in your service

如果您想要記錄自訂追蹤,請依循部署方式及位置文件中的 AKS 或 ACI 標準部署程序。If you want to log custom traces, follow the standard deployment process for AKS or ACI in the How to deploy and where document. 然後使用下列步驟:Then use the following steps:

  1. 新增 print 陳述式以更新評分檔案。Update the scoring file by adding print statements.

    print ("model initialized" + time.strftime("%H:%M:%S"))
    
  2. 更新服務組態。Update the service configuration.

    config = Webservice.deploy_configuration(enable_app_insights=True)
    
  3. 建置映像並將其部署在 AKSACI 上。Build an image and deploy it on AKS or ACI.

在 Python 中停用追蹤Disable tracking in Python

若要停用 Application Insights,請使用下列程式碼:To disable Application Insights, use the following code:

## replace <service_name> with the name of the web service
<service_name>.update(enable_app_insights=False)

使用入口網站設定Use portal to configure

您可以在 Azure 入口網站中啟用及停用 Application Insights。You can enable and disable Application Insights in the Azure portal.

  1. Azure 入口網站中,開啟您的工作區。In the Azure portal, open your workspace.

  2. 在 [部署] 索引標籤上,選取您要在其中啟用 Application Insights 的服務。On the Deployments tab, select the service where you want to enable Application Insights.

    [部署] 索引標籤上的服務清單List of services on the Deployments tab

  3. 選取 [編輯]。Select Edit.

    [編輯] 按鈕Edit button

  4. 在 [進階設定] 中,選取 [啟用 AppInsights 診斷] 核取選項。In Advanced Settings, select the Enable AppInsights diagnostics check box.

    已選取啟用診斷的核取方塊Selected check box for enabling diagnostics

  5. 選取螢幕底部的 [更新] 以套用變更。Select Update at the bottom of the screen to apply the changes.

停用Disable

  1. Azure 入口網站中,開啟您的工作區。In the Azure portal, open your workspace.

  2. 選取 [部署],選取服務,然後選取 [編輯]。Select Deployments, select the service, and select Edit.

    使用 [編輯] 按鈕Use the edit button

  3. 在 [進階設定] 中,清除 [啟用 AppInsights 診斷] 核取選項。In Advanced Settings, clear the Enable AppInsights diagnostics check box.

    已清除啟用診斷的核取方塊Cleared check box for enabling diagnostics

  4. 選取螢幕底部的 [更新] 以套用變更。Select Update at the bottom of the screen to apply the changes.

評估資料Evaluate data

您的服務資料會儲存在您的 Application Insights 帳戶中,與 Azure Machine Learning 位於相同的資源群組內。Your service's data is stored in your Application Insights account, within the same resource group as Azure Machine Learning. 若要檢視:To view it:

  1. 移至 Azure 入口網站中的 Machine Learning 服務工作區,然後按一下 Application Insights 連結。Go to your Machine Learning service workspace in the Azure portal and click on Application Insights link.

    AppInsightsLocAppInsightsLoc

  2. 選取 [概觀] 索引標籤以查看一組您服務的基本計量。Select the Overview tab to see a basic set of metrics for your service.

    概觀Overview

  3. 若要查看您的自訂追蹤,請選取 [Analytics]。To look into your custom traces, select Analytics.

  4. 在 [結構描述] 區段中,選取 [追蹤]。In the schema section, select Traces. 然後選取 [執行] 來執行查詢。Then select Run to run your query. 資料應該會以資料表格式出現,且應該會對應至您評分檔案中的自訂呼叫。Data should appear in a table format and should map to your custom calls in your scoring file.

    自訂追蹤Custom traces

若要深入了解如何使用 Application Insights,請參閱什麼是 Application Insights?To learn more about how to use Application Insights, see What is Application Insights?.

範例筆記本Example notebook

Ipynb 筆記本中的「啟用-應用程式深入解析」會示範本文中的概念。The enable-app-insights-in-production-service.ipynb notebook demonstrates concepts in this article.

了解如何依照使用 Jupyter Notebook 來探索這項服務一文來執行 Notebook。Learn how to run notebooks by following the article, Use Jupyter notebooks to explore this service.

後續步驟Next steps

您也可以在生產環境中收集您模型上的資料。You can also collect data on your models in production. 閱讀在生產環境中收集模型資料一文。Read the article Collect data for models in production.

另請參閱容器的 Azure 監視器Also read Azure Monitor for containers.