IndexError: tuple index out of range in cnn

Build the CNN model

cnn_model = Sequential()
cnn_model.add(Conv2D(32, kernel_size=(2, 2), activation=‘relu’, input_shape=(1, 28, X_train_cnn.shape[30])))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Dropout(0.25))
cnn_model.add(Flatten())
cnn_model.add(Dense(128, activation=‘relu’))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(1, activation=‘sigmoid’))

error -
2023-04-04 12:17:15.507964: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

IndexError Traceback (most recent call last)
Input In [14], in <cell line: 3>()
1 # Build the CNN model
2 cnn_model = Sequential()
----> 3 cnn_model.add(Conv2D(32, kernel_size=(2, 2), activation=‘relu’, input_shape=(1, 28, X_train_cnn.shape[30])))
4 cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
5 cnn_model.add(Dropout(0.25))

IndexError: tuple index out of range

@Soham_Das,

Welcome to the Tensorflow Forum!

The error message suggests that there is an IndexError because the index 30 is out of range for the shape of X_train_cnn . The issue might be due to using the wrong axis for the input_shape parameter of the Conv2D layer.

Assuming that X_train_cnn is a numpy array containing the input data for the CNN model, with shape (num_samples, num_channels, height, width) , the correct input shape should be (height, width, num_channels) .

Thank you!