

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

# 即時預測
<a name="timeseries-forecasting-realtime"></a>

當您需要即時產生預測時，即時預測非常有用，例如對於需要立即回應的應用程式或對單一資料點進行預測時。

透過將 AutoML 模型部署為即時端點，您可以隨需產生預測，並將接收新資料和取得預測之間的延遲降至最低。這使得即時預測非常適合需要立即、個人化或事件驅動預測功能的應用程式。

對於即時預測，資料集應該是輸入資料集的子集。即時端點的輸入資料大小約為 6MB，回應逾時限制為 60 秒。我們建議您一次攜帶一個或幾件項目。

您可以使用 SageMaker APIs 擷取 AutoML 任務的最佳候選項目，然後使用該候選項目建立 SageMaker AI 端點。

或者，您可以在建立 Autopilot 實驗時選擇自動部署選項。若要取得有關設定自動部署模型的資訊，請參閱[如何啟用自動部署](autopilot-create-experiment-timeseries-forecasting.md#timeseries-forecasting-auto-model-deployment)。

**若要使用最佳模型候選項目建立 SageMaker AI 端點：**

1. 

**擷取 AutoML 任務的詳細資訊。**

   下列 AWS CLI 命令範例使用 [DescribeAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJobV2.html) API 來取得 AutoML 任務的詳細資訊，包括最佳模型候選項目的相關資訊。

   ```
   aws sagemaker describe-auto-ml-job-v2 --auto-ml-job-name {{job-name}} --region {{region}}
   ```

1. 

**從 [InferenceContainers](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLCandidate.html#sagemaker-Type-AutoMLCandidate-InferenceContainers) 擷取容器定義，以獲得最佳模型候選項目。**

   容器定義是用於託管訓練 SageMaker AI 模型以進行預測的容器化環境。

   ```
   BEST_CANDIDATE=$(aws sagemaker describe-auto-ml-job-v2 \
     --auto-ml-job-name {{job-name}} 
     --region {{region}} \
     --query 'BestCandidate.InferenceContainers[0]' \
     --output json
   ```

   此命令會擷取最佳模型候選項目的容器定義，並將其存放在 `BEST_CANDIDATE` 變數中。

1. 

**使用最佳候選容器定義建立 SageMaker AI 模型。**

   使用上一個步驟中的容器定義，利用 [CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API 建立一個 SageMaker AI 模型。

   ```
   aws sagemaker create-model \
               --model-name '{{your-candidate-name>}}' \
               --primary-container "$BEST_CANDIDATE"
               --execution-role-arn '{{execution-role-arn>}}' \
               --region '{{region>}}
   ```

   `--execution-role-arn` 參數指定使用模型進行推論時，SageMaker AI 擔任的 IAM 角色。如需此角色所需許可的詳細資訊，請參閱 [CreateModel API：執行角色許可](https://docs.aws.amazon.com/)。

1. 

**使用模型建立 SageMaker AI 端點組態。**

   下列 AWS CLI 命令使用 [CreateEndpointConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) API 來建立端點組態。

   ```
   aws sagemaker create-endpoint-config \
     --production-variants file://production-variants.json \
     --region '{{region}}'
   ```

   `production-variants.json` 檔案包含模型組態的位置，包括模型名稱和執行個體類型。
**注意**  
建議使用 [m5.12xlarge](https://aws.amazon.com/ec2/instance-types/m5/) 執行個體進行即時預測。

   ```
   [
       {
         "VariantName": "{{variant-name}}",
         "ModelName": "{{model-name}}",
         "InitialInstanceCount": {{1}},
         "InstanceType": "{{m5.12xlarge}}"
       }
     ]
   }
   ```

1. 

**使用端點組態建立 SageMaker AI 端點。**

   下列 AWS CLI 範例使用 [CreateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API 來建立端點。

   ```
   aws sagemaker create-endpoint \
               --endpoint-name '{{endpoint-name>}}' \
               --endpoint-config-name '{{endpoint-config-name}}' \
               --region '{{region}}'
   ```

   使用 [DescribeEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeEndpoint.html) API 來檢查即時推論端點部署的進度。請參閱下列 AWS CLI 命令做為範例。

   ```
   aws sagemaker describe-endpoint \
               --endpoint-name '{{endpoint-name}}' \
               --region '{{region}}'
   ```

   `EndpointStatus`變更為後`InService`，端點即可用於即時推論。

1. 

**叫用 SageMaker AI 端點進行預測。**

   ```
   aws sagemaker invoke-endpoint \
               --endpoint-name '{{endpoint-name}}' \ 
               --region '{{region}}' \
               --body file://input-data-in-bytes.json \
               --content-type '{{application/json}}' {{outfile}}
   ```

   其中 `input-data-in-bytes.json` 檔案包含預測的輸入資料。