-1

I am trying a regression problem with signals with a lstm. The shape of X_train is (3867,5000,1) and X_test(1998,5000,1). I have this error "ValueError: Input 0 is incompatible with layer lstm_35: expected ndim=3, found ndim=2" in this lstm:

def RNN_LSTM(X_train, label_train, X_val, label_val,
             filters=[100, 100, 100],
             lstm_units=[100, 100, 100],
             act_lstm='tanh',
             act_dense='relu',
             act_end='relu',
             dropout=[0.5, 0.4, 0.3, 0.2, 0.1],
             learn_rate=0.0001):

    batch_length = 100
    num_epochs = 200
    patience_val = 20

    params = X_train.shape[2]
    timesteps = 1

    model = models.Sequential()

    # Capa LSTM
    model.add(layers.LSTM(units=lstm_units[0], activation=act_lstm, input_shape=(timesteps, params)))

    # Capas densas
    model.add(layers.Dense(units=filters[0], activation=act_dense))
    model.add(BatchNormalization())

    model.add(layers.Dense(units=filters[1], activation=act_dense))
    model.add(BatchNormalization())

    # Capa LSTM
    model.add(layers.LSTM(units=lstm_units[1], activation=act_lstm))

    # Capas densas
    model.add(layers.Dense(units=filters[2], activation=act_dense))
    model.add(BatchNormalization())

    # Capa LSTM
    model.add(layers.LSTM(units=lstm_units[2], activation=act_lstm))

    # Capa final
    model.add(layers.Dense(units=1, activation=act_end))

    model.compile(optimizer=optimizers.RMSprop(lr=learn_rate), loss='mse', metrics=['mse'])

    model.summary()

    callbacks_list = [
        keras.callbacks.EarlyStopping(
            monitor='val_mse',
            patience=patience_val,
            restore_best_weights=True,
        )
    ]

    ## reshape

    X_train = np.reshape(X_train, (X_train.shape[0], timesteps, params))
    X_val = np.reshape(X_val, (X_val.shape[0], timesteps, params))

    ## training

    hist = model.fit(
        X_train, label_train,
        epochs=num_epochs,
        batch_size=batch_length,
        callbacks=callbacks_list,
        validation_data=(X_val, label_val))

    acc_val = hist.history['val_mse']
    acc_train = hist.history['mse']

    ## show training

    epochs = range(1, len(acc_train) + 1)
    plt.plot(epochs, acc_train, 'b', label='Training mse')
    plt.plot(epochs, acc_val, 'bo', label='Validation mse')
    plt.title('Training and validation acc') 
    plt.xlabel('Epochs') 
    plt.ylabel('Accuracy') 
    plt.legend()
    plt.show()
            
    predict_train = model.predict(EGM_train)
    predict_val = model.predict(EGM_val)
            
    return predict_train, predict_val

import numpy as np

Please help me, I am not able to solve it through other answers.

I have tried this ValueError: Input 0 is incompatible with layer lstm_13: expected ndim=3, found ndim=4 but it doesn't work

  • Please explain the meaning of your input dimensions and include the output of `model.summary()`. – ATony May 08 '23 at 21:23

1 Answers1

0

Disclaimer: This is not a complete answer!

  • There are several details one would need to fully figure out what you are trying to do, such as what do the dimensions of your X_train and X_test mean.
  • We need the result of model.summary() so that we know what lstm_35 actually refers to.
  • It has been several years since I used Keras for RNN training and it "some peculiarities" that make it quite hard to use for that purpose.

In any event, if I recall correctly, with Keras you have to give it the chopped input sequence for training. This means that your X_train input tensor should have shape (<# training subsequences>, <# timesteps per subsequence>, <# dimensionality of your input sequences>). According to the original shape, you have an 1-d input sequence, which is fine. The subsequences should be 10-20 timesteps long for backpropagation through time. Of course, you don't want that to be hardcoded, so use input_shape=(None, 1).

Note that input_shape=(1, 5000) doesn't make sense because (1) it doesn't make sense to use an RNN for a sequence of length 1 (i.e., individual data points), and (2) your dimensionality would be larger than the number of data points and it is impossible to learn any reasonable model under such conditions. (Except perhaps with very heavy doses of regularization.)

ATony
  • 683
  • 2
  • 12