Keras LSTM: predict multiple sequences from single input array
I don't know does it make sense to your data or not, however if you reshape output of first layer for each sample from (1,20)
to (20,1)
, then you will get what you want:
model = Sequential()
model.add(LSTM(20, input_shape=(1, 7), return_sequences=True))
model.add(Reshape((20,1)))
model.add(TimeDistributed(Dense(5)))
model.compile(optimizer='adam', loss='mse')
model.summary()
Output:
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_3 (LSTM) (None, 1, 20) 2240
_________________________________________________________________
reshape_2 (Reshape) (None, 20, 1) 0
_________________________________________________________________
time_distributed_2 (TimeDist (None, 20, 5) 10
=================================================================
Total params: 2,250
Trainable params: 2,250
Non-trainable params: 0
UPDATE: As you have said in comments, it is also possible to resolve the issue with RepeatVector(n)
layer, but it is not as easy as Reshape
since it expects input as (num_samples, features)
and it adds another dimension to your data and makes it (num_samples, n, features)
.
Since your input shape is (784, 1, 7)
it can not resolve your issue, unless you first squeeze your data to (784,7)
using numpy.squeeze
and apply RepeatVector(20)
to make it (784,20,7)
. Then everything will be ok.
Furthermore since Dense
layers will be apply on last dimension, using Reshape
should have less trainable parameters than RepeatVector
.
Here I have added the code to use RepeatVector
:
train_set = np.squeeze(train_set) #reshape from (784,1,7) to (784,7)
model = Sequential()
model.add(RepeatVector(20, input_dim=7))
model.add(LSTM(20, return_sequences=True))
model.add(TimeDistributed(Dense(5)))
model.compile(optimizer='adam', loss='mse')
model.summary()
Output:
Model: "sequential_15"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
repeat_vector_8 (RepeatVecto (None, 20, 7) 0
_________________________________________________________________
lstm_10 (LSTM) (None, 20, 20) 2240
_________________________________________________________________
time_distributed_4 (TimeDist (None, 20, 5) 105
=================================================================
Total params: 2,345
Trainable params: 2,345
Non-trainable params: 0
As you can see trainable parameters are more. However you should test both of them on your data and check which one suits more to your data.