Challenge - 5 Problems
Sequence Padding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output shape after padding?
Given the following code that pads sequences, what will be the shape of the padded sequences?
TensorFlow
import tensorflow as tf sequences = [[1, 2, 3], [4, 5], [6]] padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=4, padding='post') print(padded.shape)
Attempts:
2 left
💡 Hint
Remember that padding adds elements to each sequence to reach maxlen.
✗ Incorrect
The input has 3 sequences, and maxlen=4 means each sequence is padded to length 4, so shape is (3,4).
❓ Model Choice
intermediateChoosing padding for RNN input
You want to feed variable-length sequences into an RNN model in TensorFlow. Which padding option is best to keep the original sequence order intact?
Attempts:
2 left
💡 Hint
RNNs process sequences in order; padding at the end preserves the start.
✗ Incorrect
Padding at the end ('post') keeps the original sequence order at the start, which is important for RNNs to learn meaningful patterns.
❓ Hyperparameter
advancedEffect of maxlen in sequence padding
What happens if you set maxlen smaller than the longest sequence when using tf.keras.preprocessing.sequence.pad_sequences?
Attempts:
2 left
💡 Hint
maxlen controls the final length after padding or truncation.
✗ Incorrect
If maxlen is smaller than some sequences, those sequences are truncated to maxlen length.
🔧 Debug
advancedWhy does this padding code raise an error?
Consider this code snippet:
import tensorflow as tf
sequences = [[1, 2], [3, 4, 5]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, padding='sideways')
What error will this code raise?
Attempts:
2 left
💡 Hint
Check the allowed values for the padding parameter.
✗ Incorrect
The padding parameter only accepts 'pre' or 'post'. 'sideways' is invalid and causes a ValueError.
🧠 Conceptual
expertWhy use masking with padded sequences?
When feeding padded sequences into a neural network, why is it important to use masking layers or techniques?
Attempts:
2 left
💡 Hint
Padded zeros are not real data and can confuse the model if not masked.
✗ Incorrect
Masking tells the model which parts of the input are padding so it can ignore them during training and prediction.
