# Problem in input shape Keras-LSTM [closed]

I want to make a predictor using Keras LSTM model. I have a sequence of places visited. The task is to predict the last destination.

I went through different examples but it seems I am not able to shape the input properly.

I am stuck on how to prepare in my program my data to give them to the LSTM model. Here is a minimum code related to my problem.

input_csv ='input.csv'
max_features = 6

#Cafe =0.1, Park =0.2, Shop =0.3, Home=0.4, Movie = 0.5, School=0.6



For example, Person A visited cafe (0.1) -> park(0.2) -> cafe (0.1) -> park(0.2) and finally ended at school(0.6). The desired task is to predict y (end) based on input X (place1,place2,place3,place4).

X = df.iloc[:,1:5].to_numpy()
X_train = X.reshape(6, 1, 4) # X.reshape(samples, timesteps, features)
X_train.shape
#(6, 1, 4)

y = df.iloc[:,-1].to_numpy()
y.shape
#(6,)

#Building model
model = Sequential()


# Train
model.fit(X_train,y)
pred = model.predict(X_train)
predict_classes = np.argmax(pred,axis=1)


ValueError: Error when checking input: expected lstm_4_input to have shape (None, 1) but got array with shape (1, 4)

I would appreciate if someone could help me in clearing my confusion or pointing to some explanations. Thank You.

• Hi, and welcome to datascience.stackexchange.com. Your question is really more about programming, and many similar question on this exact issue appear in Stack Overflow. I suggest searching there (not asking a new question, searching). Good luck! Jul 15, 2020 at 6:29

Few points that i want to mention :

• First of all, Here i think the input u are preparing where I see doubt. The objective is to predict end location. The input shape u prepare is a doubt for me because your sequence_length should be "4" and you have an initial hidden dimension if "1". If I am not wrong then your final share should be (batch_size,sequence_length,hiiden_dimension) = (6,4,1). Because this is what dimension shape LSTM is expecting always.
• Second is change your loss function because u want to do predict from more than one class as mentioned like Cafe =0.1, Park =0.2, Shop =0.3, Home=0.4, Movie = 0.5, School=0.6. Chnage your loss function to categorical_crossentropy in fit() function. Also change activation='sigmoid' to activation='softmax'.

I think these changes u will have to do according to your use case. Please feedback my answer.

• Thank you Gaurav. After applying your suggestions, I got the following error: ValueError: Error when checking target: expected dense_2 to have shape (6,) but got array with shape (1,)
– bdur
Jul 11, 2020 at 11:59
• what u will have to do is that get output at all time time stamp in LSTM. There are functionality available in tensorflow for get output at all time stemp.Each timestamp output u will have loop over it and apply individual dense layer on it. U will have to do manually loop in this case. Here the problem i see is that it only take output of last time stamp that's why it return(1,) shape while it expect (6,) according to dense that's it throw an error. Jul 13, 2020 at 4:13
• Thank you for Gaurav for your guidance. I have posted my initial running code below ( though improvements may be necessary)
– bdur
Jul 14, 2020 at 14:02

A minimum running code by modifying the model building section in the previous code:

print('Build model...')
model = Sequential()