keras-team / keras

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

Attempting to merge several convolutional1d layers. input_shape error #4082

Closed amanrai closed 7 years ago

amanrai commented 7 years ago

Hi,

I am attempting to merge several Convolution1D layers to perform a sentiment classification task.

Here is my full network:

ngram_filters = [2, 4, 6, 8]
conv_filters = []

for n_gram in ngram_filters:
    sequential = Sequential()
    sequential.add(Convolution1D(nb_filter=nb_feature_maps,
                            filter_length=n_gram,
                            border_mode='same',
                            input_shape=(maxWords,maxFeatures)))
    sequential.add(Activation('relu'))
    sequential.add(MaxPooling1D(pool_length=sequential.output_shape[1]))
    sequential.add(Flatten())

model = Sequential()

merged = Merge(conv_filters, mode='concat')

model.add(merged)

model.add(Dense(model.output_shape[1]))
model.add(Dropout(0.2))
model.add(Activation('relu'))

model.add(Dense(3))
model.add(Activation('softmax'))

When I run this code, I get the following error:

raise Exception('The first layer in a Sequential model must ' Exception: The first layer in a Sequential model must get an input_shape or batch_input_shape argument.

I checked several other issues here, but everyone suggests reading the documentation which states:

pass an input_shape argument to the first layer. This is a shape tuple (a tuple of integers or None entries, where None indicates that any positive integer may be expected). In input_shape, the batch dimension is not included.

However, anytime I try to add an input_shape to the merged layer, I get another error.

TypeError: init() got an unexpected keyword argument 'input_shape'

This second error actually makes sense because the inputs to this layer should be coming from the convolutional layers being merged.

I'm unsure of how to proceed. Any help would be greatly appreciated.

shashgupta commented 7 years ago

Hi @amanrai

You may get some help from the following answer on stackoverflow http://stackoverflow.com/questions/38656566/input-dimensions-to-a-one-dimensional-convolutional-network-in-keras

You might also have noticed that in the documentation of Convolution1D layer, it specifies the input shape as a 3D tensor with shape: (samples, steps, input_dim), so you might be getting it wrong there.

PS: I faced a similar kind of issue while working with the Convolution2D layer which showed the same exception: The first layer in a Sequential model must get an input_shape or batch_input_shape argument on adding the Flatten layer. The problem in my case was with the Keras configuration file at ~/.keras/keras.json which looks like this:

{
    "image_dim_ordering": "tf",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

I changed the backend to "theano", but didn't change image_dim_ordering to "th" for theano. As soon as I changed it, my code ran perfectly.

Hope I could be of some help. Cheers!