[英]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.