Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to pad sequences to the same length using Keras.
NLP
from tensorflow.keras.preprocessing.sequence import pad_sequences sequences = [[1, 2, 3], [4, 5], [6]] padded = pad_sequences(sequences, maxlen=[1]) print(padded)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting maxlen less than the longest sequence length, which truncates data.
Not using maxlen at all, causing inconsistent sequence lengths.
✗ Incorrect
The longest sequence has length 3, so maxlen=3 pads all sequences to length 3.
2fill in blank
mediumComplete the code to pad sequences with zeros at the end (post-padding).
NLP
from tensorflow.keras.preprocessing.sequence import pad_sequences sequences = [[7, 8], [9, 10, 11]] padded = pad_sequences(sequences, padding=[1]) print(padded)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pre' padding which adds zeros before sequences.
Using invalid padding values like 'middle' or 'end'.
✗ Incorrect
Setting padding='post' adds zeros at the end of sequences.
3fill in blank
hardFix the error in the code to truncate sequences longer than maxlen from the beginning.
NLP
from tensorflow.keras.preprocessing.sequence import pad_sequences sequences = [[1, 2, 3, 4], [5, 6, 7]] padded = pad_sequences(sequences, maxlen=3, truncating=[1]) print(padded)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' truncating which removes elements from the end.
Using invalid truncating values like 'start' or 'end'.
✗ Incorrect
truncating='pre' removes elements from the start to fit maxlen.
4fill in blank
hardFill both blanks to create a padded sequence with max length 4, post-padding, and pre-truncating.
NLP
from tensorflow.keras.preprocessing.sequence import pad_sequences sequences = [[10, 20, 30, 40, 50], [60, 70]] padded = pad_sequences(sequences, maxlen=[1], padding=[2], truncating='pre') print(padded)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxlen larger than needed, causing no truncation.
Using 'pre' padding which adds zeros before sequences.
✗ Incorrect
maxlen=4 limits length to 4; padding='post' adds zeros after sequences.
5fill in blank
hardFill all three blanks to create a padded sequence with max length 3, pre-padding, and post-truncating.
NLP
from tensorflow.keras.preprocessing.sequence import pad_sequences sequences = [[100, 200, 300, 400], [500]] padded = pad_sequences(sequences, maxlen=[1], padding=[2], truncating=[3]) print(padded)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' padding which adds zeros after sequences.
Using 'pre' truncating which removes from the start.
✗ Incorrect
maxlen=3 limits length; padding='pre' adds zeros before; truncating='post' removes from end.