Bird
0
0

Why does this padding code raise an error?

medium📝 Debug Q7 of 15
NLP - Sequence Models for NLP
Why does this padding code raise an error?
from tensorflow.keras.preprocessing.sequence import pad_sequences
sequences = [[1, 2, 3], [4, 5]]
padded = pad_sequences(sequences, maxlen=3, padding='middle')
Apad_sequences requires a 'truncating' parameter when padding
Bmaxlen must be greater than the longest sequence length
CSequences must be of equal length before padding
D'middle' is not a valid padding option; only 'pre' or 'post' are allowed
Step-by-Step Solution
Solution:
  1. Step 1: Check valid padding options

    pad_sequences accepts only 'pre' or 'post' for padding parameter.
  2. Step 2: Identify invalid value

    Using 'middle' causes an error because it is not supported.
  3. Final Answer:

    'middle' is not a valid padding option; only 'pre' or 'post' are allowed -> Option D
  4. Quick Check:

    Use only 'pre' or 'post' for padding [OK]
Quick Trick: Padding must be 'pre' or 'post' only [OK]
Common Mistakes:
MISTAKES
  • Using unsupported padding values like 'middle'
  • Assuming sequences must be equal length before padding
  • Forgetting to specify truncating when needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes