You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras网络中引入辅助输入时遭遇图断开错误的排查与解决

Keras模型构建报错:Graph disconnected问题解决

我有两个Keras模型:一个用于计算轨迹(targeting_model),另一个用于轨迹评估(scoring_model)。评分模型包含完整网络层,目标模型只保留前半部分。两者都需要目标参数来分别完成轨迹计算和评分。因为目标输出只有2个值,我打算把目标数据作为辅助输入传给评分模型,尝试用Concatenate层拼接目标输出和辅助输入时,出现了图断开的报错。

原始代码

# targeting_model
inp=layers.Input(4)

tdense1=layers.Dense(1024, activation='relu')(inp)
tdense2=layers.Dense(1024, activation='relu')(tdense1)
tdense3=layers.Dense(1024, activation='relu')(tdense2)

tout=layers.Dense(2, activation='linear')(tdense3)

# scoring_model
auxinp=layers.Input(4)

sinp=layers.Concatenate()([tout,auxinp])
sdense1=layers.Dense(1024, activation='relu')(sinp)
sdense2=layers.Dense(1024, activation='relu')(sdense1)
sout=layers.Dense(1, activation='linear')(sdense2)

targeting_model =  tf.keras.Model(inputs=inp, outputs=tout, name="targeting_model")
print(targeting_model.summary())
scoring_model =  tf.keras.Model(inputs=inp, outputs=sout, name="scoring_model")

报错信息

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 4), dtype=tf.float32, name='input_2'), name='input_2', description="created by layer 'input_2'") at layer "concatenate". The following previous layers were accessed without issue: ['dense', 'dense_1', 'dense_2', 'dense_3']

问题原因

虽然tout已经和输入inp连通,但评分模型的计算流程里还用到了auxinp这个输入层。你在定义scoring_model时只把inp传给了inputs参数,Keras无法追踪到auxinp的数据流路径,所以会提示图断开。

解决方法

把auxinp也添加到scoring_model的inputs参数中,修改后的代码如下:

scoring_model = tf.keras.Model(inputs=[inp, auxinp], outputs=sout, name="scoring_model")

编辑:已解决,核心就是要将辅助输入auxinp纳入模型的输入声明中。

内容的提问来源于stack exchange,提问作者user2741831

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:54:56