Bird
Raised Fist0
NLPml~20 mins

LSTM for text in NLP - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
LSTM Text Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LSTM layer with return_sequences=True
What is the shape of the output tensor after passing input of shape (32, 10, 50) through an LSTM layer with 64 units and return_sequences=True?
NLP
import tensorflow as tf
input_tensor = tf.random.uniform((32, 10, 50))
lstm_layer = tf.keras.layers.LSTM(64, return_sequences=True)
output = lstm_layer(input_tensor)
print(output.shape)
A(10, 64)
B(32, 64)
C(32, 10, 64)
D(32, 10, 50)
Attempts:
2 left
💡 Hint
Remember that return_sequences=True returns output for each time step.
Model Choice
intermediate
2:00remaining
Choosing LSTM for text classification
You want to classify movie reviews as positive or negative using their text. Which model architecture is best suited for this task?
AA simple feedforward neural network with bag-of-words input
BAn LSTM network processing word sequences
CA convolutional neural network on raw text characters
DA linear regression model on word counts
Attempts:
2 left
💡 Hint
Think about models that capture word order and context.
Hyperparameter
advanced
2:00remaining
Effect of increasing LSTM units
What is the most likely effect of increasing the number of units in an LSTM layer from 50 to 200 when training on a small text dataset?
AThe model may overfit and training time will increase
BThe model will ignore word order
CThe model will train faster and generalize better
DThe model will underfit due to fewer parameters
Attempts:
2 left
💡 Hint
More units mean more parameters to learn.
Metrics
advanced
2:00remaining
Evaluating LSTM text model with imbalanced classes
You trained an LSTM model for spam detection on a dataset where 90% of messages are not spam. Which metric is best to evaluate your model's performance?
AAccuracy
BConfusion matrix only
CMean Squared Error
DPrecision and Recall
Attempts:
2 left
💡 Hint
Think about metrics that handle class imbalance well.
🔧 Debug
expert
3:00remaining
Why does this LSTM model not learn?
You trained an LSTM model on text data but the training loss does not decrease. The model code is below. What is the most likely cause?
NLP
import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(input_dim=10000, output_dim=64, input_length=100),
    tf.keras.layers.LSTM(128),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Training data: X_train shape (1000, 100), y_train shape (1000,)
model.fit(X_train, y_train, epochs=10, batch_size=32)
AThe input sequences are not padded to the same length
BThe Embedding layer output_dim is too small
CThe model lacks an activation function in the LSTM layer
DThe loss function is incorrect for binary classification
Attempts:
2 left
💡 Hint
Check the input data shape and preprocessing.

Practice

(1/5)
1. What is the main advantage of using an LSTM model for text data?
easy
A. It converts text directly into images.
B. It removes all punctuation from the text.
C. It remembers the order of words in a sentence.
D. It translates text into multiple languages.

Solution

  1. Step 1: Understand LSTM's role in text

    LSTM models are designed to remember sequences, which means they keep track of word order in sentences.
  2. Step 2: Compare options with LSTM function

    Only It remembers the order of words in a sentence. correctly describes LSTM's ability to remember word order. Other options describe unrelated tasks.
  3. Final Answer:

    It remembers the order of words in a sentence. -> Option C
  4. Quick Check:

    LSTM remembers word order = B [OK]
Hint: LSTM = memory for word order in text [OK]
Common Mistakes:
  • Thinking LSTM translates languages
  • Confusing LSTM with image processing
  • Assuming LSTM removes punctuation
2. Which of the following is the correct way to add an LSTM layer in Keras for text input?
easy
A. model.add(LSTM(128, input_shape=(timesteps, features)))
B. model.add(Dense(128, input_shape=(timesteps, features)))
C. model.add(Conv2D(128, kernel_size=3))
D. model.add(Embedding(128, input_shape=(timesteps, features)))

Solution

  1. Step 1: Identify LSTM layer syntax in Keras

    The LSTM layer is added with LSTM(units, input_shape=(timesteps, features)). model.add(LSTM(128, input_shape=(timesteps, features))) matches this syntax.
  2. Step 2: Check other options for correctness

    model.add(Dense(128, input_shape=(timesteps, features))) is a Dense layer, not LSTM. model.add(Conv2D(128, kernel_size=3)) is a Conv2D layer for images. model.add(Embedding(128, input_shape=(timesteps, features))) is an Embedding layer, not LSTM.
  3. Final Answer:

    model.add(LSTM(128, input_shape=(timesteps, features))) -> Option A
  4. Quick Check:

    LSTM layer syntax = D [OK]
Hint: LSTM layer uses LSTM(), not Dense or Conv2D [OK]
Common Mistakes:
  • Using Dense instead of LSTM for sequence data
  • Confusing Embedding with LSTM layer
  • Applying Conv2D for text input
3. Given this code snippet, what will be the shape of the output from the LSTM layer?
model = Sequential()
model.add(Embedding(input_dim=1000, output_dim=64, input_length=10))
model.add(LSTM(32))
output = model.output_shape
medium
A. (None, 10, 32)
B. (None, 32)
C. (None, 64)
D. (10, 32)

Solution

  1. Step 1: Understand Embedding and LSTM output shapes

    The Embedding layer outputs (batch_size, 10, 64). The LSTM with 32 units returns (batch_size, 32) by default (last output only).
  2. Step 2: Match output shape with options

    (None, 32) matches (None, 32) where None is batch size. Other options are incorrect shapes.
  3. Final Answer:

    (None, 32) -> Option B
  4. Quick Check:

    LSTM output shape = (None, 32) [OK]
Hint: LSTM returns (batch, units) by default, not sequence [OK]
Common Mistakes:
  • Assuming LSTM outputs full sequence by default
  • Confusing embedding output with LSTM output
  • Ignoring batch size dimension
4. Identify the error in this LSTM model code for text classification:
model = Sequential()
model.add(LSTM(64, input_shape=(100,)))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')
medium
A. Optimizer 'adam' is not suitable for LSTM models
B. Dense layer activation should be 'relu' for binary classification
C. Loss function should be 'categorical_crossentropy' for binary output
D. Input shape should be 2D, e.g., (timesteps, features), not (100,)

Solution

  1. Step 1: Check input shape for LSTM layer

    LSTM expects input shape as (timesteps, features). Here, (100,) is 1D, missing feature dimension.
  2. Step 2: Validate other components

    Binary classification uses sigmoid activation and binary_crossentropy loss correctly. Adam optimizer is suitable.
  3. Final Answer:

    Input shape should be 2D, e.g., (timesteps, features), not (100,) -> Option D
  4. Quick Check:

    LSTM input shape must be 2D = A [OK]
Hint: LSTM input shape needs (timesteps, features) [OK]
Common Mistakes:
  • Using 1D input shape for LSTM
  • Changing activation incorrectly for binary tasks
  • Mixing loss functions for binary classification
5. You want to build an LSTM model to classify movie reviews as positive or negative. Which approach best improves model understanding of word meaning before LSTM processing?
hard
A. Add an Embedding layer to convert words into dense vectors before the LSTM.
B. Use a Dense layer directly on raw text input before LSTM.
C. Apply a Conv2D layer to the text input before LSTM.
D. Skip preprocessing and feed raw text strings directly to LSTM.

Solution

  1. Step 1: Understand preprocessing for text in LSTM models

    Embedding layers convert words into meaningful numeric vectors, helping LSTM understand word relationships.
  2. Step 2: Evaluate other options

    Dense layers expect numeric input, not raw text. Conv2D is for images. Feeding raw strings to LSTM causes errors.
  3. Final Answer:

    Add an Embedding layer to convert words into dense vectors before the LSTM. -> Option A
  4. Quick Check:

    Embedding before LSTM = C [OK]
Hint: Use Embedding layer to convert words before LSTM [OK]
Common Mistakes:
  • Feeding raw text directly to LSTM
  • Using Dense or Conv2D layers on raw text
  • Skipping word vector conversion