串流分析中的 Machine Learning 工作室 (傳統) 整合

重要

Machine Learning 工作室 (傳統) 的支援將於 2024 年 8 月 31 日結束。 建議您在該日期之前轉換成 Azure Machine Learning

自 2021 年 12 月 1 日起,您將無法建立新的 Machine Learning 工作室 (傳統) 資源 (工作區與 Web 服務方案)。 在 2024 年 8 月 31 日之前,您可以繼續使用現有的 Machine Learning 工作室 (傳統) 實驗與 Web 服務。

ML 工作室 (傳統) 文件即將淘汰,未來將不再更新。

串流分析支援對外呼叫 Machine Learning 工作室 (傳統) 端點的使用者定義函式。 串流分析 REST API 程式庫中會詳細說明此功能的 REST API 支援。 本文提供要在串流分析中成功實作這項功能所需的補充資訊。 您也可以在 這裡取得已發佈的教學課程。

概觀:Machine Learning 工作室 (傳統) 術語

Microsoft Machine Learning 工作室 (傳統) 提供共同作業的拖放工具,讓您可用來依據資料建置、測試及部署預測性分析解決方案。 這個工具稱為 Machine Learning 工作室 (傳統)。 您可以利用這個工作室 (傳統) 來與機器學習資源互動,並輕鬆地建置、測試和逐一查看設計。 這些資源和其定義如下。

  • 工作區工作區這個容器中會保有其他所有機器學習資源,以便集中管理和控制。
  • 實驗:資料科學家會建立 實驗 來利用資料集和訓練機器學習服務模型。
  • 端點:「端點」是工作室 (傳統) 物件,可供用來將功能作為輸入、套用指定的機器學習模型,並傳回經過評分的輸出。
  • 評分 Web 服務評分 Web 服務 是上述端點的集合。

每個端點都有適用於批次執行和同步執行的 API。 串流分析使用同步執行。 該特定服務在 Machine Learning 工作室 (傳統) 中名為要求/回應服務

串流分析作業所需的工作室 (傳統) 資源

為了處理串流分析作業,必須要有要求/回應端點、 apikey和 swagger 定義才能順利執行。 串流分析有其他端點可建構 swagger 端點的 URL、查閱介面,以及將預設 UDF 定義傳回給使用者。

透過 REST API 設定串流分析和工作室 (傳統) UDF

透過使用 REST API,您可以設定作業來呼叫工作室 (傳統) 函式。 步驟如下:

  1. 建立串流分析作業
  2. 定義輸入
  3. 定義輸出
  4. 建立使用者定義函式 (UDF)
  5. 撰寫呼叫 UDF 的串流分析轉換
  6. 啟動工作

使用基本屬性建立 UDF

下列範例程式碼會建立名為 newudf 且繫結至 Machine Learning 工作室 (傳統) 端點的純量 UDF 來作為示範。 請注意,您可以在 API 說明頁面中找到所選服務的端點 (服務 URI),以及在 [服務] 主頁面中找到 apiKey

    PUT : /subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.StreamAnalytics/streamingjobs/<streamingjobName>/functions/<udfName>?api-version=<apiVersion>

要求本文範例:

    {
        "name": "newudf",
        "properties": {
            "type": "Scalar",
            "properties": {
                "binding": {
                    "type": "Microsoft.MachineLearning/WebService",
                    "properties": {
                        "endpoint": "https://ussouthcentral.services.azureml.net/workspaces/f80d5d7a77fb4b46bf2a30c63c078dca/services/b7be5e40fd194258796fb402c1958eaf/execute ",
                        "apiKey": "replacekeyhere"
                    }
                }
            }
        }
    }

呼叫預設 UDF 的 RetrieveDefaultDefinition 端點

一旦建立好基本架構 UDF,就需要 UDF 的完整定義。 RetrieveDefaultDefinition 端點可協助您取得繫結至 Machine Learning 工作室 (傳統) 端點之純量函式的預設定義。 下列內容會要求您取得繫結至工作室 (傳統) 端點之純量函式的預設 UDF 定義。 因為已在 PUT 要求期間提供,因此它不會指定實際的端點。 串流分析會呼叫要求中提供的端點 (如果已明確提供)。 否則,它會使用原本參考的端點。 UDF 在這邊會採用單一字串參數 (一個句子),並傳回指出該句子的「情緒」標籤的單一類型字串輸出。

POST : /subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.StreamAnalytics/streamingjobs/<streamingjobName>/functions/<udfName>/RetrieveDefaultDefinition?api-version=<apiVersion>

要求本文範例:

    {
        "bindingType": "Microsoft.MachineLearning/WebService",
        "bindingRetrievalProperties": {
            "executeEndpoint": null,
            "udfType": "Scalar"
        }
    }

此項目的範例輸出會看起來像下面這樣。

    {
        "name": "newudf",
        "properties": {
            "type": "Scalar",
            "properties": {
                "inputs": [{
                    "dataType": "nvarchar(max)",
                    "isConfigurationParameter": null
                }],
                "output": {
                    "dataType": "nvarchar(max)"
                },
                "binding": {
                    "type": "Microsoft.MachineLearning/WebService",
                    "properties": {
                        "endpoint": "https://ussouthcentral.services.azureml.net/workspaces/f80d5d7a77ga4a4bbf2a30c63c078dca/services/b7be5e40fd194258896fb602c1858eaf/execute",
                        "apiKey": null,
                        "inputs": {
                            "name": "input1",
                            "columnNames": [{
                                "name": "tweet",
                                "dataType": "string",
                                "mapTo": 0
                            }]
                        },
                        "outputs": [{
                            "name": "Sentiment",
                            "dataType": "string"
                        }],
                        "batchSize": 10
                    }
                }
            }
        }
    }

使用回應修補 UDF

現在必須使用先前的回應修補 UDF,如下所示。

PATCH : /subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.StreamAnalytics/streamingjobs/<streamingjobName>/functions/<udfName>?api-version=<apiVersion>

要求本文 (RetrieveDefaultDefinition 的輸出):

    {
        "name": "newudf",
        "properties": {
            "type": "Scalar",
            "properties": {
                "inputs": [{
                    "dataType": "nvarchar(max)",
                    "isConfigurationParameter": null
                }],
                "output": {
                    "dataType": "nvarchar(max)"
                },
                "binding": {
                    "type": "Microsoft.MachineLearning/WebService",
                    "properties": {
                        "endpoint": "https://ussouthcentral.services.azureml.net/workspaces/f80d5d7a77ga4a4bbf2a30c63c078dca/services/b7be5e40fd194258896fb602c1858eaf/execute",
                        "apiKey": null,
                        "inputs": {
                            "name": "input1",
                            "columnNames": [{
                                "name": "tweet",
                                "dataType": "string",
                                "mapTo": 0
                            }]
                        },
                        "outputs": [{
                            "name": "Sentiment",
                            "dataType": "string"
                        }],
                        "batchSize": 10
                    }
                }
            }
        }
    }

實作串流分析轉換來呼叫 UDF

現在要查詢每一個輸入事件的 UDF (這裡稱為 scoreTweet),並將該事件的回應寫入至輸出。

    {
        "name": "transformation",
        "properties": {
            "streamingUnits": null,
            "query": "select *,scoreTweet(Tweet) TweetSentiment into blobOutput from blobInput"
        }
    }

取得協助

如需進一步的協助,請嘗試 Azure 串流分析的 Microsoft 問與答頁面

下一步