【问题标题】:Concatenate three inputs of different dimensions in Keras在 Keras 中连接三个不同维度的输入
【发布时间】:2020-09-18 18:58:48
【问题描述】:

我有两个相同大小的输入,然后应用向量大小为 128 的词嵌入,然后对其进行整形,使两个输入的形状均为 (none,1,128),另一个输入是上下文,其维度为 (none,1,18),我想要连接这三个输入,然后将组合输出馈送到 LSTM 层。但我无法连接输入,因为此错误的尺寸不同:

Concatenate 层需要具有匹配形状的输入,但 concat 轴除外。得到输入形状:[(None, 1, 128), (None, 1, 128), (None, 1, 18)]

   combined= Concatenate(axis=-2)([input_1,input_2, input_3])

shape (none,1,128) 的两个输入是词嵌入,而 shape(none,1,18) 的第三个输入是某个类别变量的一种热编码。

有人知道如何连接吗?任何帮助将不胜感激!

【问题讨论】:

    标签: python tensorflow machine-learning keras keras-layer


    【解决方案1】:

    在最后一个维度上连接它们

    input_1 = Input((1,128))
    input_2 = Input((1,128))
    input_3 = Input((1,18))
    
    combined = Concatenate(axis=-1)([input_1,input_2, input_3])
    

    这会产生一个形状为 (batch_dim, 1, 274) 的组合张量

    【讨论】:

    • 如果我想在axis=-2上连接,那么最好的解决方案是什么?
    • 你不能因为 128 != 18. 所有轴(除了连接一个)必须相等。如果您的范围被提供给 LSTM 层的组合输出 Concatenate(axis=-1) 就是您所需要的
    猜你喜欢
    • 2019-10-02
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多