# ValueError: 图断开连接：无法在层 'dense_2' 处获取张量 KerasTensor(type_spec=TensorSpec(shape=(None, 64), dtype=tf.float32, name='input_2')) 的值

- **ID:** `tensorflow/keras-functional-api-merging-bug`
- **领域:** tensorflow
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 93%

## 根因

Keras 函数式 API 模型的图断开连接，因为中间张量未作为输入传递给下游层，通常是由于两个子模型合并不正确或缺少跳跃连接。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| tensorflow 2.7 | active | — | — |
| tensorflow 2.8 | active | — | — |
| tensorflow 2.9 | active | — | — |

## 解决方案

1. ```
   跟踪模型定义，确保在层调用中使用的每个中间张量都作为参数传递。例如，合并两个分支：merged = layers.concatenate([branch1_output, branch2_output])，然后将 merged 传递给下一层。检查所有 KerasTensor 在函数图中是否已连接。
   ```
2. ```
   使用 model.summary() 和 plot_model(model, show_shapes=True) 可视化图并识别断开的张量，然后修正应使用该张量的层调用。
   ```

## 无效尝试

- **** — The error is topological; adding layers does not automatically connect the missing tensor. (95% 失败率)
- **** — Sequential API does not support branching or merging, which is often the intended architecture. (80% 失败率)
