【问题标题】:WARNING:tensorflow:11 out of the last 11 calls to triggered tf.function retracing警告:tensorflow:过去 11 次调用中的 11 次触发 tf.function 回溯
【发布时间】:2021-05-22 02:45:47
【问题描述】:

有人知道这个错误的原因吗?

WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
WARNING:tensorflow:11 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001F9D1C05EE0> 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:11 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001F9D5604670> 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.
C:\Users\User\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:973: FutureWarning: 'n_jobs' was deprecated in version 0.23 and will be removed in 0.25.
  warnings.warn("'n_jobs' was deprecated in version 0.23 and will be"

【问题讨论】:

    标签: python tensorflow jupyter-notebook warnings


    【解决方案1】:

    {TLDR} 尝试将 model.predict(x) 替换为 model(x)

    我的解决方案

    我也遇到了警告问题:

    WARNING:tensorflow:11 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001F9D1C05EE0> 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.
    

    我能够通过直接使用 model(x)

    替换 model.predict(x) 来解决它

    我遇到问题的背景信息

    我正在预测时间序列,并在每个新采样时间将模型的最后一层拟合到最新数据。所以我

    1. 生成并拟合基础模型并冻结所有层 + 放置顶层
    2. 适应新数据并在循环内预测

    我尝试使用警告和@TFer2 中建议的签名来实现自定义预测函数。然而,这产生了错误

    RuntimeError: Detected a call to `Model.predict` inside a `tf.function`. `Model.predict is a high-level endpoint that manages its own `tf.function`. Please move the call to `Model.predict` outside of all enclosing `tf.function`s. Note that you can call a `Model` directly on `Tensor`s inside a `tf.function` like: `model(x)`.
    

    有了这个错误,我就可以解决问题了。

    【讨论】:

    • 非常感谢!直接使用 model(x) 替换 model.predict(x) 对我有用
    • 虽然有趣的是,我仍然得到警告,但执行速度提高了十倍
    【解决方案2】:

    如果您调用具有相同参数类型的函数,tensorflow 将重用先前跟踪的图,否则将创建新图。

    函数通过计算cache key from an input's args and kwargs来确定是否重用被跟踪的具体函数:

    • 为tf.Tensor 参数生成的密钥是它的shape 和type(输入签名)
    • 为tf.Variable 参数生成的键是它的id()。
    • 为python 原语生成的密钥是它的value。
    • 为嵌套的dicts, lists, tuples, namedtuples 和attrs 生成的键是flattened tuple。

    回溯可确保 tensorflow 为每组输入生成正确的图表。但是很贵。

    您必须避免过度回溯,否则 tensorflow 通常会发出上述警告。

    有几种方法可以控制跟踪行为:

    • 指定input_signature in tf.function
    • 指定 [None] dimension in tf.TensorSpec 以实现跟踪重用的灵活性
    • Cast python arguments to Tensors 减少回溯

    更多详情可以参考Better performance with tf.function。

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多