skyduy/CNN_keras

layer expected input with ndim=4

kaya27 opened this issue · 4 comments

Hello,
I' trying to use CNN for sequence to sequence task, I just started, I'm inspired from your code just I have 3 input that I concatenate in the first conv layer but I got error:
layer expected input with ndim=4 but previous layer has output_shape (None, None, 300)
any suggestions???

@kaya27
Please paste your code here with the error information.

thank you for your reply, I'm a beginner to CNN networks
here is the code:
graph = Graph()
graph.add_input(name='cap', input_shape=(1,), dtype=int)
graph.add_input(name='pref', input_shape=(1,), dtype=int)
graph.add_input(name="pre_word_emb", input_shape=(None, embeddings_dim), dtype="float")
graph.add_node(Embedding(suff_size, suff_dim, init='glorot_uniform'), name="prefix", input="pref")
graph.add_node(Embedding(cap_size, cap_dim, init='glorot_uniform'), name="capitalization", input="cap")

graph.add_node(Convolution2D(400, 5, 5, activation='relu'), name='conv1', inputs=inputs=["prefix","capitalization","pre_word_emb"])
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool1', input='conv1')
graph.add_node(Convolution2D(44, 3, 3, activation='relu'), name='conv2', input='pool1')
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool2', input='conv2')
graph.add_node(Dropout(0.25), name='drop', input='pool2')
graph.add_node(Flatten(), name='flatten', input='drop')
graph.add_node(Dense(640, activation='relu'), name='ip', input='flatten')
graph.add_node(Dropout(0.5), name='drop_out', input='ip')
graph.add_node(Dense(19, activation='softmax'), name='result', input='drop_out')
graph.add_output(name='output', input='result')
graph.compile(optimizer='adadelta',loss={'output': 'categorical_crossentropy'})
print(graph.summary())

the error message is:
graph.add_node(Convolution2D(400, 5, 5, activation='relu'), name='conv1', inputs=["suff_emb","capitalization","pre_word_emb"])
AssertionError: Incompatible shapes: layer expected input with ndim=4 but previous layer has output_shape (None, None, 310)

thanks in advance

Sorry but I haven't used the layer inputs with the "layer list" and even the Embedding layer or integrate them, actually I'm a beginner too😄. But the error tip means your inputs is wrong, I guess that after the shape (1,) and (1,) was embedded, it's "dim" may doesn't work, because a dimension with shape 1 can mean no this dimension. another possibility is that the code grammar may be wrong somewhere.
I remember that the layer's input and output can be checked, but I forget how to do that. The keras's Docs may help😁.

thank you for your reply :) I will check the keras Docs
but as I have understand from the error message the problem is on shapes
I will told you when I will solve it (I hope :p )
have great day!