Skip to content

output of bidirectional LSTM #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hwijeen opened this issue Dec 6, 2018 · 1 comment
Open

output of bidirectional LSTM #149

hwijeen opened this issue Dec 6, 2018 · 1 comment

Comments

@hwijeen
Copy link

hwijeen commented Dec 6, 2018

First of all, thanks for your great tutorial on pytorch! It's a great tip for beginners.
I have a question about the way you use the output of a bidirectional model.

out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size*2)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out

From this code snippet, you took the LAST hidden state of forward and backward LSTM.
I think the image below illustrates what you did with the code. Please refer to this why your code corresponds to the image below. Please note that if we pick the output at the last time step, the reverse RNN will have only seen the last input (x_3 in the picture). It’ll hardly provide any predictive power.(source)
image

Is this the way you intended?
I think a more information-rich way of using the output of bidirectional LSTM is to concatenate the last hidden state of forward LSTM and first hidden state of reverse LSTM, so that both hidden states will have seen the entire input.

Thanks in advance!

@FreyWang
Copy link

you are right, surely the output is the concatenated result of the last hidden state of forward LSTM and first hidden state of reverse LSTM, or BP will be wrong

JiahaoYao added a commit to JiahaoYao/pytorch-tutorial that referenced this issue May 12, 2019
Related to yunjey#149: concatenate the last hidden state of forward LSTM and first hidden state of reverse LSTM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants