エンドポイントを呼び出す - Amazon SageMaker AI

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

エンドポイントを呼び出す

本番環境Amazon SageMaker SageMaker Canvas モデルは、アプリケーションで使用できます。他の SageMaker AI リアルタイムエンドポイントを呼び出すのと同じ方法で、エンドポイントをプログラムで呼び出します。エンドポイントをプログラムで呼び出すと、「デプロイをテストする」で説明されているのと同じフィールドを含むレスポンスオブジェクトが返されます。

エンドポイントをプログラムで呼び出す方法の詳細については、「リアルタイム推論用のモデルを呼び出す」を参照してください。

次の Python の例は、モデルタイプに基づいてエンドポイントを呼び出す方法を示します。

次の例は、エンドポイントにデプロイした JumpStart 基盤モデルを呼び出す方法を示します。

import boto3 import pandas as pd client = boto3.client("runtime.sagemaker") body = pd.DataFrame( [['feature_column1', 'feature_column2'], ['feature_column1', 'feature_column2']] ).to_csv(header=False, index=False).encode("utf-8") response = client.invoke_endpoint( EndpointName="endpoint_name", ContentType="text/csv", Body=body, Accept="application/json" )

次の例は、数値予測モデルまたはカテゴリ予測モデルを呼び出す方法を示します。

import boto3 import pandas as pd client = boto3.client("runtime.sagemaker") body = pd.DataFrame(['feature_column1', 'feature_column2'], ['feature_column1', 'feature_column2']).to_csv(header=False, index=False).encode("utf-8") response = client.invoke_endpoint( EndpointName="endpoint_name", ContentType="text/csv", Body=body, Accept="application/json" )

次の例は、時系列予測モデルを呼び出す方法を示します。時系列予測モデルの呼び出しをテストする方法の完全な例については、「Time-Series Forecasting with Amazon SageMaker Autopilot」を参照してください。

import boto3 import pandas as pd csv_path = './real-time-payload.csv' data = pd.read_csv(csv_path) client = boto3.client("runtime.sagemaker") body = data.to_csv(index=False).encode("utf-8") response = client.invoke_endpoint( EndpointName="endpoint_name", ContentType="text/csv", Body=body, Accept="application/json" )

次の例は、画像予測モデルを呼び出す方法を示します。

import boto3 client = boto3.client("runtime.sagemaker") with open("example_image.jpg", "rb") as file: body = file.read() response = client.invoke_endpoint( EndpointName="endpoint_name", ContentType="application/x-image", Body=body, Accept="application/json" )

次の例は、テキスト予測モデルを呼び出す方法を示します。

import boto3 import pandas as pd client = boto3.client("runtime.sagemaker") body = pd.DataFrame([["Example text 1"], ["Example text 2"]]).to_csv(header=False, index=False).encode("utf-8") response = client.invoke_endpoint( EndpointName="endpoint_name", ContentType="text/csv", Body=body, Accept="application/json" )