gpt4 book ai didi

python - 在 AWS SageMaker 中调用 Scikit Learn 模型的端点

转载 作者:行者123 更新时间:2023-11-30 09:16:39 25 4
gpt4 key购买 nike

在 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 model server provides a default implementation of input_fn. This function deserializes JSON, CSV, or NPY encoded data into a NumPy array.

我想知道如何修改默认值以接受 2D numpy 数组,以便将其用于实时预测。

有什么建议吗?我尝试过使用 Inference Pipeline with Scikit-learn and Linear Learner作为引用,但无法用 Scikit 模型替代线性学习器。我收到了同样的错误。

最佳答案

如果有人找到了更改默认 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)

但是如果有更好的解决方案,那将会非常有帮助。

关于python - 在 AWS SageMaker 中调用 Scikit Learn 模型的端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54358944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com