I have a data of shape (4,34):
print(X_train)
array([[[0., 1., 0., ..., 0., 1., 0.],
[0., 0., 1., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 1., 0., 1.]],
............
[[0., 1., 0., ..., 0., 1., 0.],
[0., 0., 1., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 1., 0., 1.]])
and trying to build a keras model as following
model = Sequential()
model.add(Conv1D(filters=num_filters, kernel_size=motif_len, activation='relu', input_shape=(34, 4)))
model.add(MaxPooling1D(pool_size=4, strides=1, data_format='channels_last'))
model.add(Flatten())
model.add(Dense(hidden_units, activation='relu'))
model.add(Dense(6, activation='softmax')) # Output layer for multi-class classification
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))
sadly, I'm struggling with incompatibility error, while seemingly I am doing exactly the same procedure as at the following example (which does work):
ValueError: Input 0 of layer "sequential_19" is incompatible with the layer:
expected shape=(None, 34, 4), found shape=(32, 4, 34)`
model summary:
Model: "sequential_19"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_19 (Conv1D) (None, 27, 512) 16896
max_pooling1d_14 (MaxPoolin (None, 24, 512) 0
g1D)
flatten_12 (Flatten) (None, 12288) 0
dense_24 (Dense) (None, 512) 6291968
dense_25 (Dense) (None, 6) 3078
i was trying to swap the dimensions, which made the code to brake earlier
The problem is that the shape of data is (*, 4, 34)
while the model accepts data of shape (None, 34, 4)
. You should simply transpose the input like:
X_train = X_train.transpose(0, 2, 1)