v9.1.1
Menu

Bidirectional RNN Concatenation

easyFREERecurrent Neural Networks (RNNs)Deep LearningDeep Learning 1

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

Input
forward shape (2, 3, 4), backward shape (2, 3, 4)
Output
Shape (2, 3, 8)
Reasoning
  • Inputs: forward_states and backward_states are both 2×3×42 \times 3 \times 4: 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 bb and timestep tt, the output vector is the two hidden states placed end to end: yb,t=[ hb,t→ ; hb,t← ]∈R4+4y_{b,t} = [\,h^{\rightarrow}_{b,t} \,;\, h^{\leftarrow}_{b,t}\,] \in \mathbb{R}^{4+4}.
  • Worked: at b=1,t=1b=1, t=1 that gives [1,1,1,1,0,0,0,0][1, 1, 1, 1, 0, 0, 0, 0], the first 4 entries from the forward direction and the last 4 from the backward one. The same happens at all 2×3=62 \times 3 = 6 positions.
  • Output: shape (2, 3, 8). The batch and time axes are untouched; only the feature axis grows, from 44 to 2×4=82 \times 4 = 8.
  • 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

Learn this in

More from Deep Learning 1

solution.py

Test results

Sign in to run your code against the test cases.