Tensorflow服务:张量尺寸随服务而变化

时间:2019-04-13 11:37:48

标签: tensorflow tensorflow-serving

问题

我刚开始使用Tensorflow(实际上这是我服务的第一个模型!),如果答案很明显,我深表歉意!

我正在使用image在docker上托管Tensorflow模型。问题是每次我尝试发送数据时服务器都会发送以下错误,以便服务的模型可以进行预测。

W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 4. But input(1) is a vector of size 3

背景信息

  1. 使用saved_model_cli,模型可以显示为
signature_def['predict']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['init_state'] tensor_info:
        dtype: DT_FLOAT
        shape: (2, 2, -1, 136)
        name: policy_estimator/lstm/Placeholder:0
    inputs['state'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 136)
        name: policy_estimator/state:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['action_probs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 260)
        name: policy_estimator/Softmax:0
    outputs['final_state'] tensor_info:
        dtype: DT_FLOAT
        shape: (2, 2, -1, 136)
        name: policy_estimator/packed:0
  Method name is: tensorflow/serving/predict
  1. 在托管之前,该模型可以正常运行而不会出错,因为我能够使用现有数据来训练模型。
  2. 该错误似乎与状态维度相对应。当我将要传递到状态的数据的尺寸从2(即(-1,136))更改为3(使用np.expand_dims)时,错误消息更改为
W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 5. But input(1) is a vector of size 3

当我将尺寸更改为4时,它将更改为

W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 6. But input(1) is a vector of size 3

但是,当我将其设为1维时,错误消息仍为

W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 4. But input(1) is a vector of size 3
  1. 我收到时服务器正常运行
{
 "model_version_status": [
  {
   "version": "1",
   "state": "AVAILABLE",
   "status": {
    "error_code": "OK",
    "error_message": ""
   }
  }
 ]
}

当我运行curl http://model:8501/v1/models/saved_model时,其中http://model:8501/v1/models/saved_model是模型的托管位置。

  1. 我使用python通过
  2. 请求模型
payload = [{"init_state":np.reshape(initial_state, (2,2,-1,136)).tolist(), "state": np.reshape(points, (-1, 136)).tolist()}]
headers = {"content-type": "application/json"}
data = json.dumps({"signature_name": "predict", "instances":payload})
r = requests.post('http://model:8501/v1/models/saved_model:predict', data=data, headers=headers)

this documentation之后。其中r是响应。在这种情况下,此r返回400的响应。

个人结论

我从中得出的唯一结论是,模型在提供服务时可能会发生变化。但是,这只是猜测,因为我受阻了,不确定下一步是否要解决。

我不是专业人士,因此,如果我缺少明显的内容,我深表歉意!请幽默我。如果缺少任何信息,请通知我,因为我会尽力澄清!

1 个答案:

答案 0 :(得分:0)

尝试一下:

import json
.
.
.
payload = {"init_state":np.reshape(initial_state, (2,2,-1,136)).tolist(),"state": np.reshape(points, (-1, 136)).tolist()}
headers = {"content-type": "application/json"}
data = json.dumps({"signature_name": "predict", "inputs":payload})
# add this
data = json.loads(data) # apparently json.dumps returns a json string: '{}' not a json object: {} 

r = requests.post('http://model:8501/v1/models/saved_model:predict', json=data, headers=headers)