keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.64k stars 19.42k forks source link

Character Embedding convolution1d: Input incompatible with layer, expected ndim=3, found ndim=4 #5259

Closed abaheti95 closed 7 years ago

abaheti95 commented 7 years ago

I have a character to integer mapping and a word to integer mapping. Similarly, I have two different character and word embedding layers.

In the model, I want to send one word index and a sequence of its character index at a time to the network. The word can be of variable length, therefore the character sequence has to be variable. For characters, I want to pass the sequence through an embedding layer to get char embedding sequence. Then this embedding sequence will go through a Convolution1D layer followed by GlobalMaxPooling1D layer.

But after the embedding layer when I send the char embedding sequence to Convolution1D layer it shows ValueError: Input 0 is incompatible with layer convolution1d_1: expected ndim=3, found ndim=4

My code snippet:

word_index = Input(shape=(1,))
# List of characters with unknown sequence length but one input at a time
characters = Input(shape=(None, 1))

shared_char_embedding_layer = Embedding(input_dim=(G.char_vocab_size+2), output_dim=G.char_embedding_dimension, mask_zero=True)
char_embeddings = shared_char_embedding_layer(characters)
print K.int_shape(char_embeddings)
print K.ndim(char_embeddings)
# CNN on top of char embeddings
convolution_trichar_embedding = Convolution1D(G.charCNN_dimension, 3, border_mode='valid', input_dim=G.char_embedding_dimension)(char_embeddings)

first_trichar = Flatten(convolution_trichar_embedding[:,0,:])
last_trichar = Flatten(convolution_trichar_embedding[:,-1,:])

max_pool_trichar = GlobalMaxPooling1D(convolution_trichar_embedding)

How to do I take variable length character sequence as input (what should be it's shape)?

Also, is the way of extracting first and last element of the convolution output correct?

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.