How to get cell states for each timestep from nn.LSTM?

I need both hidden and cell state vectors for each timestep. The nn.LSTM function looks like it only returns hidden states for each timestep. Thanks - this would be much appreciated :slight_smile:

2 Likes

maybe you can step through the sequence one element at a time:

lstm = nn.LSTM(3, 3) # Input dim is 3, output dim is 3
inputs = [autograd.Variable(torch.randn((1, 3))) for _ in range(5)] # make a sequence of length 5
hidden = (autograd.Variable(torch.randn(1, 1, 3)), autograd.Variable(torch.randn((1, 1, 3))))
for i in inputs:
# Step through the sequence one element at a time. after each step, hidden contains the hidden state.
out, hidden = lstm(i.view(1, 1, -1), hidden)

you can find the code from pytorch tutorial

This will slow down code a lot though. Is there any easier way? I know in Keras you can just do “return_sequences=True”. Is there something similar in Pytorch?

Hi, recently, I have the same problem. Do you get a better solution?

No, as far as I know there is no way to do this in Pytorch

I have exactly the same issue. I opened a feature request issue on Github: https://github.com/pytorch/pytorch/issues/31423

1 Like