

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

# 部署 Autopilot 模型以進行即時推論

訓練 Amazon SageMaker Autopilot 模型後，您可以設定端點並以互動方式取得預測。下一節說明將模型部署到 SageMaker AI 即時推論端點的步驟，以從您的模型取得預測。

## 即時推論
<a name="autopilot-deploy-models-text-image-classification-realtime"></a>

即時推論非常適合您具有即時、互動、低延遲需求的推論工作負載。本節說明如何使用即時推論，以互動方式從模型取得預測。

您可以使用 SageMaker API 手動部署在 Autopilot 實驗中產生最佳驗證指標的模型，如下所示。

或者，您也可以在建立 Autopilot 實驗時選擇自動部署選項。如需設定自動部署模型的相關資訊，請參閱 `[CreateAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#API_CreateAutoMLJobV2_RequestParameters)` 請求參數中的 `[ModelDeployConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-ModelDeployConfig)`。這會自動建立端點。

**注意**  
若要避免產生不必要的費用，您可以刪除不需要的端點和從模型部署建立的資源。如需有關依區域劃分之執行個體定價的資訊，請參閱 [Amazon SageMaker 定價](https://aws.amazon.com/sagemaker/pricing/)。

1. **取得候選項目容器定義**

   從 [InferenceContainers](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLCandidate.html#sagemaker-Type-AutoMLCandidate-InferenceContainers) 取得候選容器定義。推論的容器定義是指為部署和執行訓練有素的 SageMaker AI 模型以進行預測而設計的容器化環境。

   下列 AWS CLI 命令範例使用 [DescribeAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJobV2.html) API 來取得最佳模型候選項目的候選定義。

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

1. **列出候選**

   下列 AWS CLI 命令範例使用 [ListCandidatesForAutoMLJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ListCandidatesForAutoMLJob.html) API 列出所有模型候選項目。

   ```bash
   aws sagemaker list-candidates-for-auto-ml-job --auto-ml-job-name {{<job-name>}} --region {{<region>}}
   ```

1. **建立 SageMaker AI 模型**

   使用先前步驟中的容器定義和您選擇的候選容器定義，透過 [CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API 建立 SageMaker AI 模型。請參閱下列 AWS CLI 命令做為範例。

   ```bash
   aws sagemaker create-model --model-name '{{<your-candidate-name>}}' \
                       --containers ['{{<container-definition1}}>, {{<container-definition2>}}, {{<container-definition3>}}]' \
                       --execution-role-arn '{{<execution-role-arn>}}' --region '{{<region>}}
   ```

1. **建立一個端點組態**

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

   ```bash
   aws sagemaker create-endpoint-config --endpoint-config-name '{{<your-endpoint-config-name>}}' \
                       --production-variants '{{<list-of-production-variants>}}' \
                       --region '{{<region>}}'
   ```

1. **建立端點** 

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

   ```bash
   aws sagemaker create-endpoint --endpoint-name '{{<your-endpoint-name>}}' \
                       --endpoint-config-name '{{<endpoint-config-name-you-just-created>}}' \
                       --region '{{<region>}}'
   ```

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

   ```bash
   aws sagemaker describe-endpoint —endpoint-name '{{<endpoint-name>}}' —region {{<region>}}
   ```

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

1. **調用端點** 

   下列命令結構會調用端點以進行即時推論。

   ```bash
   aws sagemaker invoke-endpoint --endpoint-name '{{<endpoint-name>}}' \ 
                     --region '{{<region>}}' --body '{{<your-data>}}' [--content-type] '{{<content-type>}}' {{<outfile>}}
   ```