Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pad_sequence' instead of 'pad_sequences'.
Trying to use a non-existent function like 'sequence_pad'.
✗ Incorrect
The correct function to pad sequences in TensorFlow Keras is 'pad_sequences'.
2fill in blank
mediumComplete 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)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pre' which pads at the beginning.
Using invalid padding values like 'middle' or 'end'.
✗ Incorrect
To pad sequences at the end, use padding='post'.
3fill in blank
hardFix 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)
Attempts:
3 left
💡 Hint
Common Mistakes
Setting maxlen less than the longest sequence truncates data.
Using None does not change the length.
✗ Incorrect
To pad sequences to length 5, set maxlen=5.
4fill in blank
hardFill 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)
Attempts:
3 left
💡 Hint
Common Mistakes
Using padding='post' when pre-padding is needed.
Using default padding value 0 instead of custom -1.
✗ Incorrect
Use padding='pre' to pad at the start and value=-1 for custom padding.
5fill in blank
hardFill 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)
Attempts:
3 left
💡 Hint
Common Mistakes
Using padding='pre' instead of 'post'.
Setting maxlen less than needed length.
Forgetting to set custom padding value.
✗ Incorrect
Set maxlen=3, padding='post' to pad at the end, and value=9 for custom padding.
