Dimension error while combining CNN and LSTM ValueError: Input 0 of layer sequential is incompatible with the layer

I am working on a speech recognition system. I want to use a hybrid of CNN and LSTM but it generates the following error: ValueError: Input 0 of layer sequential_66 is incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: [None, 40, 174, 1] Can anyone please help me?

I think the problem is with inputs variable shape but I don't know how to fix it.

num_rows = 40
num_columns = 174
num_channels = 1
num_labels = yy.shape[1]
filter_size = 2

inputs = np.concatenate((x_train, x_test), axis=0)
inputs = inputs.reshape(inputs.shape[0], num_rows, num_columns,1)
targets = np.concatenate((y_train, y_test), axis=0)
#x_test = x_test.reshape(x_test.shape[0], num_rows, num_columns, num_channels)
num_folds = 10
kfold = KFold(n_splits=num_folds, shuffle=True)

acc_per_fold = []
loss_per_fold = []

fold_no = 1
for train, test in kfold.split(inputs, targets):
  model = Sequential()
  model.add(TimeDistributed(Conv2D(filters=16, kernel_size=3, activation='relu'), input_shape= (None,40, 174, 1)))
  model.add(LSTM(64, return_sequences=False))
  model.add(Dense(64, activation='relu'))
  model.add(Dense(num_labels, activation='softmax'))
  model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

  num_epochs = 80
  num_batch_size = 64

  checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.basic_cnn.hdf5', 
                               verbose=1, save_best_only=True)
  start = datetime.now()
  es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, min_delta=0.0001) 
  mc = ModelCheckpoint('saved_models/weights.best.basic_cnn.hdf5', monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')

  history=model.fit(inputs[train], targets[train], batch_size=num_batch_size, epochs=num_epochs, callbacks=[es,mc], validation_data=(inputs[test], targets[test]), verbose=1)

  duration = datetime.now() - start
  print("Training completed in time: ", duration)
  # Generate generalization metrics
  scores = model.evaluate(inputs[test], targets[test], verbose=0)
  print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')
  acc_per_fold.append(scores[1] * 100)
  fold_no = fold_no + 1

Read more here: https://stackoverflow.com/questions/64961673/dimension-error-while-combining-cnn-and-lstm-valueerror-input-0-of-layer-sequen

Content Attribution

This content was originally published by MonoLiza at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: