0

I want to apply the CNN model by adding CNN followed by fully connected followed by CNN, but I get an error?

#defining model
model=Sequential()

#part 3 CNN followed by fully connected followed by CNN  

#adding convolution layer
model.add(Conv1D(32,3, activation='relu', padding='same', 
                 input_shape = (X_train.shape[1],1)))


#adding fully connected layer
model.add(Flatten())
model.add(Dense(256,activation='relu'))
model.add(Dense(128,activation='relu'))
model.add(Dense(32,activation='relu'))


#adding convolution layer
model.add(Conv1D(64,3, activation='relu', padding='same'))

#adding pooling layer
model.add(MaxPool1D(pool_size=(2,), strides=2, padding='same'))



#adding output layer
model.add(Dense(2,activation='softmax'))

#compiling the model
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

model.summary()

the error : ValueError: Input 0 of layer conv1d_39 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 64)

Fatima
  • 1
  • 1

1 Answers1

0

The convolution1D layer expects 3D input. model.add(Dense(32,activation='relu')) layer's output is 2D, which is being passed as an input to model.add(Conv1D(64,3, activation='relu', padding='same')). You can use the Reshape layer to avoid the error.

model.add(Conv1D(32,3, activation='relu', padding='same', 
                 input_shape = (X_train.shape[1],1)))
model.add(Flatten())
model.add(Dense(256,activation='relu'))
model.add(Dense(128,activation='relu'))
model.add(Dense(32,activation='relu'))

#adding Reshape layer
model.add(Reshape((1,32)))

model.add(Conv1D(64,3, activation='relu', padding='same'))
model.add(MaxPool1D(pool_size=(2,), strides=2, padding='same'))
model.add(Dense(2,activation='softmax'))