LSTM stands for Long Short-Term Memory. It is a type of neural network layer designed to remember information for long periods.
Click to reveal answer
beginner
How does an LSTM layer help with sequence data?
An LSTM layer keeps track of important information over time by using gates to control what to remember, forget, or output. This helps it learn patterns in sequences like sentences or time series.
Click to reveal answer
intermediate
Name the three main gates inside an LSTM cell.
The three main gates are: Forget gate (decides what to forget), Input gate (decides what new information to add), and Output gate (decides what to output).
Click to reveal answer
intermediate
What is the purpose of the cell state in an LSTM layer?
The cell state acts like a conveyor belt that carries important information through the sequence, allowing the LSTM to keep or discard information as needed.
Click to reveal answer
beginner
In TensorFlow, how do you add an LSTM layer to a model?
You can add an LSTM layer using: tf.keras.layers.LSTM(units, return_sequences=True/False). 'units' is the number of memory cells.
Click to reveal answer
What problem does an LSTM layer mainly solve?
AReducing model size
BRemembering long-term dependencies in sequences
CImproving image resolution
DSorting data faster
✗ Incorrect
LSTM layers are designed to remember important information over long sequences, which helps with tasks like language or time series.
Which gate in an LSTM decides what information to forget?
AForget gate
BInput gate
COutput gate
DReset gate
✗ Incorrect
The forget gate controls which information is removed from the cell state.
In TensorFlow, which argument controls if an LSTM layer returns the full sequence or just the last output?
Areturn_sequences
Bactivation
Cinput_shape
Dunits
✗ Incorrect
The 'return_sequences' argument decides if the LSTM outputs the full sequence or only the last output.
What type of data is an LSTM layer best suited for?
AImages
BTabular data
CSequential data like text or time series
DStatic numbers
✗ Incorrect
LSTMs are designed to handle sequential data where order and context matter.
Which of these is NOT a part of an LSTM cell?
AOutput gate
BForget gate
CInput gate
DPooling layer
✗ Incorrect
Pooling layers are used in convolutional networks, not inside LSTM cells.
Explain how an LSTM layer processes sequence data and why it is better than a simple RNN.
Think about how LSTM remembers important info over time.
You got /4 concepts.
Describe how to add an LSTM layer in TensorFlow and what key parameters you need to set.
Focus on the code and what controls output shape.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of an LSTM layer in a neural network?
easy
A. To remember important information from past sequence data
B. To perform image classification
C. To reduce the size of input data
D. To generate random numbers
Solution
Step 1: Understand LSTM function
LSTM layers are designed to handle sequence data by remembering important past information, unlike regular layers.
Step 2: Compare options
Options A, B, and C describe unrelated tasks. Only To remember important information from past sequence data correctly describes LSTM's purpose.
Final Answer:
To remember important information from past sequence data -> Option A
Quick Check:
LSTM purpose = Remember past info [OK]
Hint: LSTM = memory for sequences, not general tasks [OK]
Common Mistakes:
Confusing LSTM with convolutional layers
Thinking LSTM reduces data size
Assuming LSTM generates random outputs
2. Which of the following is the correct way to add an LSTM layer with 50 units using the `units` keyword in TensorFlow Keras?
easy
A. tf.keras.layers.LSTM(50)
B. tf.keras.layers.LSTM(units=50)
C. tf.keras.layers.LSTM(50, return_sequences=True)
D. tf.keras.layers.LSTM(50, dropout=0.2)
Solution
Step 1: Recall LSTM syntax
The LSTM layer requires the number of units as a named argument 'units=50' for clarity and correctness.
Step 2: Evaluate options
tf.keras.layers.LSTM(50) is allowed but less clear. Options C and D add extra parameters but the question asks for the correct basic syntax using the 'units' keyword.
Final Answer:
tf.keras.layers.LSTM(units=50) -> Option B
Quick Check:
LSTM syntax = units=number [OK]
Hint: Use named argument 'units' for clarity in LSTM layer [OK]
Common Mistakes:
Omitting 'units=' keyword
Confusing dropout with units parameter
Adding unnecessary parameters when not asked
3. What will be the output shape of the following LSTM layer if the input shape is (batch_size, 10, 8)?
tf.keras.layers.LSTM(16, return_sequences=False)
medium
A. (batch_size, 8)
B. (batch_size, 10, 16)
C. (batch_size, 16)
D. (batch_size, 10, 8)
Solution
Step 1: Understand return_sequences effect
When return_sequences=False, the LSTM returns only the last output for the sequence, so output shape is (batch_size, units).
Step 2: Apply to given parameters
Units=16, so output shape is (batch_size, 16). Options B and D include sequence length, which is incorrect here.
Hint: return_sequences=False means output shape excludes time steps [OK]
Common Mistakes:
Assuming output keeps sequence length when return_sequences=False
Confusing input shape with output shape
Mixing units and input feature dimensions
4. Identify the error in this LSTM layer code snippet:
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(32, input_shape=(20, 5), return_sequences=False))
model.add(tf.keras.layers.LSTM(16))
model.add(tf.keras.layers.Dense(1))
medium
A. The first LSTM layer must have return_sequences=True
B. The input_shape should be (5, 20) instead of (20, 5)
C. The second LSTM layer should have return_sequences=True
D. Dense layer cannot follow LSTM layers
Solution
Step 1: Check LSTM stacking rules
When stacking LSTM layers, all except the last must have return_sequences=True to pass full sequence outputs to the next layer.
Step 2: Analyze given code
The first LSTM has return_sequences=False, outputting only (batch_size, 32). The second LSTM expects 3D sequence input but receives 2D, causing a shape incompatibility error.
Final Answer:
The first LSTM layer must have return_sequences=True -> Option A
Quick Check:
Stacked LSTM earlier layers need return_sequences=True [OK]
Hint: Only last LSTM can have return_sequences=False [OK]
Common Mistakes:
Forgetting return_sequences=True in middle LSTM layers
Mixing input_shape dimensions
Thinking Dense can't follow LSTM
5. You want to build an LSTM model that outputs a sequence of the same length as input (10 time steps) with 20 features each. Which configuration achieves this?
hard
A. tf.keras.layers.LSTM(20, return_sequences=False, input_shape=(10, 20))
B. tf.keras.layers.LSTM(20, return_sequences=True, input_shape=(20, 10))
C. tf.keras.layers.LSTM(10, return_sequences=True, input_shape=(20, 20))
D. tf.keras.layers.LSTM(20, return_sequences=True, input_shape=(10, 20))
Solution
Step 1: Understand input and output shapes
Input shape is (10, 20) meaning 10 time steps, 20 features. To output sequence of same length, return_sequences must be True.
Step 2: Check options
tf.keras.layers.LSTM(20, return_sequences=True, input_shape=(10, 20)) matches input shape and sets return_sequences=True with units=20, so output shape is (batch_size, 10, 20). Options A, C and D have wrong return_sequences or input shapes.
Final Answer:
tf.keras.layers.LSTM(20, return_sequences=True, input_shape=(10, 20)) -> Option D