0
$\begingroup$

I have 77 columns, with 4 class labels (already one-hot-encoded) by get_dummies.

x_train = X_train.reshape(-1, 1, 77)
x_test  = X_test.reshape(-1, 1, 77)
y_train = y.reshape(-1, 1, 4)
y_test = y_test.reshape(-1, 1, 4)

batch_size = 32
model = Sequential()
model.add(Convolution1D(64, kernel_size=77, padding="same", activation="relu", input_shape=(77, 1)))
model.add(MaxPooling1D(pool_size=5))
model.add(BatchNormalization())
model.add(Bidirectional(LSTM(64, return_sequences=False))) 
model.add(Reshape((128, 1), input_shape = (128, )))
    
model.add(MaxPooling1D(pool_size=5))
model.add(BatchNormalization())
model.add(Bidirectional(LSTM(128, return_sequences=False))) 
    
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())

This is the model summary :

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, 77, 64)            4992      
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 15, 64)            0         
_________________________________________________________________
batch_normalization (BatchNo (None, 15, 64)            256       
_________________________________________________________________
bidirectional (Bidirectional (None, 128)               66048     
_________________________________________________________________
reshape (Reshape)            (None, 128, 1)            0         
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 25, 1)             0         
_________________________________________________________________
batch_normalization_1 (Batch (None, 25, 1)             4         
_________________________________________________________________
bidirectional_1 (Bidirection (None, 256)               133120    
_________________________________________________________________
dropout (Dropout)            (None, 256)               0         
_________________________________________________________________
dense (Dense)                (None, 5)                 1285      
_________________________________________________________________
activation (Activation)      (None, 5)                 0         
=================================================================
Total params: 205,705
Trainable params: 205,575
Non-trainable params: 130
_________________________________________________________________
None

When I tried to fit the model:

history = model.fit(x_train, y_train,validation_data=(x_test,y_test), epochs=10)

I got this error :

raise ValueError(

    ValueError: Input 0 of layer sequential_7 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape (None, 1, 77)

What is wrong in the input_shape ?

$\endgroup$
1
  • $\begingroup$ Please consider marking one of the answers as correct or, alternatively, let us know how if answers were not useful and why. $\endgroup$
    – noe
    Jun 27, 2021 at 15:23

2 Answers 2

0
$\begingroup$

In the definition of the convolutional layer you defined input shape to be (77, 1), but then your actual input has shape (None, 1, 77). As you can see, the dimensionality of the axes are swapped. They should match.

$\endgroup$
0
$\begingroup$

Your dataset doesn't match with the expected input's or inner's shape. You have to reshape them accordingly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.