Search code examples
pythontensorflowkerasdeep-learninglstm

LSTM Inputs Confusion


I've been trying to understand LSTM inputs for a while now and I think I understand but I keep getting confused on how to implement them.

This is what I think, please correct me if I am wrong.

When specifying an LSTM, you specify the number of cells and the input shape (I've been having issues with the input shape). The Number of cells specifies how many cells should look at the data given and does not affect the required input shape. The input shape (When Stateful) goes by batch size, Timesteps in a batch, and features in a time step. A stateful LSTM retains it's Internal States until reset. Is this right?

If so I'm having so much confusion trying to specify the input shape for my network. This is because I'm trying to upgrade a current network and I cant figure out how and where to specify the input shape without an error.

The way I'm trying to upgrade it is initially I have a CNN going to a dense layer. I'm trying to change it such that it adds an LSTM that takes the CNN's flattened 1D output as one batch and one time step with features dependent on the size of the CNN's Output. Then concatenates its output with the CNN's output (The LSTM's Input) then feeds into the dense layer. Thus it now behaves like LSTM with a skip connection. The issue that I cant seem to understand is when and how to specify the LSTM layer's Input_shape as it has NO INPUT_SHAPE parameter for the functional API? Or maybe I'm just super confused, Everyone uses different API's going over different examples and it gets super confusing what is and isn't specified and how.

This is the Upgrade

Thank you, even if you just help with one of the two parts.

TLDR:

  1. Do I understand LSTM Parameters correctly?
  2. How and when do I specify LSTM Input_shapes if at all?

Solution

  • LSTM units argument means dimensions of LSTM matrices and output shape.

    With Functional API you can specify input shape for the very first layer only. If your LSTM layer follows CNN - then its input shape is determined automatically as CNN output.