0
0
NLPml~10 mins

Padding and sequence length in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A3
B5
C2
D4
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.
2fill in blank
medium

Complete 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'
A'post'
B'pre'
C'middle'
D'end'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pre' padding which adds zeros before sequences.
Using invalid padding values like 'middle' or 'end'.
3fill in blank
hard

Fix 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'
A'start'
B'post'
C'pre'
D'end'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' truncating which removes elements from the end.
Using invalid truncating values like 'start' or 'end'.
4fill in blank
hard

Fill 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'
A4
B'post'
C'pre'
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxlen larger than needed, causing no truncation.
Using 'pre' padding which adds zeros before sequences.
5fill in blank
hard

Fill 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'
A3
B'pre'
C'post'
D'start'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' padding which adds zeros after sequences.
Using 'pre' truncating which removes from the start.