GCP上带有自定义预测程序的TensorFlow模型

2024-04-27 20:03:35 发布

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

我正在使用AI平台在GCP上部署一个基于LSTM的tensorflow模型。我已经在本地机器上对模型进行了培训和测试。模型如下:

lookback = 16
lstm_input = Input(shape=(1, look_back), name='lstm_input')
x = LSTM(250, name='lstm_0')(lstm_input)
...
x = Dense(1, name='dense_3')(x)

该模型旨在根据之前的16个值一次预测一个值。但实际上,我确实需要16个值作为预测值。我通过将预测值附加到原始值的末尾,在本地实现了这一点,然后再次执行预测,直到得到16个值,如下所示:

def predict(num_prediction, model):
    prediction_list = list(total_data.iloc[-look_back-1:])

    for _ in range(num_prediction):
        x = np.array(prediction_list[-look_back:])
        x = x.reshape((1, 1, look_back)) #(152, 1, 15)
        out = model.predict(x)
        prediction_list.append(out)
    prediction_list = prediction_list[look_back-1:]

    return prediction_list

此外,我还对数据进行了局部标准化,并在训练和预测时将平均值和std值记录在案。我已经将它们导出到一个json文件,并将其与我的模型(.pb)文件一起上传到GCP

所以一切都是在本地进行的。但我无法在GCP上实现这一点,因为我不知道如何使用我自己的参数进行非规范化,并进行类似的递归预测。我正试图利用自定义预测例程来完成这项任务,但一旦将模型部署到那个里作为预测,即使是简单的预测也不起作用。由于我已按照here的指导对输入进行了完美的格式化:

{"instances":[ [[[-1.11942447, -1.22374662, -0.46512613,  2.32171842,
          0.93306964,  0.71291355,  1.74912453,  1.2969071 ,
          0.18740628,  1.20175769,  1.08269237,  0.29160862,
          0.84450949,  2.34316635,  1.11384735,  0.65933734]]] ]}

但它在预测时会抛出以下错误:

{
  "error": "Prediction failed: Error during model execution: <_MultiThreadedRendezvous of RPC that terminated with:\n\tstatus = StatusCode.INVALID_ARGUMENT\n\tdetails = \"transpose expects a vector of size 4. But input(1) is a vector of size 3\n\t [[{{node model_4/lstm_0/transpose}}]]\"\n\tdebug_error_string = \"{\"created\":\"@1587901568.938325490\",\"description\":\"Error received from peer ipv4:127.0.0.1:8081\",\"file\":\"src/core/lib/surface/call.cc\",\"file_line\":1056,\"grpc_message\":\"transpose expects a vector of size 4. But input(1) is a vector of size 3\\n\\t [[{{node model_4/lstm_0/transpose}}]]\",\"grpc_status\":3}\"\n>"
}

因此,这似乎是一个形状问题,我可能不得不增加尺寸,但添加另一对括号只会使情况恶化,出现以下错误:

{
  "error": "Prediction failed: Error during model execution: <_MultiThreadedRendezvous of RPC that terminated with:\n\tstatus = StatusCode.INVALID_ARGUMENT\n\tdetails = \"transpose expects a vector of size 5. But input(1) is a vector of size 3\n\t [[{{node model_4/lstm_0/transpose}}]]\"\n\tdebug_error_string = \"{\"created\":\"@1588177916.982978933\",\"description\":\"Error received from peer ipv4:127.0.0.1:8081\",\"file\":\"src/core/lib/surface/call.cc\",\"file_line\":1056,\"grpc_message\":\"transpose expects a vector of size 5. But input(1) is a vector of size 3\\n\\t [[{{node model_4/lstm_0/transpose}}]]\",\"grpc_status\":3}\"\n>"
} 

更新1:现在只需给一个值加上3个括号,就可以对其进行预测。成功了。但仍在努力进行递归预测和非规范化


Tags: of模型inputsizemodelbackerrorlist