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

- **ID:** `tensorflow/placeholder-not-supported-eager`
- **领域:** tensorflow
- **类别:** type_error
- **错误码:** `PLH`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| tensorflow 2.15 | active | — | — |
| tensorflow 2.16 | active | — | — |
| tensorflow 2.17 | active | — | — |

## 解决方案

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)])`.
   ```

## 无效尝试

- **** — Disabling eager execution with `tf.compat.v1.disable_eager_execution()` works but loses TF2 benefits and is deprecated. (50% 失败率)
- **** — Replacing placeholder with `tf.Variable` fails because variables hold specific values, not input slots. (80% 失败率)
