簡體   English   中英

在 AWS SageMaker 中為 Scikit Learn 模型調用終端節點

[英]Invoking Endpoint in AWS SageMaker for Scikit Learn Model

在 AWS Sagemaker 上部署 scikit 模型后,我使用以下方法調用我的模型:

import pandas as pd
payload = pd.read_csv('test3.csv')
payload_file = io.StringIO()
payload.to_csv(payload_file, header = None, index = None)

import boto3
client = boto3.client('sagemaker-runtime')
response = client.invoke_endpoint(
    EndpointName= endpoint_name,
    Body= payload_file.getvalue(),
    ContentType = 'text/csv')
import json
result = json.loads(response['Body'].read().decode())
print(result)

上面的代碼工作得很好,但是當我嘗試時:

payload = np.array([[100,5,1,2,3,4]])

我收到錯誤:

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from container-1 with message 
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>500 Internal Server Error</title> <h1>
Internal Server Error</h1> <p>The server encountered an internal error and was unable to complete your request.  
Either the server is overloaded or there is an error in the application.</p> 

Scikit-learn SageMaker Estimators and Models中提到,

SageMaker Scikit-learn 模型服務器提供 input_fn 的默認實現。 此函數將 JSON、CSV 或 NPY 編碼的數據反序列化為 NumPy 數組。

我想知道如何修改默認值以接受 2D numpy 數組,以便它可以用於實時預測。

有什么建議嗎? 我曾嘗試將推理管道與 Scikit-learn 和 Linear Learner 結合使用作為參考,但無法用 Scikit 模型替換 Linear Learner。 我收到了同樣的錯誤。

如果有人找到了更改默認 input_fn、predict_fn 和 output_fn 以接受 numpy 數組或字符串的方法,請分享。

但是我確實找到了一種使用默認值執行此操作的方法。

import numpy as np
import pandas as pd

df = pd.DataFrame(np.array([[100.0,0.08276299999999992,77.24,0.0008276299999999992,43.56,
                             6.6000000000000005,69.60699488825647,66.0,583.0,66.0,6.503081996847735,44.765133295284,
                             0.4844340723821271,21.35599999999999],
                            [100.0,0.02812099999999873,66.24,0.0002855600000003733,43.56,6.6000000000000005,
                             1.6884635296354735,66.0,78.0,66.0,6.754543287329573,47.06480204081666,
                             0.42642318733140017,0.4703999999999951],
                            [100.0,4.374382,961.36,0.043743819999999996,25153.96,158.6,649.8146514292529,120.0,1586.0
                             ,1512.0,-0.25255116297020636,1.2255274408634853,-2.5421402801039323,614.5056]]),
                  columns=['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n'])
import io
from io import StringIO
test_file = io.StringIO()
df.to_csv(test_file,header = None, index = None)

然后:

import boto3
client = boto3.client('sagemaker-runtime')
response = client.invoke_endpoint(
    EndpointName= endpoint_name,
    Body= test_file.getvalue(),
    ContentType = 'text/csv')
import json
result = json.loads(response['Body'].read().decode())
print(result)

但是,如果有更好的解決方案,那將非常有幫助。

您應該能夠為 model.deploy() 返回的預測器設置序列化器/反序列化器。 在此處的 FM 示例筆記本中有一個這樣做的示例:

https://github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/factorization_machines_mnist/factorization_machines_mnist.ipynb

請試試這個,讓我知道它是否適合你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM