I am training an LSTM model to predict time series data. I have a problem with the output shape of my model. My model works when the input window size is smaller than or equal to the output window size. However, it does not work when the output window size is bigger than the input window size.
What do I mean by this? Let's say I have these x and y.
# shape = (# of samples, Window Size, # of Features)
x = np.random.random((100, 5, 8))
y = np.random.random((100, 15, 8))
In my dataset, I am predicting the next 15 data points with prior 5 data points i.e. x0 ~ x4 -> x5 ~ x19. The shapes are the same as the numpy arrays above.
My LSTM architecture looks like this.
my_model = Sequential(
[
tf.keras.layers.LSTM(
32,
input_shape=x.shape[-2:], # (N, # of features)
return_sequences=True,
),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.LSTM(18, return_sequences=True),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(x.shape[-1])),
tf.keras.layers.Lambda(lambda x: x[:, -y.shape[-1]:, :]) # Adjust output shape
]
)
The model perfectly works when X's window size is smaller than or equal to Y's window size. But with the provided x and y the model gives (None, 5, 8) as its output and I don't know why that is the case. How do I adjust my LSTM model to work with any type of input and output window size?
Many thanks.
I tried to look up prior questions here but I could not find my solution. I tried to drop some layers but basically I cannot make the output window size bigger than the input window size.
As I understood, you need to tune your model layers to get the final output shape (15, 8) which can be done by adding some new Dense layers to expand output of the second LSTM, then reshape it to (15, 8).
my_model = Sequential(
[
tf.keras.layers.LSTM(
32,
input_shape=x.shape[-2:], # (N, # of features)
return_sequences=True,
),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.LSTM(18, return_sequences=True),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(x.shape[-1])),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(15 * 8),
tf.keras.layers.Reshape((15, 8)),
tf.keras.layers.Dense(8)
]
)
Although the output shape now matches your y array, am not sure if this architecture will be trained efficiently but at least you can later modify it again.