从 AWS SageMaker 中部署的 SKLearn 模型返回最近的邻居
Returning nearest neighbors from SKLearn model deployed in AWS 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 学习器没有预测方法。因此,尝试使用 'predict' 方法而不是 '.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 是否仅允许具有 'predict' 方法的模型?
推断,依次使用了3个函数:input_fn
、predict_fn
、output_fn
。它们采用默认值,但您可以覆盖它们以执行所需的自定义操作。在您的情况下,例如,您可以将 predict_fn
覆盖为 运行 所需的命令。在此处查看更多详细信息 https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#deploying-scikit-learn-models
在 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')
我在 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 学习器没有预测方法。因此,尝试使用 'predict' 方法而不是 '.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 是否仅允许具有 'predict' 方法的模型?
推断,依次使用了3个函数:input_fn
、predict_fn
、output_fn
。它们采用默认值,但您可以覆盖它们以执行所需的自定义操作。在您的情况下,例如,您可以将 predict_fn
覆盖为 运行 所需的命令。在此处查看更多详细信息 https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#deploying-scikit-learn-models
在 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')