

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

# 檢查預測結果
<a name="async-inference-check-predictions"></a>

您有幾種方式可以檢查非同步端點的預測結果。一些選項為：

1. Amazon SNS 主題。

1. 檢查 Amazon S3 儲存貯體中的輸出。

## Amazon SNS 主題
<a name="async-inference-check-predictions-sns-topic"></a>

Amazon SNS 是面向消息的應用程式的通知服務，多個訂閱者通過一系列傳輸協議 (包括 HTTP、Amazon SQS 和電子郵件) 請求和接收時間關鍵性消息的 “推送” 通知。使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) 並指定 Amazon SNS 主題建立端點時，Amazon SageMaker 非同步推論會發布通知。

**注意**  
為了接收 Amazon SNS 通知，您的 IAM 角色必須具有 `sns:Publish` 許可。請參閱[完成先決條件](async-inference-create-endpoint-prerequisites.md)瞭解有關使用異步推理所必須滿足的要求的資訊。

要使用 Amazon SNS 檢查異步終端節點的預測結果，首先需要建立一個主題、訂閱主題、確認您對主題的訂閱，並記下該主題的 Amazon Resource Name (ARN)。有關如何建立、訂閱和查找 Amazon SNS 主題的亞馬遜 ARN 的詳細資訊，請參閱[配置 Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/sns-configuring.html)。

當您使用 `CreateEndpointConfig` 建立端點組態時，請在 `AsyncInferenceConfig` 欄位中提供 Amazon SNS 主題 ARN。您可以同時指定 Amazon SNS `ErrorTopic` 和 `SuccessTopic`。

```
import boto3

sagemaker_client = boto3.client('sagemaker', region_name=<aws_region>)

sagemaker_client.create_endpoint_config(
    EndpointConfigName={{<endpoint_config_name>}}, # You specify this name in a CreateEndpoint request.
    # List of ProductionVariant objects, one for each model that you want to host at this endpoint.
    ProductionVariants=[
        {
            "VariantName": {{"variant1"}}, # The name of the production variant.
            "ModelName": {{"model_name"}}, 
            "InstanceType": {{"ml.m5.xlarge"}}, # Specify the compute instance type.
            "InitialInstanceCount": {{1}} # Number of instances to launch initially.
        }
    ],
    AsyncInferenceConfig={
        "OutputConfig": {
            # Location to upload response outputs when no location is provided in the request.
            "S3OutputPath": "s3://{{<bucket>/<output_directory>}}"
            "NotificationConfig": {
                "SuccessTopic": "arn:aws:sns:{{aws-region:account-id:topic-name}}",
                "ErrorTopic": "arn:aws:sns:{{aws-region:account-id:topic-name}}",
            }
        }
    }
)
```

建立端點並調用它後，您會收到 Amazon SNS 主題的通知。例如，如果您訂閱接收來自主題的電子郵件通知，則每次調用終端節點時都會收到電子郵件通知。下列範例說明成功調用電子郵件通知的 JSON 內容。

```
{
   "awsRegion":"us-east-1",
   "eventTime":"2022-01-25T22:46:00.608Z",
   "receivedTime":"2022-01-25T22:46:00.455Z",
   "invocationStatus":"Completed",
   "requestParameters":{
      "contentType":"text/csv",
      "endpointName":"{{<example-endpoint>}}",
      "inputLocation":"s3://{{<bucket>}}/{{<input-directory>}}/input-data.csv"
   },
   "responseParameters":{
      "contentType":"text/csv; charset=utf-8",
      "outputLocation":"s3://{{<bucket>}}/{{<output_directory>}}/prediction.out"
   },
   "inferenceId":"11111111-2222-3333-4444-555555555555", 
   "eventVersion":"1.0",
   "eventSource":"aws:sagemaker",
   "eventName":"InferenceResult"
}
```

## 請檢查您的 S3 儲存貯體
<a name="async-inference-check-predictions-s3-bucket"></a>

當你使用 `InvokeEndpointAsync` 調用一個端點，它就會傳回一個回應物件。您可以使用回應物件取得存放輸出所在位置的 Amazon S3 URI。使用輸出位置，您就可以使用 SageMaker Python SDK SageMaker AI 工作階段類別，以程式化方式檢查輸出。

下列會存放 `InvokeEndpointAsync` 的輸出字典做為名為回應的變數。使用回應變數，您就會取得 Amazon S3 輸出 URI，並存放做為名為 `output_location` 的字串變數。

```
import uuid
import boto3

sagemaker_runtime = boto3.client("sagemaker-runtime", region_name={{<aws_region>}})

# Specify the S3 URI of the input. Here, a single SVM sample
input_location = "s3://{{bucket-name/test_point_0.libsvm}}" 

response = sagemaker_runtime.invoke_endpoint_async(
    EndpointName={{'<endpoint-name>'}},
    InputLocation=input_location,
    InferenceId=str(uuid.uuid4()), 
    ContentType={{"text/libsvm"}} #Specify the content type of your data
)

output_location = response['OutputLocation']
print(f"OutputLocation: {output_location}")
```

如需支援的執行個體內容類型的資訊，請參閱[推論的一般資料格式](cdf-inference.md)。

使用 Amazon S3 輸出位置，您就可以使用 [SageMaker Python SDK SageMaker AI 工作階段類別](https://sagemaker.readthedocs.io/en/stable/api/utility/session.html?highlight=session)，在 Amazon S3 檔案中進行讀取。下列程式碼範例示範如何建立函式 (`get_ouput`)，以重複嘗試從 Amazon S3 輸出位置讀取檔案：

```
import sagemaker
import urllib, time
from botocore.exceptions import ClientError

sagemaker_session = sagemaker.session.Session()

def get_output(output_location):
    output_url = urllib.parse.urlparse(output_location)
    bucket = output_url.netloc
    key = output_url.path[1:]
    while True:
        try:
            return sagemaker_session.read_s3_file(
                                        bucket=output_url.netloc, 
                                        key_prefix=output_url.path[1:])
        except ClientError as e:
            if e.response['Error']['Code'] == 'NoSuchKey':
                print("waiting for output...")
                time.sleep(2)
                continue
            raise
            
output = get_output(output_location)
print(f"Output: {output}")
```