# pytorch lstm output not match hidden state

**URL:** <https://discuss.pytorch.org/t/pytorch-lstm-output-not-match-hidden-state/114814>\
**Category:** nlp\
**Created:** [March 15, 2021, 6:24am UTC](https://discuss.pytorch.org/t/pytorch-lstm-output-not-match-hidden-state/114814 "2021-03-15T06:24:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![naturesphere](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/naturesphere/32/35815_2.png) [@naturesphere](https://discuss.pytorch.org/u/naturesphere)\
**Post date:** [March 15, 2021, 6:24am UTC](https://discuss.pytorch.org/t/pytorch-lstm-output-not-match-hidden-state/114814/1 "2021-03-15T06:24:04Z")

</div>

Here is the code:

```auto
import torch
from torch.nn.utils.rnn import pack_sequence, pad_packed_sequence
rnn = torch.nn.LSTM(2, 3, 1, bidirectional=True)
x1= torch.randn(1,2)
x2=torch.randn(2,2)
x=pack_sequence([x1,x2], enforce_sorted=False)
y, (hn, cn) = rnn(x)
y,lens=pad_packed_sequence(y)

```

print the hidden state result:

```auto
print(torch.cat([hn[-2], hn[-1]], dim=1))

```

get result:

```auto
tensor([[-0.0265, -0.1149, -0.0466, 0.1080, 0.0901, 0.0865],
        [0.0736, -0.2503, -0.0634, 0.0801, 0.1401, -0.0032]],
       grad_fn=<CatBackward>)

```

print the output:

```auto
for i,j in enumerate(lens):
    print(y[i][j-1])

```

get result:

```auto
tensor([-0.0265, -0.1149, -0.0466, 0.1080, 0.0901, 0.0865],
       grad_fn=<SelectBackward>)
tensor([0.0736, -0.2503, -0.0634, 0.0932, 0.0962, -0.0915],
       grad_fn=<SelectBackward>)

```

the second tensor is not the same with the hidden state!

Why?

---

<div class="post-metadata">

**Author:** ![naturesphere](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/naturesphere/32/35815_2.png) [@naturesphere](https://discuss.pytorch.org/u/naturesphere)\
**Post date:** [March 15, 2021, 8:59am UTC](https://discuss.pytorch.org/t/pytorch-lstm-output-not-match-hidden-state/114814/2 "2021-03-15T08:59:08Z")

</div>

Which one should be used as result?

---

<div class="post-metadata">

**Author:** ![vdw](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vdw/32/10074_2.png) [@vdw](https://discuss.pytorch.org/u/vdw)\
**Post date:** [March 15, 2021, 9:39am UTC](https://discuss.pytorch.org/t/pytorch-lstm-output-not-match-hidden-state/114814/3 "2021-03-15T09:39:02Z")

</div>

I think I had the [same question](https://discuss.pytorch.org/t/rnn-output-vs-hidden-state-dont-match-up-my-misunderstanding/43280) where I also posted my answer. Maybe you can check if it indeed matches your concerns.

---

<div class="post-metadata">

**Author:** ![naturesphere](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/naturesphere/32/35815_2.png) [@naturesphere](https://discuss.pytorch.org/u/naturesphere)\
**Post date:** [March 17, 2021, 1:51am UTC](https://discuss.pytorch.org/t/pytorch-lstm-output-not-match-hidden-state/114814/4 "2021-03-17T01:51:04Z")

</div>

I still can not understand. Why the first tensor is match and the second is not.
