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

TensorFlow拼接两个模型时出现TensorShape报错:as_list()未定义

问题排查:ValueError: as_list() is not defined on an unknown TensorShape.

错误原因

你在CNN分支的最后一行存在疏漏:

x = Dense(32, activation='relu')

这里仅定义了Dense层对象,但未将上一层的输出张量x传入该层计算,导致x是层实例而非张量。而MLP分支的y是正常输出张量,Concatenate层要求输入必须为张量,不同类型的输入会导致张量形状无法被正确解析,从而触发该报错。

解决方法

给最后一个Dense层传入输入张量,修正代码:

x = Dense(32, activation='relu')(x)

修正后的完整代码

cnn_model = Input(shape=(49, 10))
x = Conv1D(16, kernel_size=3, activation='relu')(cnn_model)
x = MaxPooling1D(pool_size=2)(x)
x = Conv1D(32, kernel_size=3, activation='relu')(x)
x = MaxPooling1D(pool_size=2)(x)
x = Flatten()(x)
x = Dense(32, activation='relu')(x)  # 此处已修正

mlp_model = Input(shape=(4,))
y = Dense(64, activation='relu')(mlp_model)
y = Dense(32, activation='relu')(y)

combined = Concatenate()([x, y])

z = Dense(16, activation='relu')(combined)
output = Dense(1, activation='sigmoid')(z)

model = Model(inputs=[cnn_model, mlp_model], outputs=output)

修改后,x成为CNN分支的输出张量,与y的形状均可被正确识别,拼接操作就能正常执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 18:05:13