Bidirectional RNNs help a model understand information from both past and future in a sequence. This makes predictions better when context from both sides matters.
Bidirectional RNNs in PyTorch
torch.nn.RNN(input_size, hidden_size, num_layers=1, bidirectional=True)
Set bidirectional=True to make the RNN read input forwards and backwards.
The output size doubles because it combines forward and backward passes.
rnn = torch.nn.RNN(input_size=10, hidden_size=20, bidirectional=True)
rnn = torch.nn.RNN(input_size=5, hidden_size=15, num_layers=2, bidirectional=True)
This code creates a bidirectional RNN and runs a random input through it. The output shape shows the combined forward and backward hidden states for each step. The hidden state contains the last hidden states from both directions.
import torch import torch.nn as nn # Parameters input_size = 3 hidden_size = 4 seq_len = 5 batch_size = 2 # Create bidirectional RNN rnn = nn.RNN(input_size, hidden_size, bidirectional=True, batch_first=True) # Random input: batch_size sequences, each with seq_len steps, each step with input_size features inputs = torch.randn(batch_size, seq_len, input_size) # Forward pass outputs, hidden = rnn(inputs) print('Output shape:', outputs.shape) print('Output:', outputs) print('Hidden shape:', hidden.shape) print('Hidden:', hidden)
The output size is hidden_size * 2 because it combines forward and backward states.
Use batch_first=True to have input shape as (batch, seq_len, features) for easier handling.
Hidden state shape is (num_layers * 2, batch, hidden_size) because of two directions.
Bidirectional RNNs read sequences forwards and backwards to capture full context.
They double the hidden size in output by combining two directions.
Useful when both past and future information matter for predictions.