PLH tensorflow type_error ai_generated true

RuntimeError:tf.placeholder() 与即时执行不兼容。

RuntimeError: tf.placeholder() is not compatible with eager execution.

ID: tensorflow/placeholder-not-supported-eager

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

版本兼容性

版本状态引入弃用备注
tensorflow 2.15 active
tensorflow 2.16 active
tensorflow 2.17 active

根因分析

在默认启用即时执行的 TensorFlow 2.x 中使用 tf.placeholder;占位符仅适用于图模式。

English

Using tf.placeholder in TensorFlow 2.x where eager execution is enabled by default; placeholders are only for graph mode.

generic

官方文档

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

解决方案

  1. Replace `tf.placeholder(tf.float32, shape=[None, 10])` with function arguments in eager mode. For example: `def model_fn(x): return tf.keras.layers.Dense(10)(x)`. Then call `model_fn(input_tensor)` directly.
  2. If graph mode is required, use `tf.function` with input signatures: `@tf.function(input_signature=[tf.TensorSpec(shape=[None, 10], dtype=tf.float32)])`.

无效尝试

常见但无效的做法:

  1. 50% 失败

    Disabling eager execution with `tf.compat.v1.disable_eager_execution()` works but loses TF2 benefits and is deprecated.

  2. 80% 失败

    Replacing placeholder with `tf.Variable` fails because variables hold specific values, not input slots.