Bird
Raised Fist0
NLPml~10 mins

LSTM for text in NLP - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the LSTM layer from Keras.

NLP
from tensorflow.keras.layers import [1]
Drag options to blanks, or click blank then click option'
ADense
BLSTM
CConv2D
DDropout
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Dense or Conv2D instead of LSTM.
Forgetting to import LSTM from keras.layers.
2fill in blank
medium

Complete the code to add an LSTM layer with 50 units to the model.

NLP
model.add(LSTM([1]))
Drag options to blanks, or click blank then click option'
A10
B100
C50
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using too few units like 5 which may underfit.
Using too many units like 100 which may overfit or slow training.
3fill in blank
hard

Fix the error in the code to compile the model with categorical crossentropy loss.

NLP
model.compile(optimizer='adam', loss='[1]', metrics=['accuracy'])
Drag options to blanks, or click blank then click option'
Acategorical_crossentropy
Bbinary_crossentropy
Chinge
Dmean_squared_error
Attempts:
3 left
💡 Hint
Common Mistakes
Using binary_crossentropy for multi-class problems.
Using mean_squared_error which is for regression.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

NLP
{word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Using less than instead of greater than in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts if count is greater than 0.

NLP
{ [1]: [2] for [3], count in word_counts.items() if count > 0 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Bcount
Cword
Dcount.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using count.upper() which is invalid since count is a number.
Using word instead of word.upper() as key.

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