sachinruk
sachinruk

Reputation: 9869

merging inputs with embeddings and dense layer

I have two inputs, x_a and x_b where x_a is a categorical variable (hence the embedding) and x_b which is a usual feature matrix. Basically I want to multiply x_b by a weights matrix W_b which is a 10x64 matrix so that I end up with a 64 dimensional output.

from keras.models import Sequential
from keras.layers import Dense, Activation, Embedding, Merge

encoder_cc = Sequential()

# Input layer for countries(x_a)
encoder_cc.add(Embedding(cc_idx.max(),64))
# Input layer for triggers(x_b)
encoder_trigger = Sequential()
# This should effectively be <W_b>
encoder_trigger.add(Dense(64, input_dim=10, init='uniform'))

model = Sequential()
model.add(Merge([encoder_cc, encoder_trigger], mode='concat'))

Then I want to combine (merge) these two before I do the usual Neural net stuff. Except that I get the error:

Exception: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 1, 64), (None, 64)]

Any thoughts on how I can resolve this?

Upvotes: 3

Views: 1721

Answers (1)

Nassim Ben
Nassim Ben

Reputation: 11543

The Embedding layer is made to be used for sequences, it outputs then a tensor with 3D of shape (None, sequence_length, embedding_size) which is in your case (None, 1, 64) because your input is of length 1.

The way to correct this is either to add a Flatten() layer to your encoder_cc model as Stephan suggests, or add a Reshape((64,)) after the embedding layer. This will force the output shape to be (None, 64) and match the shape of the other model.

I hope this helps.

Upvotes: 3

Related Questions