2

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
1
  • 2
    Are you trying to make a convolution across those 400 features? And each of those 400 features is a single number? In that case, you should indeed reshape your data as (batch_size, 400, 1) (i.e. 400 "steps" and one feature per step). Commented Jul 18, 2017 at 8:54

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have accepted the answer. But, can you explain what do you mean by the two interpretations? Thanks!
It depends on what is interpretation of your input data. If you interpret is a sequence of legth 400 - second approach seems to be more reasonable. If you want to apply Conv1D to output from a Dense layer then first one is more appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.