代码之家  ›  专栏  ›  技术社区  ›  Baron Yugovich

keras concatenate失败,形状不兼容

  •  0
  • Baron Yugovich  · 技术社区  · 8 年前

    这是我的密码

    x_and_h = Concatenate()([x_t_emb, h_t])
    

    这就是错误:

    ValueError: `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 16), (1, 64)]
    

    我一直在尝试各种重塑操作以使形状兼容,但这两次尝试都失败了:

    h_t = tf.reshape(h_t, shape=tf.TensorShape([None, h_t.get_shape()[-1].value])) 
    x_t = tf.reshape(x_t, shape=tf.TensorShape([1, x_t.get_shape()[-1].value]))
    

    有人能告诉我发生了什么事,以及如何解决这个问题吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Alex    8 年前

    你能提供更多关于你定义的层的细节吗?

    无论如何,对于连接层,在同一轴上的两个张量之间必须至少有一个维度匹配。

    简单来说,你可以很容易地看到“无”与1不匹配,16与64不匹配。

    除此之外,尝试像你所做的那样重塑是错误的。最初h_t为形状(1,64),并具有:

    h_t = tf.reshape(h_t, shape=tf.TensorShape([None, h_t.get_shape()[-1].value]))
    

    您试图将它重新整形为(none,64)张量,这在概念上是错误的,因为none意味着它可以是任何整数,通常表示您的批大小。

    尝试执行以下操作时出现相同问题:

    x_t = tf.reshape(x_t, shape=tf.TensorShape([1, x_t.get_shape()[-1].value]))
    

    当你试图把它重塑成一个固定的形状时,你的初始形状是(无,16):(1,16)。

    我认为你应该试着让你的h-u层返回一个形状为(none,1,16)或(none,16)的张量,这样你就可以先展平,然后在第一种情况下连接,在第二种情况下只使用concatenate。