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

- **ID:** `tensorflow/graph-mode-placeholder-in-eager`
- **领域:** tensorflow
- **类别:** runtime_error
- **错误码:** `EPLE`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.0 | active | — | — |
| 2.1 | active | — | — |
| 2.2 | active | — | — |
| 2.3 | active | — | — |

## 解决方案

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

## 无效尝试

- **Disable eager execution with tf.compat.v1.disable_eager_execution()** — Disabling eager execution may cause other TF2 features to break, and is not recommended for new code. (70% 失败率)
- **Replace placeholder with tf.Variable** — tf.Variable is for model parameters, not for input tensors; it will change the model semantics. (80% 失败率)
