Skip to content Skip to sidebar Skip to footer

Using Conv1d “error When Checking Input: Expected Conv1d_input To Have 3 Dimensions, But Got Array With Shape (213412, 36)”

My input is simply a csv file with 237124 rows and 37 columns : The first 36 columns as features The last column is a Binary class label I am trying to train my data on the conv

Solution 1:

There's a couple of problems I notice with your code.

  • Xtrain - Needs to be a 3D tensor. Because anything else, Conv1D cannot process. So if you have 2D data you need to add a new dimension to make it 3D.
  • Your input_shape needs to be changed to reflect that. For example, if you added only a single channel, it should be [n_features, 1].
# Here I'm assuming some dummy data# Xtrain => [213412, 36, 1] (Note that you need Xtrain to be 3D not 2D - So we're adding a channel dimension of 1)
Xtrain = np.expand_dims(np.random.normal(size=(213412, 36)),axis=-1)
# Ytrain => [213412, 10]
Ytrain = np.random.choice([0,1], size=(213412,10))

n_timesteps, n_features, n_outputs =Xtrain.shape[0], Xtrain.shape[1], Ytrain.shape[1]

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=1, 
activation='relu',input_shape=(n_features,1)))

model.add(Conv1D(filters=64, kernel_size=1, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(Xtrain, Ytrain, epochs=10, batch_size=32, verbose=0)

Solution 2:

You need to specifi only how many dimension X has, not how many samples you will pass for the input layer.

 model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_features,)))

This means that the input will be N samples of shape n_features

For the last layer you should change the number of units to how many classes you have instead of how many rows your data has.

Post a Comment for "Using Conv1d “error When Checking Input: Expected Conv1d_input To Have 3 Dimensions, But Got Array With Shape (213412, 36)”"