1

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(RepeatVector(25))  
model.add(LSTM(un , return_sequences=True))
model.add(TimeDistributed(Dense(20))) 

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

user3486308
  • 1,270
  • 5
  • 18
  • 28

1 Answers1

2

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.

Shubham Panchal
  • 2,190
  • 9
  • 21