I am building a CNN with Keras and it is showing an error. The code is the following:
input_shape = (21,) # For your tabular data, add a channel dimension of 1
model = models.Sequential()
# Convolutional layers
model.add(layers.Conv1D(32, 3, activation='relu', input_shape=(21, 1)))
model.add(layers.MaxPooling1D(2))
model.add(layers.Conv1D(64, 3, activation='relu'))
model.add(layers.MaxPooling1D(2))
model.add(layers.Conv1D(64, 3, activation='relu'))
# Flatten the output and add dense layers
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax')) # 10 output units for 10 classes
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train_reshaped, Y_train, epochs=10, validation_data=(X_test, Y_test))
# Predict using the model
y_pred = model.predict(X_test_reshaped)
y_pred_classes = np.argmax(y_pred, axis=1)
The input has 5 million rows with 21 features. The code should make a multi-class classification with 10 different classes. What is wrong with the code?
Thanks for your help.
The shape of your
X_train_reshapedis(32, 1, 21), but you've defined your model withinput_shape=(21, 1). So you need to reshapeX_train_reshapedto the shape(32, 21, 1)to match the input shape of your model.Here's a simple example for your reference: