【问题标题】:Keras - Concatenating two inputs of same 1st and 3rd dimension but differing 2nd dimensionKeras - 连接具有相同的第 1 维和第 3 维但不同的第 2 维的两个输入
【发布时间】:2020-10-28 07:05:46
【问题描述】:

我正在创建一个功能性 API keras 模型,以根据两个混合数据输入回归和预测数值。

该网络由两个模型组成,它们的输出旨在连接起来并输入到另一个模型中,该模型将输出最终值以与要预测的值进行比较。

我的(未完成的)代码如下所示:


def categorical_model():
    inputA = Input(shape=(3, 1329, ))
    x = Dense(8, activation='relu')(inputA)
    x = Dense(4, activation='relu')(x)
    return Model(inputs=inputA, outputs=x)

def continuous_model():
    inputB = Input(shape=(1329, 2))
    y = Dense(64, activation="relu")(inputB)
    y = Dense(32, activation="relu")(y)
    y = Dense(4, activation="relu")(y)
    return Model(inputs=inputB, outputs=y)

cat = categorical_model()
con = continuous_model()

catcon_list = [cat.output, con.output]
concatenated = concatenate(catcon_list, axis=0, name = 'concatenate')

concatenated = Dense(2, activation='softmax')(concatenated)

merged =  Model(input=[cat.input,
                    con.input],
             output=concatenated)

merged.summary()

代码生成ValueError,如下所示:

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 3, 4), (None, 1329, 4)]

可以连接这些输入吗?如何将轴更改为二维?

【问题讨论】:

  • 你的数据是什么形状的?
  • A=(3,1329, ), B=(1329, ) @MarcoCerliani
  • 你能把 A 移到 (1329,3,) 中吗?

标签: python tensorflow keras


【解决方案1】:

这应该可行:

concatenated = Concatenate(axis=1)(catcon_list)

merged = Model([cat.input,
                con.input],
               concatenated)

【讨论】:

  • 此代码导致 TypeError: TypeError: ('Keyword argument not understood:', 'input') in line 102 merged = Model(input=[cat.input,
  • 还有另一个错误:您使用“输入”而不是“输入”调用模型 - 请参阅我的编辑
猜你喜欢
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 2017-10-23
  • 1970-01-01
相关资源
最近更新 更多