在 Keras 中集成多个模型预测触发回溯警告

Ensembling multiple model predictions in Keras triggering retracing warning

我正在尝试使用构建在类似架构上的四个 TensorFlow 模型生成 4 种不同类型的预测。当我使用相同的功能调用 model.predict() 函数四次时,我收到以下警告:

WARNING:tensorflow:5 out of the last 9 calls to <function Model.make_predict_function.<locals>.predict_function at 0x2b18970e88c0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
WARNING:tensorflow:6 out of the last 10 calls to <function Model.make_predict_function.<locals>.predict_function at 0x2b189734ab90> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
WARNING:tensorflow:7 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x2b189784be60> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
WARNING:tensorflow:7 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x2b1897e1c440> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for  more details.

这是我加载模型并做出预测时所做的。代码基于本教程:https://machinelearningmastery.com/stacking-ensemble-for-deep-learning-neural-networks/

model_1=load_model('./Models/model_1.hdf5')
model_2=load_model('./Models/model_2.hdf5')
model_3=load_model('./Models/model_3.hdf5')
model_4=load_model('./Models/model_4.hdf5')
models=[model_1,model_2,model_3,model_4]

# get prediction outputs for the four models
def get_predictions(features,models):
    return np.array([i.predict(features) for i in models])

preds=get_predictions(features,models)

如何消除警告?我在这里做错了什么吗?谢谢!

去年没有人回答,但我刚刚 运行 遇到了同样的问题。解决方案是在单个 Keras 模型中定义单独的神经网络。训练和计算仍然在每个单独的网络中独立发生,您仍然可以以相同的方式获取各个输出的平均值,但回溯警告消失了 - 更重要的是 - 性能要好得多。

而不是使用

preds = m.predict(features)

使用:

preds = m(features)