'How to Solve 1 Dimensional CNN Input Shape Error?

I am trying to train a 1D CNN. My input is a numpy array which has 476 rows and 4 columns. I couldn't figure out how to set input shape. I also tried to reshape input to (476, 4, 1) but still got error. Train_labels's shape is (476, ). O is train data and 0[0].shape is (4,) Here is the code, error and data below.

ValueError: Input 0 of layer "sequential_17" is incompatible with the layer: expected shape=(None, 476, 4), found shape=(1, 4, 1)

Traceback (most recent call last)
<ipython-input-363-440de758fd68> in <module>()
     10 
     11 
---> 12 model.fit(o, train_labels, epochs=5, batch_size=1)
     13 print(model.evaluate(o, train_labels))

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise
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 '
model=Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(476,4)))
model.add(Conv1D(filters=16, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))

Here is the model structure below.

Model: "sequential_34"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_56 (Conv1D)          (None, 474, 32)           416       
                                                                 
 conv1d_57 (Conv1D)          (None, 472, 16)           1552      
                                                                 
 dropout_19 (Dropout)        (None, 472, 16)           0         
                                                                 
 max_pooling1d_19 (MaxPoolin  (None, 236, 16)          0         
 g1D)                                                            
                                                                 
 flatten_19 (Flatten)        (None, 3776)              0         
                                                                 
 dense_40 (Dense)            (None, 50)                188850    
                                                                 
 dense_41 (Dense)            (None, 2)                 102       
                                                                 
Total params: 190,920
Trainable params: 190,920
Non-trainable params: 0

My data is a numpy array which has 476 rows and 4 columns. It's shape is (476,4).

[[0.35603836 0.6439616  0.49762452 0.5023755 ]
 [0.12395032 0.87604964 0.49762452 0.5023755 ]
 [0.5605615  0.43943852 0.49762452 0.5023755 ]
 ...
 [0.6250699  0.37493005 0.48114303 0.51885694]
 [0.6650569  0.33494312 0.48114303 0.51885694]
 [0.53505033 0.46494964 0.48114303 0.51885694]]


Solution 1:[1]

I solved the problem myself and wanted to provide an answer for anyone having the same issue.

Here is the steps I applied:

  • I used sparse categorical crossentropy.
  • I changed input shape to (number of attributes, 1)
  • I changed CNN kernel size from 3 to 2
model=Sequential()
model.add(Conv1D(filters=8, kernel_size=2, activation='relu', input_shape=(4,1)))
model.add(Conv1D(filters=4, kernel_size=2, activation='relu'))
model.add(Dropout(0.3))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(20, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 imdatyaa