# Getting ValueError while implementing LSTM in keras

I am getting this error while implementing LSTM in Keras:

""""Error when checking input: expected lstm_16_input to have 3 dimensions, but got array with shape (156060, 1)""""

I have 156060 text phrases all of different lengths, so I was trying to implement LSTM on it without padding. So at first I indexed all these phrases into numerical values and so I got an array of shape (156060,1) and I have labels of shape (156060, 5). Now I implemented my model like this:

model = Sequential()

model.compile(optimizer = "adam", loss = "categorical_crossentropy",
metrics = ["accuracy"])
model.summary()

#encoded_docs.shape = (156060,1) and labels.shape = (156060,5)
model.fit(encoded_docs, labels, epochs = 5)


I know there is some mistake in my input shape but I am unable to find it. If someone can explain me this, it will be a great help.

Your input_shape shouldn't include your batch_size dimension. If you want to perform batch optimization, then you should put input_shape=(1,) and specify in addition batch_size=156060 - but this won't work with LSTM (or any RNN for that matter). If you're fine with mini-batch optimization, then specify a different size for batch_size. You can also set the batch_size parameter inside fit instead - this will be a little bit more flexible.

LSTM's though will expect a time-dimension. Your input should be of shape: (156060,time_steps,num_features) it doesn't make sense that your input is of shape (156060,1) if you are trying to train a LSTM. I would learn a bit more about what a LSTM is and what it does before just trying to plug in numbers willy nilly.

• And what will be the time_steps and num_features in this case. – Akshat Jain Oct 5 '18 at 5:40
• You have 156060 phrases...if you do word level then time_steps would be number of words per phrase and then num_features would be the number of features required to describe a word. For a 10 word phrase encoded as one-hot vectors time_steps would be 10 and then num_features would be the number of words in your dictionary for example. – enumaris Oct 5 '18 at 16:01