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
相关产品推荐
相关产品推荐

