Bidirectional RNN Concatenation
Problem Statement
Combine forward and backward hidden states from a Bidirectional RNN.
Background
In Bidirectional RNNs, we process the sequence in both directions:
- Forward pass: left to right
- Backward pass: right to left
The final output at each timestep is typically the concatenation of both directions' hidden states, doubling the feature dimension.
Your Task
Write a function combine_bidirectional(forward_states, backward_states) that concatenates the forward and backward hidden states along the last dimension.
Input Format
- forward_states: numpy array of shape (batch_size, seq_len, hidden_dim)
- backward_states: numpy array of shape (batch_size, seq_len, hidden_dim)
Output Format
Return a numpy array of shape (batch_size, seq_len, 2 * hidden_dim).
Example
forward shape (2, 3, 4), backward shape (2, 3, 4)
Shape (2, 3, 8)
- Inputs:
forward_statesandbackward_statesare both : a batch of 2 sequences, 3 timesteps each, and a 4-number hidden state per timestep. Forward is all ones, backward all zeros. - Math: for every sequence and timestep , the output vector is the two hidden states placed end to end: .
- Worked: at that gives , the first 4 entries from the forward direction and the last 4 from the backward one. The same happens at all positions.
- Output: shape (2, 3, 8). The batch and time axes are untouched; only the feature axis grows, from to .
- Notice: order matters. Forward features always come first, so features 1 to 4 hold the forward units and features 5 to 8 the backward units. Joining along the time axis instead would give (2, 6, 4).
Constraints
- Both inputs have the same shape
- 1 <= batch_size <= 64
- 1 <= seq_len <= 512
- 1 <= hidden_dim <= 256