EPLE tensorflow runtime_error ai_generated true

RuntimeError: tf.placeholder() 与急切执行不兼容。请改用 tf.keras.Input()。

RuntimeError: tf.placeholder() is not compatible with eager execution. Use tf.keras.Input() instead.

ID: tensorflow/graph-mode-placeholder-in-eager

其他格式: JSON · Markdown 中文 · English
90%修复率
90%置信度
1证据数
2023-05-10首次发现

版本兼容性

版本状态引入弃用备注
2.0 active
2.1 active
2.2 active
2.3 active

根因分析

TensorFlow 2.x 默认以急切模式运行,但代码使用了 tf.placeholder,这是 TF2 中已移除的图模式 API。

English

TensorFlow 2.x runs in eager mode by default, but the code uses tf.placeholder which is a graph-mode API removed in TF2.

generic

官方文档

https://www.tensorflow.org/guide/eager

解决方案

  1. Replace all tf.placeholder calls with tf.keras.Input(shape=..., dtype=...) and rebuild the model using the Keras Functional API.
  2. If you must use graph mode, wrap the code in a @tf.function decorator and use tf.compat.v1.placeholder inside a tf.compat.v1.Graph() context, but this is not future-proof.

无效尝试

常见但无效的做法:

  1. Disable eager execution with tf.compat.v1.disable_eager_execution() 70% 失败

    Disabling eager execution may cause other TF2 features to break, and is not recommended for new code.

  2. Replace placeholder with tf.Variable 80% 失败

    tf.Variable is for model parameters, not for input tensors; it will change the model semantics.