tensorflow
type_error
ai_generated
true
ValueError: 图断开连接:无法在层 'dense_2' 处获取张量 KerasTensor(type_spec=TensorSpec(shape=(None, 64), dtype=tf.float32, name='input_2')) 的值
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 64), dtype=tf.float32, name='input_2')) at layer 'dense_2'
ID: tensorflow/keras-functional-api-merging-bug
93%修复率
89%置信度
1证据数
2023-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| tensorflow 2.7 | active | — | — | — |
| tensorflow 2.8 | active | — | — | — |
| tensorflow 2.9 | active | — | — | — |
根因分析
Keras 函数式 API 模型的图断开连接,因为中间张量未作为输入传递给下游层,通常是由于两个子模型合并不正确或缺少跳跃连接。
English
A Keras Functional API model has a disconnected graph because an intermediate tensor was not passed as input to a downstream layer, often due to incorrect merging of two sub-models or missing skip connection.
官方文档
https://www.tensorflow.org/guide/keras/functional_api#graph_of_layers解决方案
-
跟踪模型定义,确保在层调用中使用的每个中间张量都作为参数传递。例如,合并两个分支:merged = layers.concatenate([branch1_output, branch2_output]),然后将 merged 传递给下一层。检查所有 KerasTensor 在函数图中是否已连接。
-
使用 model.summary() 和 plot_model(model, show_shapes=True) 可视化图并识别断开的张量,然后修正应使用该张量的层调用。
无效尝试
常见但无效的做法:
-
95% 失败
The error is topological; adding layers does not automatically connect the missing tensor.
-
80% 失败
Sequential API does not support branching or merging, which is often the intended architecture.