0

I have tried various ways to reshape my data for a LTSM neural network but I continue to the get following error:

ValueError: Error when checking input: expected lstm_29_input to have shape (1, 1) but got array with shape (1, 3841)

My original x_train data is shaped:

array([[-1.86994147, -2.28655553, -2.13504696, ...,  2.38827443,
         1.29554594, 46.        ],
       [-1.99436975, -2.41145158, -1.93463695, ...,  2.57659554,
         1.27274537, 68.        ],
       [-2.19207883, -2.24740434, -1.73407733, ...,  2.66955543,
         1.80862379, 50.        ]

x_train shape: (1125, 3841)

x_valid shape: (375, 3841)

y_train shape: (1125,)

So I've reshaped both my x_train and x_valid data (for predictions) in order to compile properly the model. I've looked at various examples with similar errors and they reshape it as seen below and they usually get their models working but in my case, I am not sure what is going on.

# reshape input to be [samples, time steps, features]
trainX = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
validX = np.reshape(x_valid, (x_valid.shape[0], 1, x_valid.shape[1]))

Model Construction

def cnn_model():
    
    """"
    Creates the model of the CNN.

    :param nr_measures: the number of output nodes needed
    :return: the model of the CNN
    """
    # Initializing the ANN
    model = Sequential()

    # Adding the input layer and the first hidden layer
    model.add(Conv1D(64, 2, activation="relu", input_shape=(x_train.shape[1], 1)))
    model.add(Flatten())
    model.add(BatchNormalization())

    # Adding the second hidden layer
    model.add(Dense(128, activation="relu"))
    model.add(BatchNormalization())

    # Adding the output layer
    model.add(Dense(1, activation = "softplus"))
    model.compile(loss="mse", optimizer="adam", metrics = ['mse'])

    return model

Calling and Training Model

#Call Model
cnn_model= cnn_model(trainX)

#Train Model
history = cnn_model.fit(trainX, y_train, batch_size = 50, epochs = 150, verbose = 0 ,validation_data = (validX, y_valid))

1 Answer 1

1

When you change the shape of trainX using following line

trainX = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))

it becomes 1125, 1, 3841.

Now when you have written following line

model.add(LSTM(units = 50, return_sequences = True, input_shape = (1, trainX.shape[1])))

The model expects input_shape to be (batch_size, 1, 1).

You should have written

model.add(LSTM(units = 50, return_sequences = True, input_shape = (1, trainX.shape[2])))

Now the model will expect input_shape to be (batch_size, 1, 3841) which will correspond to your new tranX.

On a side note, with sequence length/lookback/lag time of 1 and input features of 3841 , using LSTM does not really make sense because LSTM is designed to extract temporal features and here you have no temporal/historical data. Probably Conv1D layer will be more relevant before reshaping the input to (batch_size, 3148, 1) This is just a suggestion.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the clarification. I was curious on a follow up question. I am still new to the field of deep learning and still trying to understand when trying to use a particular neural network for a specific regression case. Would something like the function I put in the question work? I updated the question with the CNN model I am thinking to use.

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.