예측 결과 검사 - 아마존 SageMaker

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

예측 결과 검사

비동기 엔드포인트의 예측 결과를 확인할 수 있는 방법에는 여러 가지가 있습니다. 다음과 같은 몇 가지 옵션이 있습니다.

  1. Amazon SNS 주제

  2. Amazon S3 버킷의 출력을 확인합니다.

Amazon SNS 주제

Amazon SNS는 메시징 지향 애플리케이션을 위한 알림 서비스로 여러 구독자가 HTTP, Amazon SQS, 이메일을 비롯한 다양한 전송 프로토콜을 통해 시간이 중요한 메시지의 “푸시” 알림을 요청하고 수신할 수 있습니다. Amazon SageMaker 비동기 추론은 Amazon SNS 주제를 사용하여 CreateEndpointConfig엔드포인트를 생성하고 지정하는 경우 알림을 게시합니다.

참고

Amazon SNS 알림을 수신하려면 IAM 역할에 sns:Publish 권한이 있어야 합니다. 비동기 추론을 사용하기 위해 충족해야 하는 요구 사항에 대한 자세한 내용은 필수 조건을 참고하십시오.

Amazon SNS를 사용하여 비동기 엔드포인트에서 예측 결과를 확인하려면, 먼저 주제를 생성하고, 주제를 구독하고, 주제 구독을 확인하고, 해당 주제의 Amazon 리소스 이름(ARN)을 기록해 두어야 합니다. Amazon SNS 주제의 Amazon ARN을 생성하고 구독하고 찾는 방법에 대한 자세한 내용은 Amazon SNS 구성을 참고하십시오.

CreateEndpointConfig로 엔드포인트 구성을 생성할 때 AsyncInferenceConfig 필드에 Amazon SNS 주제 ARN을 입력하십시오. Amazon SNS ErrorTopicSuccessTopic을 모두 지정할 수 있습니다.

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 버킷 확인

InvokeEndpointAsync로 엔드포인트를 호출하면 응답 객체가 반환됩니다. 응답 객체를 사용하여 출력이 저장되는 Amazon S3 URI를 가져올 수 있습니다. 출력 위치를 사용하면 SageMaker Python SDK SageMaker 세션 클래스를 사용하여 프로그래밍 방식으로 출력을 확인할 수 있습니다.

다음은 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}")

지원되는 콘텐츠 유형에 대한 자세한 내용은 추론을 위한 일반적인 데이터 형식을 참고하십시오.

그러면 Amazon S3 출력 위치에서 SageMaker Python SDK SageMaker 세션 클래스를 사용하여 Amazon S3 파일을 읽을 수 있습니다. 다음 코드 예제는 Amazon S3 출력 위치에서 파일을 반복적으로 읽으려고 시도하는 함수(get_ouput)를 생성하는 방법을 보여줍니다.

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}")