# How to design a many-to-many LSTM?

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))


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

• Can you provide some more code on how you designed the model? Mar 5 '19 at 1:59
• @ShubhamPanchal: I edited my question.
– WDR
Mar 5 '19 at 6:51
• In the last layer change Dense( 20 ) to Dense( 1 ). Mar 5 '19 at 11:18
• 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!
– WDR
Mar 5 '19 at 20:43

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.

model.add(TimeDistributed(Dense(20)))

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.