简体   繁体   中英

Error while concatenating keras layers: A `Concatenate` layer requires inputs with matching shapes

I have this model, which called Hierarchical Attention Networks : 在此处输入图片说明

Which proposed for document classification. I use word2vec embedding for the sentences words, and I want to concatenate another sentence-level embedding at point A (see the figure).

I used it with documents contain 3 sentences; the model summary: 在此处输入图片说明

word_input = Input(shape=(self.max_senten_len,), dtype='float32')
word_sequences = self.get_embedding_layer()(word_input)
word_lstm = Bidirectional(self.hyperparameters['rnn'](self.hyperparameters['rnn_units'], return_sequences=True, kernel_regularizer=kernel_regularizer))(word_sequences)
word_dense = TimeDistributed(Dense(self.hyperparameters['dense_units'], kernel_regularizer=kernel_regularizer))(word_lstm)
word_att = AttentionWithContext()(word_dense)
wordEncoder = Model(word_input, word_att)
sent_input = Input(shape=(self.max_senten_num, self.max_senten_len), dtype='float32')
sent_encoder = TimeDistributed(wordEncoder)(sent_input)

""" I added these following 2 lines. The dimension of self.training_features is (number of training rows, 3, 512). 512 is the dimension of the sentence-level embedding.  """
USE = Input(shape=(self.training_features.shape[1], self.training_features.shape[2]), name='USE_branch')
merge = concatenate([sent_encoder, USE], axis=1)

sent_lstm = Bidirectional(self.hyperparameters['rnn'](self.hyperparameters['rnn_units'], return_sequences=True, kernel_regularizer=kernel_regularizer))(merge)
sent_dense = TimeDistributed(Dense(self.hyperparameters['dense_units'], kernel_regularizer=kernel_regularizer))(sent_lstm)
sent_att = Dropout(dropout_regularizer)(AttentionWithContext()(sent_dense))
preds = Dense(len(self.labelencoder.classes_))(sent_att)
self.model = Model(sent_input, preds)

When I compile the above code, I get the following error:

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

I specified the concatenation axis=1, to concatenate on (3) the number of sentences, but I don't know why I'm still getting the error.

This is because the shape don't match if you specify that axis. This will work if you do this instead:

merge = concatenate([sent_encoder, USE], axis=-1)

Now there is no conflict of shapes on the remaining axes

The error was due to two lines:

merge = concatenate([sent_encoder, USE], axis=1)
# should be:
merge = concatenate([sent_encoder, USE], axis=2) # or -1 as @mlRocks suggested

and the line:

self.model = Model(sent_input, preds)
# should be:
self.model = Model([sent_input, USE], preds) # to define both inputs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM