3D input in LSTMs for multiple variables

Hello everyone, I am trying to implement Pytorch LSTM to stock market closing prices using historical values.

My input features are Open, High, Low, Close and Volume values for 12 days and my target variable is the closing price for the 13th day. So from my understanding, my input size is 5, output size is 1 and number of time steps is 12.

So, I can construct my LSTM like;

lstm = nn.LSTM(input_size=5, hidden_size=20, batch_first=True)

linear = nn.Linear(in_features=20, out_features=1)

I think my output shape should be (20, 1), one output for each of the sequence in the batch. To achieve that, how do I create my input to the LSTM?

Thanks in advance.

Based on your description of the use case I would assume your output would have the shape [batch_size, 1] and you are working on a regression task.
To do so you could pass the output activation from the last time step of the lstm to the linear layer, which should have the expected shape afterwards.

Hi @ptrblck.

Thanks for the reply. Sorry, I forgot to mention my batch size which is 20. Yeah, I am expecting an final output of [batch_size, 1], one output for each sequence.

However, I seem to be getting an output of shape, [batch_size, seq_len, 1] ~ (20, 5, 1).

This is what I have been doing.

out, _ = lstm(input)
out = linear(out)

Is there something I am doing wrong? Or is [batch_size, seq_len, 1] is the expected output size here?



Made it work. Seems like I didn’t clearly read the second half of your answer. Passed the output from the last time step to the linear layer and got the required shape.

Thanks a lot.