Bidirectional RNN Implementation pytorch
Hi I am trying to understand bidirectional RNN.
&gt; class RNN(nn.Module):
&gt; 

&gt; 
&gt;   def __init__(self,n_vocab,n_embed,hidden_size,output_size):
&gt; 
&gt;     super().__init__()
&gt; 
&gt;     self.hidden_size = hidden_size
&gt; 
&gt;     self.embedding = nn.Embedding(n_vocab+1,n_embed) ## n_vocab is unique words in dictionary ## n_embed is hyperparameter

&gt;     self.rnn = nn.RNN(n_embed, hidden_size, num_layers = 1, batch_first = True,bidirectional = True) # 
&gt; 
&gt;     self.fc =  nn.Linear(hidden_size,output_size)   
&gt; 
&gt;   def forward(self,x):&gt;
&gt; 
&gt;     x = x # input batch_size * seq_length
&gt; 
&gt;     batch_size = x.size(0) 
&gt; 
&gt;     #print('Batch Size is',batch_size)
&gt; 
&gt;     x = self.embedding(x) # batch-size x seq_length x embedding_dimension
&gt; 
&gt;     x,hidden =self.rnn(x)   #batch-size x seq_length x hidden_size
&gt; 
&gt; 
&gt; 
&gt;     return x,hidden

I am returning both hidden state and output while going through tutorials some says that I need to concatenate hidden state (torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim = 1)) and in some tutorials take output state (x[:,-1,:]) but both of results come difference.
What is the correct way of doing Bidirectional RNN.

Both ways are correct, depending on different conditions. If nn.RNN is bidirectional (as it is in your case), you will need to concatenate the hidden state's outputs. In case, nn.RNN is bidirectional, it will output a hidden state of shape: (num_layers * num_directions, batch, hidden_size). In the context of neural networks, when the RNN is bidirectional, we would need to concatenate the hidden states from two sides (LTR, and RTL). That's why you need to concatenate the hidden states using: torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim = 1) which is basically the same as x[:,-1,:]).
