Solving Python Error – Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape

Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape … is an error which occurs when you do not reshape your data properly.

This post is a guide showing you why you are having this error and how you can get rid of it in the most efficient way possible, I will also include some alternative solutions that may help you.

Solving Python Error – Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape

This is an error which occurs when you do not reshape your data properly.

The error should be very similar to the one bellow. Make sure you are not confusinf error messages.

                                                                       #
Traceback (most recent call last): File "first_approach.py", line 80, in model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 1)

ValueError: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (454754, 21)
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully …

Solution 1 : reshape X_train and X_test

Since we want one timestep for each instance we should Set timesteps to 1. timesteps = 1 and we should reshape X_train and X_test, just like in the example bellow.

                                                                       #
import numpy as np
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# X.reshape(samples, timesteps, features)
                                                                       #

The solution should work for most cases, if that is not the case try the solution bellow.

Solution 2 : Fix for people who do not want one timestep for each instance

This solution is for people who do not want one timestep for each instance (aka timesteps != 1)

The solution is not simple but not very complicated, the code bellow is a demonstration of the fix.

                                                                       #
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back+1):
a = dataset[i:(i+look_back), :]
dataX.append(a)
dataY.append(dataset[i + look_back - 1, :])
return np.array(dataX), np.array(dataY)
                                                                       #

Do not forget to import numpy before the code block above. This solution should help you get rid of the issue, I wish you good luck.

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

Summing-up

This is the end of our article, I hope the solution we offered solved your issue, If you like our effort and explanations and want to support our team, please consider donating to our Kofi account above, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/