I am currently building a CNN that does binary classification, I first do feature extraction using wavelet transform then pass that output to the model. But I'm getting the below error constantly.
train_labels shape: (660,) (labels)
train_data shape: (660, 12) where (num of samples, features)
I've tried:
add a new dimension to the dataset using np.newaxis but it produces cardinality errors
Data cardinality is ambiguous: x sizes: 1 y sizes: 660; i reshape the labels then but that's inefficient since then the model maps to 660 classes instead of 2.
ValueError: in user code: File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function * return step_function(self, iterator) File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step ** outputs = model.train_step(data) File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step y_pred = self(x, training=True) File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility raise ValueError(f'Input {input_index} of layer "{layer_name}" is ' ValueError: Input 0 of layer "sequential_52" is incompatible with the layer: expected shape=(None, 660, 12), found shape=(None, 12)
My code:
model = Sequential()
model.add(Conv1D((16), (1), input_shape= (660, 12) ,name = 'Conv1')) #yes
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv1D(32, (1),name = 'Conv2'))#yes
model.add(Activation('relu'))#yes
model.add(Dense(256, name = 'FC2'))#yes
model.add(Activation('relu'))#yes
model.add(Dropout(0.25))#yes
model.add(Dropout(0.5))#yes
model.add(Dense(1, activation = 'sigmoid'))#yes
sgd = SGD()
model.compile(loss='binary_crossentropy',optimizer=sgd,metrics=['accuracy'])
I reproduced your model and used
model.summary()
to take a closer look at the data shape at the different layers. Are you sure you want to have the shape(None,660,1)
at the output?If you want to do a one output binary classification I suggest that you use a
Flatten
-layer or aMaxPool1D
-layer somewhere before the final layer.