Skip to content

Fix forward pass of BiRNN #13

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

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -10,10 +10,20 @@ BiRNNImpl::BiRNNImpl(int64_t input_size, int64_t hidden_size, int64_t num_layers
}

torch::Tensor BiRNNImpl::forward(torch::Tensor x) {
auto out = lstm->forward(x)
.output
.slice(1, -1)
.squeeze(1);
out = fc->forward(out);
auto out = lstm->forward(x).output; // out: tensor of shape (batch_size, sequence_length, 2 * hidden_size)

// Concatenate the last hidden state of forward LSTM and first hidden state of backward LSTM
//
// Source: Translated from python code at
// https://github.com/yunjey/pytorch-tutorial/pull/174/commits/8c0897ee93fed8d9b352d33a60c1f931c9be5351
auto out_directions = out.chunk(2, 2);
// Last hidden state of forward direction output
auto out_1 = out_directions[0].slice(1, -1).squeeze(1); // out_1: tensor of shape (batch_size, hidden_size)
// First hidden state of backward direction output
auto out_2 = out_directions[1].slice(1, 0, 1).squeeze(1); // out_2: tensor of shape (batch_size, hidden_size)
auto out_cat = torch::cat({out_1, out_2}, 1); // out_cat: tensor of shape (batch_size, 2 * hidden_size)

out = fc->forward(out_cat);
return torch::log_softmax(out, 1);
}