

我們不再更新 Amazon Machine Learning 服務或接受新使用者。本文件可供現有使用者使用，但我們不再更新。如需詳細資訊，請參閱[什麼是 Amazon Machine Learning](https://docs.aws.amazon.com/machine-learning/latest/dg/what-is-amazon-machine-learning.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 要求即時預測
<a name="requesting-real-time-predictions"></a>

即時預測是對 Amazon Machine Learning (Amazon ML) 的同步呼叫。當 Amazon ML 取得請求時，就會進行預測，並立即傳回回應。即時預測常用來在互動式 Web、行動或桌面應用程式內啟用預測功能。您可以使用低延遲 `Predict` API，即時查詢使用 Amazon ML 建立的 ML 模型進行預測。`Predict` 操作會接受要求承載中的單一輸入觀察，並在回應中同步傳回預測。這與批次預測 API 不同，該 API 會使用指向輸入觀察位置的 Amazon ML 資料來源物件 ID 叫用，並以非同步方式將 URI 傳回至包含所有這些觀察預測的檔案。Amazon ML 會在 100 毫秒內回應大多數的即時預測請求。

您可以在 Amazon ML 主控台中嘗試即時預測，而不會產生費用。如果您接著決定使用即時預測，則必須先建立即時預測產生的端點。您可以在 Amazon ML 主控台或使用 `CreateRealtimeEndpoint` API 執行此操作。在您有端點之後，請使用即時預測 API 來產生即時預測。

**注意**  
在您建立模型的即時端點之後，即會根據模型大小來開始產生容量保留費用。如需詳細資訊，請參閱[ 定價](https://aws.amazon.com/machine-learning/pricing/)。如果您在主控台中建立即時端點，則主控台會顯示端點將持續增加的預估費用明細。若要在不再需要取得該模型的即時預測時停止產生費用，請使用主控台或 `DeleteRealtimeEndpoint` 操作來移除即時端點。

如需`Predict`請求和回應的範例，請參閱《*Amazon Machine Learning API 參考*》中的[預測](https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_Predict.html)。若要查看使用您模型的確切回應格式範例，請參閱[嘗試即時預測](#testing-real-time-predictions)。

**Topics**
+ [

## 嘗試即時預測
](#testing-real-time-predictions)
+ [

## 建立即時端點
](#creating-a-real-time-endpoint)
+ [

## 找到即時預測端點 (主控台)
](#locate-endpoint-by-console)
+ [

## 找到即時預測端點 (API)
](#locating-the-real-time-prediction-endpoint)
+ [

## 建立即時預測要求
](#real-time-prediction-request-format)
+ [

## 刪除即時端點
](#delete-endpoint)

## 嘗試即時預測
<a name="testing-real-time-predictions"></a>

為了協助您決定是否啟用即時預測，Amazon ML 可讓您嘗試在單一資料記錄上產生預測，而不會產生與設定即時預測端點相關聯的額外費用。若要嘗試即時預測，您必須具有 ML 模型。若要大規模建立即時預測，請使用 Amazon Machine Learning API 參考中的[預測](https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_Predict.html) API。 *Amazon Machine Learning *

**嘗試即時預測**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/machinelearning/](https://console.aws.amazon.com/machinelearning/) 的 Amazon Machine Learning 主控台。

1. 從導覽列的 **Amazon Machine Learning** 下拉式選單中選擇 **ML models (ML 模型)**。

1. 選擇您想要用來嘗試即時預測的模型，例如教學課程中的 `Subscription propensity model`。

1. 在 ML 模型報告頁面上，於 **Predictions (預測)** 下選擇 **Summary (摘要)**，然後選擇 **Try real-time predictions (嘗試即時預測)**。  
![\[Tools section with option to try real-time predictions.\]](http://docs.aws.amazon.com/zh_tw/machine-learning/latest/dg/images/try-real-time.png)

   Amazon ML 會顯示構成 Amazon ML 用來訓練模型之資料記錄的變數清單。

1. 在表單的每個欄位中輸入資料，或以 CSV 格式將單一資料記錄貼入文字方塊，即可繼續進行。

   若要使用表單，請在各個 **Value (值)** 欄位內輸入您想要用來測試即時預測的資料。如果您輸入的資料記錄未包含一或多個資料屬性的值，請將輸入欄位空白。

   若要提供資料記錄，請選擇 **Paste a record (貼上記錄)**。將單一 CSV 格式的資料列貼到文字欄位中，然後選擇**提交**。Amazon ML 會自動為您填入**值**欄位。
**注意**  
資料記錄中的資料必須具有與培訓資料相同數目的資料行，並依相同的順序排列。唯一的例外是您應該省略目標值。如果您包含目標值，Amazon ML 會忽略它。

1. 在頁面底部，選擇 **Create prediction (建立預測)**。Amazon ML 會立即傳回預測。

   在 **Prediction results (預測結果)** 窗格中，您會看到 `Predict` API 呼叫所傳回的預測物件，以及 ML 模型類型、目標變數的名稱和預測類別或值。如需解譯結果的資訊，請參閱[解譯二元分類 ML 模型的批次預測檔案內容](reading-the-batchprediction-output-files.md#interpreting-the-contents-of-batch-prediction-files-for-a-binary-classification-ml-model)。  
![\[Prediction results showing binary ML model type with predicted label 0 and score details.\]](http://docs.aws.amazon.com/zh_tw/machine-learning/latest/dg/images/real-time-result.png)

## 建立即時端點
<a name="creating-a-real-time-endpoint"></a>

若要產生即時預測，您需要建立即時端點。若要建立即時端點，您必須已有要產生即時預測的 ML 模型。您可以使用 Amazon ML 主控台或呼叫 `CreateRealtimeEndpoint` API 來建立即時端點。如需使用 `CreateRealtimeEndpoint` API 的詳細資訊，請參閱《Amazon Machine Learning API 參考[https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_CreateRealtimeEndpoint.html](https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_CreateRealtimeEndpoint.html)》中的 。

**建立即時端點**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/machinelearning/](https://console.aws.amazon.com/machinelearning/) 的 Amazon Machine Learning 主控台。

1. 從導覽列的 **Amazon Machine Learning** 下拉式選單中選擇 **ML models (ML 模型)**。

1. 選擇您要產生即時預測的模型。

1. 在 **ML model summary (ML 模型摘要)** 頁面上，於 **Predictions (預測)** 下選擇 **Create real-time endpoint (建立即時端點)**。

   即會顯示說明即時預測定價方式的對話方塊。

1. 選擇**建立**。即時端點請求會傳送至 Amazon ML 並進入佇列。即時端點的狀態是 **Updating (正在更新)**。  
![\[Real-time endpoint status shown as "Updating" in a user interface element.\]](http://docs.aws.amazon.com/zh_tw/machine-learning/latest/dg/images/endpoint-updating.png)

1. 當即時端點就緒時，狀態會變更為**就緒**，Amazon ML 會顯示端點 URL。透過 `Predict` API，使用端點 URL 建立即時預測要求。如需使用 `Predict` API 的詳細資訊，請參閱《Amazon Machine Learning API 參考[https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_Predict.html](https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_Predict.html)》中的 。  
![\[Real-time endpoint status showing Ready with an endpoint URL and Peak Requests Per Second value.\]](http://docs.aws.amazon.com/zh_tw/machine-learning/latest/dg/images/endpoint-ready.png)

## 找到即時預測端點 (主控台)
<a name="locate-endpoint-by-console"></a>

若要使用 Amazon ML 主控台尋找 ML 模型的端點 URL，請導覽至模型的 **ML 模型摘要**頁面。

**找到即時端點 URL**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/machinelearning/](https://console.aws.amazon.com/machinelearning/) 的 Amazon Machine Learning 主控台。

1. 從導覽列的 **Amazon Machine Learning** 下拉式選單中選擇 **ML models (ML 模型)**。

1. 選擇您要產生即時預測的模型。

1. 在 **ML model summary (ML 模型摘要)** 頁面上，向下捲動以查看 **Predictions (預測)** 區段。

1. 模型的端點 URL 會列在 **Real-time prediction (即時預測)** 中。使用此 URL 做為即時預測呼叫的 **Endpoint Url (端點 URL)**。如需如何使用端點產生預測的資訊，請參閱《Amazon Machine Learning API 參考[https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_Predict.html](https://docs.aws.amazon.com/machine-learning/latest/APIReference/API_Predict.html)》中的 。

## 找到即時預測端點 (API)
<a name="locating-the-real-time-prediction-endpoint"></a>

當您使用 `CreateRealtimeEndpoint` 操作建立即時端點時，會在回應中將端點的 URL 和狀態傳回給您。如果您已使用主控台建立即時端點，或想要稍後擷取您所建立端點的 URL 和狀態，請呼叫具有您要查詢即時預測之模型識別符的 `GetMLModel` 操作。端點資訊包含在回應的 `EndpointInfo` 區段中。針對具有相關聯即時端點的模型，`EndpointInfo` 可能會如下所示：

```
"EndpointInfo":{
    "CreatedAt": 1427864874.227,
    "EndpointStatus": "READY",
    "EndpointUrl": "https://endpointUrl",
    "PeakRequestsPerSecond": 200
}
```

沒有即時端點的模型會傳回下列各項：

```
EndpointInfo":{
    "EndpointStatus": "NONE",
    "PeakRequestsPerSecond": 0
}
```

## 建立即時預測要求
<a name="real-time-prediction-request-format"></a>

範例 `Predict` 要求承載可能如下所示：

```
{
    "MLModelId": "model-id",
    "Record":{
        "key1": "value1",
        "key2": "value2"
    },
    "PredictEndpoint": "https://endpointUrl"
}
```

`PredictEndpoint` 欄位必須對應至 `EndpointInfo` 結構的 `EndpointUrl` 欄位。Amazon ML 使用此欄位將請求路由到即時預測機群中的適當伺服器。

`MLModelId` 是具有即時端點之先前培訓過模型的識別符。

`Record` 是變數名稱與變數值的對應。每個配對都代表一個觀察。`Record` 映射包含 Amazon ML 模型的輸入。它類似培訓資料集中的單一資料列，而沒有目標變數。不論培訓資料中的值類型為何，`Record` 都會包含字串對字串對應。

**注意**  
您可以省略沒有值的變數，但這可能會降低您預測的準確性。您可以包含的變數愈多，模型會更準確。

`Predict` 要求所傳回的回應格式取決於用於查詢預測的模型類型。在所有情況下，`details` 欄位都會包含預測要求的資訊，尤其是包含具有模型類型的 `PredictiveModelType` 欄位。

下列範例示範二元模型的回應：

```
{
    "Prediction":{
        "details":{
            "PredictiveModelType": "BINARY"
        },
        "predictedLabel": "0",
        "predictedScores":{
            "0": 0.47380468249320984
        }
    }
}
```

請注意包含預測標籤`predictedLabel`的欄位，在此案例中為 0。Amazon ML 透過比較預測分數與分類截止值來計算預測標籤：
+ 您可以檢查 `GetMLModel` 操作回應中的 `ScoreThreshold` 欄位，或在 Amazon ML 主控台中檢視模型資訊，以取得目前與 ML 模型相關聯的分類截止。如果您未設定分數閾值，Amazon ML 會使用預設值 0.5。
+ 您可以檢查 `predictedScores` 對應，以取得二元分類模型的確切預測分數。在這個對應內，預測標籤會與確切的預測分數搭配使用。

如需二元預測的詳細資訊，請參閱[解譯預測](binary-model-insights.md#interpreting-the-predictions)。

下列範例示範回歸模型的回應。請注意，預測數值位於 `predictedValue` 欄位中：

```
{
    "Prediction":{
        "details":{
            "PredictiveModelType": "REGRESSION"
        },
        "predictedValue": 15.508452415466309
    }
}
```

下列範例示範多類別模型的回應：

```
{
    "Prediction":{
        "details":{
            "PredictiveModelType": "MULTICLASS"
        },
        "predictedLabel": "red",
        "predictedScores":{
            "red": 0.12923571467399597,
            "green": 0.08416014909744263,
            "orange": 0.22713537514209747,
            "blue": 0.1438363939523697,
            "pink": 0.184102863073349,
            "violet": 0.12816807627677917,
            "brown": 0.10336143523454666
        }
    }
}
```

與二元分類模型類似，預測標籤/類別位於 `predictedLabel` 欄位中。您可以查看 `predictedScores` 對應，以進一步了解預測與每個類別的緊密相關程度。此對應內的類別分數愈高，預測與類別的相關性愈強，而最高值最後會選取為 `predictedLabel`。

如需多類別預測的詳細資訊，請參閱[多類別模型深入分析](multiclass-model-insights.md)。

## 刪除即時端點
<a name="delete-endpoint"></a>

當您完成即時預測時，請刪除即時端點，以避免產生額外費用。只要刪除端點，就會立即停止產生費用。

**刪除即時端點**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/machinelearning/](https://console.aws.amazon.com/machinelearning/) 的 Amazon Machine Learning 主控台。

1. 從導覽列的 **Amazon Machine Learning** 下拉式選單中選擇 **ML models (ML 模型)**。

1. 選擇不再需要即時預測的模型。

1. 在 ML 模型報告頁面上，於 **Predictions (預測)** 下選擇 **Summary (摘要)**。

1. 選擇 **Delete real-time endpoint (刪除即時端點)**。

1. 在 **Delete real-time endpoint (刪除即時端點)** 對話方塊中，選擇 **Delete (刪除)**。