Bird
Raised Fist0
TensorFlowml~20 mins

Bidirectional RNN in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bidirectional RNN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Bidirectional RNN Output Shape

Consider a bidirectional RNN layer in TensorFlow with units=32 and return_sequences=True. If the input shape is (batch_size, timesteps, features), what will be the output shape of this layer?

A(batch_size, 64)
B(batch_size, 32)
C(batch_size, timesteps, 32)
D(batch_size, timesteps, 64)
Attempts:
2 left
💡 Hint

Remember that a bidirectional RNN concatenates outputs from forward and backward passes.

❓ Predict Output
intermediate
2:00remaining
Output of Bidirectional LSTM Layer

What is the output of the following TensorFlow code snippet?

TensorFlow
import tensorflow as tf
import numpy as np

inputs = tf.constant(np.random.random((1, 5, 10)), dtype=tf.float32)
layer = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(4, return_sequences=False))
output = layer(inputs)
print(output.shape)
A(1, 4)
B(1, 8)
C(5, 8)
D(5, 4)
Attempts:
2 left
💡 Hint

Check the return_sequences parameter and how bidirectional layers concatenate outputs.

❓ Model Choice
advanced
2:00remaining
Choosing the Right Bidirectional RNN for Sequence Tagging

You want to build a model to tag each word in a sentence with its part of speech. Which bidirectional RNN layer configuration is best suited for this task?

ASimple RNN with <code>return_sequences=False</code>
BBidirectional LSTM with <code>return_sequences=False</code>
CBidirectional GRU with <code>return_sequences=True</code>
DBidirectional LSTM with <code>return_sequences=False</code> and dropout=0.5
Attempts:
2 left
💡 Hint

Think about whether you need output for each timestep or just one output per sequence.

❓ Hyperparameter
advanced
2:00remaining
Effect of Increasing Units in Bidirectional RNN

What is the most likely effect of doubling the number of units in a bidirectional RNN layer on model training?

ATraining time increases and model capacity increases, possibly improving accuracy
BTraining time decreases and model capacity decreases, reducing accuracy
CTraining time stays the same but model capacity decreases
DTraining time increases but model capacity stays the same
Attempts:
2 left
💡 Hint

More units mean more parameters to learn.

🔧 Debug
expert
3:00remaining
Identifying the Cause of Shape Mismatch in Bidirectional RNN

Given the following code, what is the cause of the error?

import tensorflow as tf
inputs = tf.keras.Input(shape=(10, 8))
# Bidirectional LSTM with 16 units
x = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16))(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer='adam', loss='mse')

# Trying to train with input shape (32, 10, 8) and target shape (32, 10, 1)
x_train = tf.random.normal((32, 10, 8))
y_train = tf.random.normal((32, 10, 1))
model.fit(x_train, y_train, epochs=1)
AThe LSTM layer should have return_sequences=True to output sequence for each timestep
BThe input shape is incorrect; it should be (10, 1) instead of (10, 8)
CThe Dense layer output units should be 10 instead of 1
DThe model output shape is (32, 1) but target shape is (32, 10, 1), causing mismatch
Attempts:
2 left
💡 Hint

Check if the model output shape matches the target shape.

Practice

(1/5)
1. What is the main advantage of using a Bidirectional RNN compared to a standard RNN?
easy
A. It uses less memory by processing only half the sequence.
B. It trains faster because it skips some time steps.
C. It processes the input sequence in both forward and backward directions to capture more context.
D. It only works with fixed-length sequences.

Solution

  1. Step 1: Understand how standard RNNs process sequences

    Standard RNNs read sequences only in one direction, usually forward, so they only capture past context.
  2. Step 2: Recognize the bidirectional approach

    Bidirectional RNNs read sequences both forward and backward, allowing them to capture information from past and future context.
  3. Final Answer:

    It processes the input sequence in both forward and backward directions to capture more context. -> Option C
  4. Quick Check:

    Bidirectional RNN = forward + backward context [OK]
Hint: Bidirectional means reading sequence both ways [OK]
Common Mistakes:
  • Thinking it reduces memory usage
  • Assuming it skips time steps
  • Believing it only works on fixed-length sequences
2. Which of the following is the correct way to create a Bidirectional LSTM layer in TensorFlow?
easy
A. tf.keras.layers.Bidirectional(units=64, layer=tf.keras.layers.LSTM())
B. tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=64))
C. tf.keras.layers.LSTM(units=64, bidirectional=True)
D. tf.keras.layers.LSTM(tf.keras.layers.Bidirectional(units=64))

Solution

  1. Step 1: Recall the TensorFlow syntax for Bidirectional wrapper

    In TensorFlow, you wrap an RNN layer like LSTM inside tf.keras.layers.Bidirectional.
  2. Step 2: Check each option

    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=64)) correctly wraps LSTM with Bidirectional. Options A, B, and C misuse the syntax or parameters.
  3. Final Answer:

    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=64)) -> Option B
  4. Quick Check:

    Bidirectional wraps RNN layer [OK]
Hint: Wrap RNN layer inside Bidirectional() [OK]
Common Mistakes:
  • Passing bidirectional=True to LSTM directly
  • Putting Bidirectional inside LSTM
  • Incorrect parameter order
3. Consider the following TensorFlow code snippet:
import tensorflow as tf
model = tf.keras.Sequential([
  tf.keras.layers.Bidirectional(tf.keras.layers.SimpleRNN(4, return_sequences=True), input_shape=(5, 10)),
  tf.keras.layers.Dense(1)
])
output = model(tf.random.uniform((1, 5, 10)))
print(output.shape)

What will be the printed output shape?
medium
A. (1, 5, 1)
B. (1, 4, 1)
C. (1, 8, 1)
D. (5, 1)

Solution

  1. Step 1: Analyze the Bidirectional SimpleRNN output shape

    SimpleRNN has 4 units, bidirectional doubles output units to 8. Since return_sequences=True, output shape is (batch, time_steps, 8).
  2. Step 2: Check the Dense layer effect

    Dense(1) applies to last dimension, converting 8 features to 1, so output shape becomes (batch, time_steps, 1) = (1, 5, 1).
  3. Final Answer:

    (1, 5, 1) -> Option A
  4. Quick Check:

    Bidirectional doubles units, Dense changes features [OK]
Hint: Bidirectional doubles units; Dense changes last dim [OK]
Common Mistakes:
  • Ignoring return_sequences=True effect
  • Not doubling units for bidirectional
  • Confusing batch and time dimensions
4. You wrote this code but get an error:
model = tf.keras.Sequential([
  tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32), input_shape=(None, 10)),
  tf.keras.layers.Dense(1)
])

The error says: ValueError: input_shape must be a tuple of integers, not NoneType
What is the likely cause and fix?
medium
A. Add return_sequences=True to the LSTM layer.
B. Remove the Bidirectional wrapper to fix the error.
C. Change LSTM units from 32 to 10.
D. Replace input_shape=(None, 10) with input_shape=(5, 10) or a fixed integer for time steps.

Solution

  1. Step 1: Understand input_shape requirements

    TensorFlow expects input_shape to be a tuple of integers without None for time steps in Sequential models.
  2. Step 2: Fix input_shape

    Replace input_shape=(None, 10) with a fixed integer like (5, 10) to specify sequence length.
  3. Final Answer:

    Replace input_shape=(None, 10) with input_shape=(5, 10) or fixed integer -> Option D
  4. Quick Check:

    input_shape must have fixed integers [OK]
Hint: Use fixed integers in input_shape, not None [OK]
Common Mistakes:
  • Leaving None in input_shape for Sequential
  • Removing Bidirectional unnecessarily
  • Changing units instead of input shape
5. You want to build a sentiment analysis model using a Bidirectional GRU in TensorFlow. The input sequences have variable length. Which approach correctly handles variable-length sequences and avoids processing padded parts?
hard
A. Use tf.keras.layers.Bidirectional(tf.keras.layers.GRU(64)) with masking layer before it to ignore padded tokens.
B. Set return_sequences=False in GRU and ignore padding.
C. Manually trim all sequences to the same length before feeding to the model.
D. Use Bidirectional GRU without masking and rely on Dense layer to fix padding.

Solution

  1. Step 1: Understand variable-length sequence handling

    Variable-length sequences are padded to same length; masking tells RNN to ignore padded parts.
  2. Step 2: Use masking with Bidirectional GRU

    Adding a tf.keras.layers.Masking layer before Bidirectional GRU ensures padded tokens are ignored during processing.
  3. Final Answer:

    Use Bidirectional GRU with masking layer before it -> Option A
  4. Quick Check:

    Masking ignores padding in variable-length sequences [OK]
Hint: Add Masking layer before Bidirectional RNN for padding [OK]
Common Mistakes:
  • Ignoring masking and expecting model to handle padding
  • Trimming sequences manually losing data
  • Relying on Dense layer to fix padding
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard