|  View source on GitHub | 
Pads sequences to the same length.
tf.keras.utils.pad_sequences(
    sequences,
    maxlen=None,
    dtype='int32',
    padding='pre',
    truncating='pre',
    value=0.0
)
Used in the notebooks
| Used in the guide | Used in the tutorials | 
|---|---|
This function transforms a list (of length num_samples)
of sequences (lists of integers)
into a 2D NumPy array of shape (num_samples, num_timesteps).
num_timesteps is either the maxlen argument if provided,
or the length of the longest sequence in the list.
Sequences that are shorter than num_timesteps
are padded with value until they are num_timesteps long.
Sequences longer than num_timesteps are truncated
so that they fit the desired length.
The position where padding or truncation happens is determined by
the arguments padding and truncating, respectively.
Pre-padding or removing values from the beginning of the sequence is the
default.
sequence = [[1], [2, 3], [4, 5, 6]]keras.utils.pad_sequences(sequence)array([[0, 0, 1],[0, 2, 3],[4, 5, 6]], dtype=int32)
keras.utils.pad_sequences(sequence, value=-1)array([[-1, -1, 1],[-1, 2, 3],[ 4, 5, 6]], dtype=int32)
keras.utils.pad_sequences(sequence, padding='post')array([[1, 0, 0],[2, 3, 0],[4, 5, 6]], dtype=int32)
keras.utils.pad_sequences(sequence, maxlen=2)array([[0, 1],[2, 3],[5, 6]], dtype=int32)
| Returns | |
|---|---|
| NumPy array with shape (len(sequences), maxlen) |