调用PyTorch模型的SageMaker端点

2024-04-24 15:54:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Postman和AWS CLI调用我的SageMaker模型端点。端点的状态为“正在服务”,但每当我尝试调用它时,它都会给我一个错误。当我尝试在SageMaker笔记本中使用predict函数并为其提供一个numpy数组(例如np.array([1,2,3,4]))时,它成功地为我提供了一个输出。我不确定我做错了什么

$ aws2 sagemaker-runtime invoke-endpoint \
$ --endpoint-name=pytorch-model \
$ --body=1,2 \
$ --content-type=text/csv \
$ --cli-binary-format=raw-in-base64-out \
$ output.json

An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "tensors used as indices must be long, byte or bool tensors
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 125, in transform
    result = self._transform_fn(self._model, input_data, content_type, accept)
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 215, in _default_transform_fn
    prediction = self._predict_fn(data, model)
  File "/opt/ml/model/code/pytorch-model-reco.py", line 268, in predict_fn
    return torch.argsort(- final_matrix[input_data, :], dim = 1)
IndexError: tensors used as indices must be long, byte or bool tensors

Tags: inpyselfdatamodellinetransform端点
1条回答
网友
1楼 · 发布于 2024-04-24 15:54:57

线索在stacktrace的最后几行:

  File "/opt/ml/model/code/pytorch-model-reco.py", line 268, in predict_fn
    return torch.argsort(- final_matrix[input_data, :], dim = 1)
IndexError: tensors used as indices must be long, byte or bool tensors

在第268行的predict_fn{}中,您试图使用input_data作为final_matrix的索引,但是input_data是错误的类型

我想当输入类型为text/csv时,您的predict_fn应该执行一些类型转换。当输入类型为numpy数据时,此类型转换发生在predict_fn之外。查看一下^{}源代码可能会发现更多信息

相关问题 更多 >