从AWS SageMaker中部署的SKLearn模型返回最近的邻居

时间:2019-06-29 14:21:36

标签: python scikit-learn nearest-neighbor amazon-sagemaker

我在AWS Sagemaker中构建了无监督的NearestNeighbors模型,并将其部署到了端点。现在,我尝试使用模型端点为给定的输入向量生成k个最近邻。

但是,出现以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-31-f595a603f928> in <module>()
     12 # print(predictor.predict(sample_vector))
     13 
---> 14 distance, indice = pred.kneighbors(sample_vector, n_neighbors=11)

AttributeError: 'SKLearnPredictor' object has no attribute 'kneighbors'

SKLearn NearestNeighbors学习者没有预测方法。因此,尝试使用“预测”方法而不是“ .kneighbors”也会产生错误:

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model 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>
". See https://us-east-2.console.aws.amazon.com/cloudwatch/home?region=us-east-2#logEventViewer:group=/aws/sagemaker/Endpoints/sagemaker-scikit-learn-2019-06-29-13-11-50-512 in account 820407560908 for more information.

是否可以在Sagemaker中调用此端点,或者Sagemaker SKLearn SDK仅允许使用“预测”方法的模型?

2 个答案:

答案 0 :(得分:1)

推断时,依次使用3个功能:input_fnpredict_fnoutput_fn。它们采用默认值,但是您可以覆盖它们以执行所需的自定义操作。就您而言,例如,您可以覆盖predict_fn以运行所需的命令。在https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#deploying-scikit-learn-models

上查看更多详细信息

答案 1 :(得分:0)

我为此苦苦挣扎了好几天,一直在查看 predict_fn 和其他地方。就我而言,我是在 python lambda 函数中调用端点,如果您只是添加 Accept='application/json; verbose=True' 作为附加参数,它会输出距离。对于其他输出选项:https://docs.aws.amazon.com/sagemaker/latest/dg/kNN-inference-formats.html

import boto3
runtime= boto3.client('runtime.sagemaker')
payload = '1,2,3' #comma separated string
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                               ContentType='text/csv',
                                               Body=payload,
                                               Accept='application/json; verbose=True')