1D CNN using tensorflow keras classification problems


I have been trying to use 1D CNN to do simple classification problems. Such as creating a tabular data in csv and input it into python to do some simple classifications. First 31 columns of the data are the features and the last column is the condition. I have been doing classification with other ML method such as Lightgbm and Randomforest. I want to try using 1D CNN and see whether the accuracy can be improved.

X = raw_data[feature_names]

P = predict_data_raw[feature_names]
P1 = predict_data_raw[feature_names1]

y = raw_data['Conditions']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=22, test_size=0.1)
 model = Sequential()
 model.add(Conv1D(filters=32, kernel_size=3, activation='relu'))
 model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
 model.add(Dense(64, activation='relu'))
 model.add(Dense(32, activation='relu'))
 model.add(Dense(2, activation='softmax'))
 model.compile(loss='loss_function', optimizer='adam', metrics=['accuracy'])

I want to output the prediction results and the prediction probabilities of the conditions. However, the training stuck at some points and show this error:

ValueError: Exception encountered when calling layer "sequential_26" (type Sequential).

Input 0 of layer "conv1d_33" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 31)

Call arguments received by layer "sequential_26" (type Sequential):
  • inputs=tf.Tensor(shape=(None, 31), dtype=float64)
  • training=True
  • mask=None


Conv1D expects a 3-dimensional input, while your input is just 2-dimensional. You can reshape your data or add a Reshape layer:

model = Sequential()
model.add(Reshape((31, 1))

You might need to add an input_shape

Answered By – AndrzejO

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published