I have an input array of shape (1000,20, 4) and output(labels) of shape (1000,25,1). But don't know how to use Keras LSTM library to build a sequential model for this!

Can someone help me design a simple LSTM for doing that? (I tried to use RepeatVector() and TimeDistributed(Dense()) in several ways but I get errors like the following:

model = Sequential()
model.add(LSTM(units = un , input_shape = (20, 4), return_sequences = False)) 
model.add(LSTM(un , return_sequences=True))

ValueError: Error when checking target: expected lstm_419 to have shape (25, 20) but got array with shape (25, 1)

  • $\begingroup$ Can you provide some more code on how you designed the model? $\endgroup$ Mar 5, 2019 at 1:59
  • $\begingroup$ @ShubhamPanchal: I edited my question. $\endgroup$ Mar 5, 2019 at 6:51
  • 1
    $\begingroup$ In the last layer change Dense( 20 ) to Dense( 1 ). $\endgroup$ Mar 5, 2019 at 11:18
  • $\begingroup$ It worked! How is it possible? How does it work? Please explain more in an answer and I will accept it. I read about repeat and time distributed layers but still I can't understand how do they exactly work! $\endgroup$ Mar 5, 2019 at 20:43

1 Answer 1


The shape of the labels array was ( 25 , 1 ). The shape of your previous model was ( 25 , 20 ). The dimension 20 was because of this line.


The Dense layer had an output dimension in the form of units= argument as 20. 25 was the length of the sequence.

So, on each of the 25 timesteps, a Dense layer operation was done and each produced a array of shape ( 20 , ). This process happened for 25 timesteps which resulted in a final output of shape ( 25, 20 ).

RepeatVector : It repeats the input vector given to it n number of times.

Timedistributed: Executes the layer operation which is wrapped in it ( in your case it was Dense layer ) for every timestep in the given input sequence.

To know more about the above layers, refer to this answer of mine.

Hence, In the last layer changing Dense( 20 ) to Dense( 1 ) will correct the vector dimension.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.