I am trying to understand a model developed for time series forecasting. It uses a Con1D layer and two LSTM layers and after that, a dense layer. My question is, should it use Flatten()
between the LSTM and the Denser layer? In my mind, the output should just have one value, which has a shape of (None, 1)
, and it can be achieved by using Flatten()
between LSTM and Dense layer. Without the Flatten()
, the output shape would be (None, 30, 1)
. Alternatively, I can remove the return_sequences=True
from the second LSTM layer, which I think has the same effect as the Flatten()
. Which one is a more appropriate way? Do they affect the loss? Here is the model.
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=32, kernel_size=3, strides=1, padding="causal", activation="relu", input_shape=(30 ,1)),
tf.keras.layers.LSTM(32, return_sequences=True),
tf.keras.layers.LSTM(32, return_sequences=True),
# tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1),
])
Here is the model summary without Flatten()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d (Conv1D) (None, 30, 32) 128
_________________________________________________________________
lstm (LSTM) (None, 30, 32) 8320
_________________________________________________________________
lstm_1 (LSTM) (None, 30, 32) 8320
_________________________________________________________________
dense (Dense) (None, 30, 1) 33
=================================================================
Total params: 16,801
Trainable params: 16,801
Non-trainable params: 0
_________________________________________________________________