【问题标题】:Ensembling multiple model predictions in Keras triggering retracing warning在 Keras 中集成多个模型预测触发回溯警告
【发布时间】:2021-01-11 08:40:45
【问题描述】:

我正在尝试使用四个基于相似架构的 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)

如何摆脱警告?我在这里做错了吗?谢谢!

【问题讨论】:

    标签: python tensorflow machine-learning keras ensemble-learning


    【解决方案1】:

    而不是使用

    preds = m.predict(特征)

    用途:

    preds = m(特征)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-31
        • 1970-01-01
        • 2021-08-25
        • 2021-05-22
        • 2017-08-18
        • 2020-03-07
        相关资源
        最近更新 更多