简体   繁体   English

ValueError: Shape must be rank 3 but is rank 2.一个`Concatenate`层需要具有匹配形状的输入,除了concat

[英]ValueError: Shape must be rank 3 but is rank 2. A `Concatenate` layer requires inputs with matching shapes except for the concat

I am trying to use Tensorflow Functional API to define a multi input neural network.我正在尝试使用 Tensorflow Functional API来定义多输入神经网络。

This is my code:这是我的代码:

from keras_self_attention import SeqSelfAttention
from tensorflow import keras
Input1 = Input(shape=(120, ),name="Input1")
Input2 = Input(shape=(10, ),name="Input2")
embedding_layer = Embedding(30,5,  input_length=120,)(Input1) 
lstm_layer = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=512))(embedding_layer)                  
attention=SeqSelfAttention(attention_activation='sigmoid')(lstm_layer) 
merge = concatenate([attention, Input2])                                  

However, I get the following error:但是,我收到以下错误:

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

If I change shape of Input2 to (None,10, ), then I get this error:如果我将 Input2 的形状更改为 (None,10, ),则会出现此错误:

ValueError: Shape must be rank 3 but is rank 2 for '{{node model/concatenate/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](model/dense/BiasAdd, model/Cast_1, model/concatenate/concat/axis)' with input shapes: [?,?,1024], [?,10], [].

and if I change shape of Input2 to (1,10, ), then I get this error:如果我将 Input2 的形状更改为 (1,10, ),则会出现此错误:

ValueError: Shape must be rank 3 but is rank 2 for '{{node model/concatenate/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](model/dense/BiasAdd, model/Cast_1, model/concatenate/concat/axis)' with input shapes: [?,?,1024], [?,10], [].

How can I reshape output of attention layer from (None, None, 1024) to something which I can concatenate with (None, 10)?如何将注意力层的输出从 (None, None, 1024) 重塑为可以与 (None, 10) 连接的内容?

The inputs for concatenate layer will not have matching dimensions.连接层的输入将没有匹配的维度。 You can add a reshape layer in front of the concatenate layer to alleviate this issue.您可以在连接层前添加一个重塑层来缓解这个问题。

[(None, None, 1024), (None, 10)].

Here one is three and one is two.这里一是三,一是二。 Reshape the first input to [None,1024] or reshape the second input to [None, 1 , 10] whichever suits your need.将第一个输入整形为[None,1024]或将第二个输入整形为[None, 1 , 10]适合您的需要。

reshape_layer = tf.keras.layers.Reshape((1024,))(attention)
merge = concatenate([reshapelayer, Input2])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:“连接”层要求输入具有匹配形状,但连接轴除外 - ValueError: `Concatenate` layer requires inputs with matching shapes except for the concat axis ValueError:“连接”层要求输入的形状与连接轴一致,但连接轴除外 - ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis 如何解决“ValueError:`Concatenate`层需要具有匹配形状的输入(连接轴除外)”? - How to resolve "ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis"? 如何解决“ValueError: A `Concatenate` 层要求输入具有匹配的形状(连接轴除外)”? - How to resolve “ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis”? `Concatenate` 层需要具有匹配形状的输入,除了 concat 轴 Keras - A `Concatenate` layer requires inputs with matching shapes except for the concat axis Keras ValueError:“Concatenate”层要求输入具有匹配的形状(连接轴除外)。 得到输入形状:[(None, 523, 523, 32) 等 - ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 523, 523, 32), etc ValueError:“连接”层需要具有匹配形状的输入,连接轴除外。 得到输入形状:[(None, 36, 36, 128) 等 - ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 36, 36, 128), etc ValueError:“Concatenate”层需要具有匹配形状的输入 - ValueError: A `Concatenate` layer requires inputs with matching shapes “‘连接’层需要具有匹配形状的输入,连接轴除外。” 如何解决这个问题? - "A `Concatenate` layer requires inputs with matching shapes except for the concat axis." how to solve this issuse? Keras Unet:“连接”层需要具有匹配形状的输入,连接轴除外 错误关闭 1 - Keras Unet: A `Concatenate` layer requires inputs with matching shapes except for the concat axis Error off by 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM