共用方式為


範例:使用 Azure Machine Learning 設計工具建置和部署自定義技能 (封存)

此範例已封存且不支援。 它說明如何使用 Azure Machine Learning 設計工具 是一種易於使用的互動式畫布,可針對回歸和分類等工作建立機器學習模型。 在認知搜尋擴充管線中叫用設計工具所建立的模型需要一些額外的步驟。 在此範例中,您將建立簡單的迴歸模型來預測汽車的價格,並叫用推斷端點作為 AML 技能。

請遵循範例管線 & 數據集檔頁面中的回歸 - 汽車價格預測 (進階) 教學課程,以建立模型來預測汽車的價格。

重要

在即時推斷程序之後部署模型會產生有效端點,但不是在認知搜尋中可用於 AML 技能的端點。

註冊模型和下載資產

定型模型之後,註冊定型的模型,然後遵循步驟下載 trained_model_outputs 資料夾中的所有檔案,或從模型成品頁面中只下載 score.pyconda_env.yml 檔案。 在模型部署為即時推斷端點之前,您會先編輯評分指令碼。

認知搜尋擴充管線只處理單一文件,還會產生要求,其中包含單一預測的輸入。 下載的 score.py 接受記錄清單,並以序列化 JSON 字串形式傳回預測清單。 您將對 score.py 進行兩項變更

  • 編輯指令碼以使用單一輸入記錄,而非清單
  • 編輯指令碼以傳回 JSON 物件,只具有單一屬性,也就是預測的價格。

開啟下載的 score.py 並編輯 run(data) 函式。 此函式目前設定為需要下列輸入,如模型的 _samples.json 檔案中所述。

[
  {
    "symboling": 2,
    "make": "mitsubishi",
    "fuel-type": "gas",
    "aspiration": "std",
    "num-of-doors": "two",
    "body-style": "hatchback",
    "drive-wheels": "fwd",
    "engine-location": "front",
    "wheel-base": 93.7,
    "length": 157.3,
    "width": 64.4,
    "height": 50.8,
    "curb-weight": 1944,
    "engine-type": "ohc",
    "num-of-cylinders": "four",
    "engine-size": 92,
    "fuel-system": "2bbl",
    "bore": 2.97,
    "stroke": 3.23,
    "compression-ratio": 9.4,
    "horsepower": 68.0,
    "peak-rpm": 5500.0,
    "city-mpg": 31,
    "highway-mpg": 38,
    "price": 6189.0
  },
  {
    "symboling": 0,
    "make": "toyota",
    "fuel-type": "gas",
    "aspiration": "std",
    "num-of-doors": "four",
    "body-style": "wagon",
    "drive-wheels": "fwd",
    "engine-location": "front",
    "wheel-base": 95.7,
    "length": 169.7,
    "width": 63.6,
    "height": 59.1,
    "curb-weight": 2280,
    "engine-type": "ohc",
    "num-of-cylinders": "four",
    "engine-size": 92,
    "fuel-system": "2bbl",
    "bore": 3.05,
    "stroke": 3.03,
    "compression-ratio": 9.0,
    "horsepower": 62.0,
    "peak-rpm": 4800.0,
    "city-mpg": 31,
    "highway-mpg": 37,
    "price": 6918.0
  },
  {
    "symboling": 1,
    "make": "honda",
    "fuel-type": "gas",
    "aspiration": "std",
    "num-of-doors": "two",
    "body-style": "sedan",
    "drive-wheels": "fwd",
    "engine-location": "front",
    "wheel-base": 96.5,
    "length": 169.1,
    "width": 66.0,
    "height": 51.0,
    "curb-weight": 2293,
    "engine-type": "ohc",
    "num-of-cylinders": "four",
    "engine-size": 110,
    "fuel-system": "2bbl",
    "bore": 3.15,
    "stroke": 3.58,
    "compression-ratio": 9.1,
    "horsepower": 100.0,
    "peak-rpm": 5500.0,
    "city-mpg": 25,
    "highway-mpg": 31,
    "price": 10345.0
  }
]

您的變更將確保模型在編制索引期間可接受認知搜尋所產生的輸入,這是單一記錄。

{
    "symboling": 2,
    "make": "mitsubishi",
    "fuel-type": "gas",
    "aspiration": "std",
    "num-of-doors": "two",
    "body-style": "hatchback",
    "drive-wheels": "fwd",
    "engine-location": "front",
    "wheel-base": 93.7,
    "length": 157.3,
    "width": 64.4,
    "height": 50.8,
    "curb-weight": 1944,
    "engine-type": "ohc",
    "num-of-cylinders": "four",
    "engine-size": 92,
    "fuel-system": "2bbl",
    "bore": 2.97,
    "stroke": 3.23,
    "compression-ratio": 9.4,
    "horsepower": 68.0,
    "peak-rpm": 5500.0,
    "city-mpg": 31,
    "highway-mpg": 38,
    "price": 6189.0
}

將第 27 行到第 30 行換成


    for key, val in data.items():
        input_entry[key].append(decode_nan(val))

您也需要編輯指令碼產生的輸出,從字串改成 JSON 物件。 將原始檔案中的 return 陳述式 (第 37 行) 編輯成:

    output = result.data_frame.values.tolist()
    return {
        "predicted_price": output[0][-1]
    }

以下是更新後的 run 函式,其中已變更輸入格式和預測輸出,將會接受單一記錄作為輸入,並傳回含有預測價格的 JSON 物件。

def run(data):
    data = json.loads(data)
    input_entry = defaultdict(list)
    # data is now a JSON object not a list of JSON objects
    for key, val in data.items():
        input_entry[key].append(decode_nan(val))

    data_frame_directory = create_dfd_from_dict(input_entry, schema_data)
    score_module = ScoreModelModule()
    result, = score_module.run(
        learner=model,
        test_data=DataTable.from_dfd(data_frame_directory),
        append_or_result_only=True)
    #return json.dumps({"result": result.data_frame.values.tolist()})
    output = result.data_frame.values.tolist()
    # return the last column of the the first row of the dataframe
    return  {
        "predicted_price": output[0][-1]
    }

註冊和部署模型

儲存變更後,您現在可以在入口網站中註冊模型。 選取註冊模型,並指定有效的名稱。 選擇 Other 作為 [模型架構],選擇 Custom 作為 [架構名稱],選擇 1.0 作為 [架構版本]。 選取 Upload folder 選項,然後選取含有已更新的 score.pyconda_env.yaml 的資料夾。

選取模型,然後選取 Deploy 動作。 部署步驟假設您已佈建 AKS 推斷叢集。 認知搜尋目前不支援容器執行個體。

  1. 提供有效的端點名稱
  2. 選取 Azure Kubernetes Service 計算類型
  3. 為推斷叢集選取計算名稱
  4. enable authentication 切換成 on
  5. 選取 Key-based authentication 作為類型
  6. 選取已更新的 score.py 作為 entry script file
  7. 選取 conda_env.yaml 作為 conda dependencies file
  8. 選取 [部署] 按鈕以部署新的端點。

若要整合新建立的端點與認知搜尋

  1. 將包含單一汽車記錄的 JSON 檔案新增至 Blob 容器
  2. 使用匯入數據精靈設定 AI 擴充管線。 請務必選取 JSON 作為 parsing mode
  3. Add Enrichments 索引標籤上,選取單一技能 Extract people names 作為預留位置。
  4. 將新的欄位新增至稱為 predicted_price 的索引,類型為 Edm.Double,並將 [可擷取] 屬性設定為 true。
  5. 完成匯入資料程序

將 AML 技能新增至技能集

從技能集清單中,選取您建立的技能集。 您現在將編輯技能集,將人員識別技能換成 AML 技能,以預測價格。 在 [技能集定義 (JSON)] 索引標籤上,從 [技能] 下拉式清單中選取 Azure Machine Learning (AML)。 選取工作區,為了讓 AML 技能探索您的端點,工作區和搜尋服務必須位於相同的 Azure 訂用帳戶中。 選取您稍早在教學課程中建立的端點。 驗證技能中是否已填入您在部署端點時所設定的 URI 和驗證資訊。 複製技能範本,並取代技能集中的技能。 編輯技能:

  1. 將 name 設定為有效的名稱
  2. 加入描述
  3. 將 degreesOfParallelism 設定為 1
  4. 將 context 設定為 /document
  5. 將 inputs 設定為所有必要的輸入,請參閱下面的技能定義範例
  6. 設定 outputs 以擷取傳回的預測價格。
{
      "@odata.type": "#Microsoft.Skills.Custom.AmlSkill",
      "name": "AMLdemo",
      "description": "AML Designer demo",
      "context": "/document",
      "uri": "Your AML endpoint",
      "key": "Your AML endpoint key",
      "resourceId": null,
      "region": null,
      "timeout": "PT30S",
      "degreeOfParallelism": 1,
      "inputs": [
        {
          "name": "symboling",
          "source": "/document/symboling"
        },
        {
          "name": "make",
          "source": "/document/make"
        },
        {
          "name": "fuel-type",
          "source": "/document/fuel-type"
        },
        {
          "name": "aspiration",
          "source": "/document/aspiration"
        },
        {
          "name": "num-of-doors",
          "source": "/document/num-of-doors"
        },
        {
          "name": "body-style",
          "source": "/document/body-style"
        },
        {
          "name": "drive-wheels",
          "source": "/document/drive-wheels"
        },
        {
          "name": "engine-location",
          "source": "/document/engine-location"
        },
        {
          "name": "wheel-base",
          "source": "/document/wheel-base"
        },
        {
          "name": "length",
          "source": "/document/length"
        },
        {
          "name": "width",
          "source": "/document/width"
        },
        {
          "name": "height",
          "source": "/document/height"
        },
        {
          "name": "curb-weight",
          "source": "/document/curb-weight"
        },
        {
          "name": "engine-type",
          "source": "/document/engine-type"
        },
        {
          "name": "num-of-cylinders",
          "source": "/document/num-of-cylinders"
        },
        {
          "name": "engine-size",
          "source": "/document/engine-size"
        },
        {
          "name": "fuel-system",
          "source": "/document/fuel-system"
        },
        {
          "name": "bore",
          "source": "/document/bore"
        },
        {
          "name": "stroke",
          "source": "/document/stroke"
        },
        {
          "name": "compression-ratio",
          "source": "/document/compression-ratio"
        },
        {
          "name": "horsepower",
          "source": "/document/horsepower"
        },
        {
          "name": "peak-rpm",
          "source": "/document/peak-rpm"
        },
        {
          "name": "city-mpg",
          "source": "/document/city-mpg"
        },
        {
          "name": "highway-mpg",
          "source": "/document/highway-mpg"
        },
        {
          "name": "price",
          "source": "/document/price"
        }
      ],
      "outputs": [
        {
          "name": "predicted_price",
          "targetName": "predicted_price"
        }
      ]
    }

更新索引子輸出欄位對應

索引子輸出欄位對應會決定哪些擴充儲存至索引。 將索引子的輸出欄位對應區段換成下列程式碼片段:

"outputFieldMappings": [
    {
      "sourceFieldName": "/document/predicted_price",
      "targetFieldName": "predicted_price"
    }
  ]

您現在可以執行索引子,並驗證索引中的 predicted_price 屬性中是否已填入 AML 技能輸出的結果。