0

I am trying to run a CNN-LSTM model on a series of images with dimensions (9219, 7, 7, 12) but I get the following error :

model = Sequential()

model.add(Conv1D(32, 4, activation='relu', padding='same', input_shape=(train_x.shape[1], train_x.shape[2], train_x.shape[3])))
model.add(LSTM(32, return_sequences=True))
model.add(MaxPooling1D(2))
model.add(Conv1D(16, 8, activation="relu", padding='same'))
model.add(LSTM(64, return_sequences=True))
model.add(MaxPooling1D(2))
model.add(Conv1D(16, 8, activation="relu", padding='same'))
model.add(LSTM(128))
model.add(Dense(3, activation='sigmoid'))

And I get this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-117-d8b5365ec22e> in <cell line: 4>()
      2 
      3 model.add(Conv1D(32, 4, activation='relu', padding='same', input_shape=(train_x.shape[1], train_x.shape[2], train_x.shape[3])))
----> 4 model.add(LSTM(32, return_sequences=True))
      5 model.add(MaxPooling1D(2))
      6 model.add(Conv1D(16, 8, activation="relu", padding='same'))

2 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    233             ndim = shape.rank
    234             if ndim != spec.ndim:
--> 235                 raise ValueError(
    236                     f'Input {input_index} of layer "{layer_name}" '
    237                     "is incompatible with the layer: "

ValueError: Input 0 of layer "lstm_32" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 7, 7, 32)
2
  • Try with input_shape=(train_x.shape[1], train_x.shape[2])? Commented Aug 24, 2023 at 5:17
  • No, that would just be leaving a dimension of his data. It should be input_shape=(train_x.shape[1], train_x.shape[2] * train_x.shape[3]) Commented Aug 24, 2023 at 5:24

1 Answer 1

0

In your input shape, you're providing 3D input shape, whereas you're supposed to provide a 2D input shape like this input_shape=(train_x.shape[1], train_x.shape[2] * train_x.shape[3]). That's why the second hidden layer throws a input shape error. Here's how your code should look like:

Corrected model

model = Sequential()
model.add(Conv1D(32, 4, activation='relu', padding='same', input_shape=(train_x.shape[1], train_x.shape[2] * train_x.shape[3])))
model.add(LSTM(32, return_sequences=True))
model.add(MaxPooling1D(2))
model.add(Conv1D(16, 8, activation="relu", padding='same'))
model.add(LSTM(64, return_sequences=True))
model.add(MaxPooling1D(2))
model.add(Conv1D(16, 8, activation="relu", padding='same'))
model.add(LSTM(128))
model.add(Dense(3, activation='sigmoid'))

Model Summary

from keras.optimizers import Adam

# change the optimizer,loss function
# and metrics according to your need

model.compile(optimizer=Adam(learning_rate=0.001),
              loss='binary_crossentropy',
              metrics=['accuracy'])
print(model.summary())

Output:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d (Conv1D)             (None, 10, 32)            672       
                                                                 
 lstm (LSTM)                 (None, 10, 32)            8320      
                                                                 
 max_pooling1d (MaxPooling1  (None, 5, 32)             0         
 D)                                                              
                                                                 
 conv1d_1 (Conv1D)           (None, 5, 16)             4112      
                                                                 
 lstm_1 (LSTM)               (None, 5, 64)             20736     
                                                                 
 max_pooling1d_1 (MaxPoolin  (None, 2, 64)             0         
 g1D)                                                            
                                                                 
 conv1d_2 (Conv1D)           (None, 2, 16)             8208      
                                                                 
 lstm_2 (LSTM)               (None, 128)               74240     
                                                                 
 dense (Dense)               (None, 3)                 387       
                                                                 
=================================================================
Total params: 116675 (455.76 KB)
Trainable params: 116675 (455.76 KB)
Non-trainable params: 0 (0.00 Byte)
Hope it helps!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.