【问题标题】:Invoking SageMaker Endpoint for PyTorch Model为 PyTorch 模型调用 SageMaker 端点
【发布时间】:2020-08-04 08:41:37
【问题描述】:

我正在尝试从 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

【问题讨论】:

    标签: python amazon-web-services pytorch endpoint amazon-sagemaker


    【解决方案1】:

    线索在堆栈跟踪的最后几行:

      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 行 pytorch-model-reco.py 中的 predict_fn 中,您尝试使用 input_data 作为 final_matrix 的索引,但 input_data 是错误的类型。

    我猜想当输入类型为text/csv 时,您的predict_fn 应该进行某种类型转换。当您的输入类型是 numpy 数据时,这种类型转换发生在 predict_fn 之外。查看sagemaker_inference 源代码可能会发现更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 2018-06-30
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多