silent_dev
silent_dev

Reputation: 1616

Adding convolution layer in keras giving errors

I have a dataset which has two classes and has 400 features. Each feature is a floating point number. I am trying to build a basic CNN in keras but I am facing the following error. I have checked other solutions but those solutions ask to reshape the training data into (batch_size, steps, input_dim). I don't think that is a valid solution here.

My code and error message are posted below.

    model = Sequential()
    model.add(Dense(200, input_dim=400, init='glorot_uniform', activation='relu'))
    model.add(Conv1D(100,
                     4,
                     padding='valid',
                     activation='relu',
                     strides=1))
    model.add(GlobalMaxPooling1D())
    model.add(Dense(50))
    model.add(Activation('relu'))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])
    return model

Error Message:

Traceback (most recent call last):
  File "train_CNN.py", line 61, in <module>
    model = create_baseline()
  File "train_CNN.py", line 44, in create_baseline
    strides=1))
  File "/users/prateek.n/.local/lib/python2.7/site-packages/keras/models.py", li                                                                                                                ne 469, in add
    output_tensor = layer(self.outputs[0])
  File "/users/prateek.n/.local/lib/python2.7/site-packages/keras/engine/topolog                                                                                                                y.py", line 552, in __call__
    self.assert_input_compatibility(inputs)
  File "/users/prateek.n/.local/lib/python2.7/site-packages/keras/engine/topolog                                                                                                                y.py", line 451, in assert_input_compatibility
    str(K.ndim(x)))

ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found                                                                                                                 ndim=2

Upvotes: 2

Views: 412

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40516

So Conv1D needs 3 dimensional input of shape (batch_size, timesteps, features). The output from a first Dense layer has shape (batch_size, 200). If you want to interpret these 200 features as 200 timesteps of one feature you could simply:

model = Sequential()
model.add(Dense(200, input_dim=400, init='glorot_uniform', activation='relu'))
model.add(Reshape((200, 1))
model.add(Conv1D(100,
                 4,
                 padding='valid',
                 activation='relu',
                 strides=1))

If you want to interpret the input as time sequence you could also:

model = Sequential()
model.add(Dense(200, input_shape=(400, 1), init='glorot_uniform', activation='relu'))
model.add(Conv1D(100,
                 4,
                 padding='valid',
                 activation='relu',
                 strides=1))

and reshape your input data to have a valid shape. In this case your input will be interpreted as 400 timesteps of one feature and a first Dense layer will transform your data to shape (batch_size, 400, 200) as Dense in Keras > 2.0 is applied independently to each element of a time sequence.

Upvotes: 3

Related Questions