Problem in input shape Keras-LSTM

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
df = pd.read_csv(input_csv)

df.head()
#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()
model.add(LSTM(6, dropout=0.2, recurrent_dropout=0.2, input_shape=(None, 1)))
model.add(Dense(max_features, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# 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.

Topic reshape lstm keras

Category Data Science


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

print('Build model...')
model = Sequential()
#model.add(LSTM(12, input_shape=( X_train.shape[1:])))

model.add(LSTM(6, dropout=0.2, recurrent_dropout=0.2, input_shape=(None, 1)))
model.add(Dense(max_features, activation='softmax'))

# try using different optimizers and different optimizer configs
model.compile(loss='sparse_categorical_crossentropy', #loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.