Bird
Raised Fist0
TensorFlowml~10 mins

Sequence padding in TensorFlow - Interactive Code Practice

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

Complete the code to pad sequences to the same length using TensorFlow.

TensorFlow
import tensorflow as tf
sequences = [[1, 2, 3], [4, 5], [6]]
padded = tf.keras.preprocessing.sequence.[1](sequences)
print(padded)
Drag options to blanks, or click blank then click option'
Apad_sequence
Bpad_sequences
Csequence_pad
Dpad_seq
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pad_sequence' instead of 'pad_sequences'.
Trying to use a non-existent function like 'sequence_pad'.
2fill in blank
medium

Complete the code to pad sequences with zeros at the end (post-padding).

TensorFlow
import tensorflow as tf
sequences = [[1, 2], [3, 4, 5]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, padding=[1])
print(padded)
Drag options to blanks, or click blank then click option'
A'post'
B'middle'
C'pre'
D'end'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pre' which pads at the beginning.
Using invalid padding values like 'middle' or 'end'.
3fill in blank
hard

Fix the error in the code to pad sequences to a max length of 5.

TensorFlow
import tensorflow as tf
sequences = [[1, 2, 3], [4, 5]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=[1])
print(padded)
Drag options to blanks, or click blank then click option'
A5
B3
C2
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting maxlen less than the longest sequence truncates data.
Using None does not change the length.
4fill in blank
hard

Fill both blanks to pad sequences with a custom value and pad at the beginning.

TensorFlow
import tensorflow as tf
sequences = [[1], [2, 3]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, padding=[1], value=[2])
print(padded)
Drag options to blanks, or click blank then click option'
A'pre'
B'post'
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using padding='post' when pre-padding is needed.
Using default padding value 0 instead of custom -1.
5fill in blank
hard

Fill all three blanks to pad sequences to length 3, pad at the end, and use padding value 9.

TensorFlow
import tensorflow as tf
sequences = [[1, 2], [3]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=[1], padding=[2], value=[3])
print(padded)
Drag options to blanks, or click blank then click option'
A3
B'post'
C9
D'pre'
Attempts:
3 left
💡 Hint
Common Mistakes
Using padding='pre' instead of 'post'.
Setting maxlen less than needed length.
Forgetting to set custom padding value.

Practice

(1/5)
1. What is the main purpose of sequence padding in TensorFlow?
easy
A. To make all sequences the same length by adding zeros
B. To shuffle the sequences randomly
C. To normalize the values in sequences
D. To remove zeros from sequences

Solution

  1. Step 1: Understand sequence length requirements

    Models need inputs of the same length to process batches efficiently.
  2. Step 2: Role of padding

    Padding adds zeros to shorter sequences to match the longest sequence length.
  3. Final Answer:

    To make all sequences the same length by adding zeros -> Option A
  4. Quick Check:

    Sequence padding = same length sequences [OK]
Hint: Padding adds zeros to equalize sequence lengths [OK]
Common Mistakes:
  • Thinking padding shuffles sequences
  • Confusing padding with normalization
  • Assuming padding removes zeros
2. Which of the following is the correct way to pad sequences using TensorFlow's tf.keras.preprocessing.sequence.pad_sequences function?
easy
A. pad_sequences(sequences, maxlen=5, padding='post')
B. pad_sequences(sequences, maxlen=5, pad='after')
C. pad_sequences(sequences, length=5, padding='pre')
D. pad_sequences(sequences, max_length=5, padding='middle')

Solution

  1. Step 1: Check function parameters

    The correct parameter for length is maxlen, and padding accepts 'pre' or 'post'.
  2. Step 2: Validate option syntax

    pad_sequences(sequences, maxlen=5, padding='post') uses maxlen=5 and padding='post', which is correct syntax.
  3. Final Answer:

    pad_sequences(sequences, maxlen=5, padding='post') -> Option A
  4. Quick Check:

    Correct param names = pad_sequences(sequences, maxlen=5, padding='post') [OK]
Hint: Use maxlen and padding='pre' or 'post' in pad_sequences [OK]
Common Mistakes:
  • Using wrong parameter names like pad or length
  • Using invalid padding values like 'middle'
  • Confusing maxlen with max_length
3. What will be the output of this code?
import tensorflow as tf
sequences = [[1, 2, 3], [4, 5], [6]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=4, padding='pre')
print(padded.tolist())
medium
A. [[1, 2, 3], [4, 5], [6]]
B. [[0, 1, 2, 3], [0, 0, 4, 5], [0, 0, 0, 6]]
C. [[1, 2, 3, 0], [4, 5, 0, 0], [6, 0, 0, 0]]
D. [[0, 0, 1, 2], [0, 4, 5, 0], [6, 0, 0, 0]]

Solution

  1. Step 1: Understand padding='pre'

    Padding adds zeros before the sequence to reach maxlen=4.
  2. Step 2: Apply padding to each sequence

    First: [1,2,3] -> [0,1,2,3]; Second: [4,5] -> [0,0,4,5]; Third: [6] -> [0,0,0,6]
  3. Final Answer:

    [[0, 1, 2, 3], [0, 0, 4, 5], [0, 0, 0, 6]] -> Option B
  4. Quick Check:

    Pre-padding adds zeros before sequences [OK]
Hint: Pre-padding adds zeros at the start of sequences [OK]
Common Mistakes:
  • Confusing pre-padding with post-padding
  • Expecting original sequences without padding
  • Miscounting zeros added
4. This code throws an error. What is the mistake?
import tensorflow as tf
sequences = [[1, 2], [3, 4, 5]]
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=3, padding='middle')
print(padded)
medium
A. Sequences must be numpy arrays, not lists
B. maxlen should be larger than longest sequence
C. Invalid padding value 'middle' used
D. Missing argument 'value' for padding

Solution

  1. Step 1: Check padding parameter values

    Valid padding values are 'pre' or 'post'. 'middle' is invalid.
  2. Step 2: Identify error cause

    Using 'middle' causes a ValueError because it's not supported.
  3. Final Answer:

    Invalid padding value 'middle' used -> Option C
  4. Quick Check:

    Padding must be 'pre' or 'post' [OK]
Hint: Use only 'pre' or 'post' for padding parameter [OK]
Common Mistakes:
  • Using unsupported padding values
  • Thinking maxlen must be bigger than longest sequence
  • Assuming lists cause errors
5. You have sequences of different lengths and want to pad them so that all sequences have length 6. You want zeros added after the sequences. Which code correctly achieves this and why?
hard
A. pad_sequences(sequences, maxlen=6, value=1) because it pads with ones to length 6
B. pad_sequences(sequences, maxlen=6, padding='pre') because it pads zeros before sequences to length 6
C. pad_sequences(sequences, maxlen=6, truncating='post') because it truncates sequences after length 6
D. pad_sequences(sequences, maxlen=6, padding='post') because it pads zeros after sequences to length 6

Solution

  1. Step 1: Understand padding='post'

    Padding='post' adds zeros after the sequence to reach maxlen.
  2. Step 2: Check other options

    pad_sequences(sequences, maxlen=6, padding='pre') because it pads zeros before sequences to length 6 pads before sequences, not after. pad_sequences(sequences, maxlen=6, truncating='post') because it truncates sequences after length 6 truncates sequences, not pads. pad_sequences(sequences, maxlen=6, value=1) because it pads with ones to length 6 pads with ones, not zeros.
  3. Final Answer:

    pad_sequences(sequences, maxlen=6, padding='post') -> Option D
  4. Quick Check:

    Post-padding adds zeros after sequences [OK]
Hint: Use padding='post' to add zeros after sequences [OK]
Common Mistakes:
  • Confusing pre and post padding
  • Using truncating instead of padding
  • Changing pad value from zero unintentionally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard