W3cubDocs

/TensorFlow Python

tf.keras.preprocessing.sequence.pad_sequences

tf.keras.preprocessing.sequence.pad_sequences(
    sequences,
    maxlen=None,
    dtype='int32',
    padding='pre',
    truncating='pre',
    value=0.0
)

Defined in tensorflow/python/keras/_impl/keras/preprocessing/sequence.py.

Pads sequences to the same length.

This function transforms a list of num_samples 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 otherwise.

Sequences that are shorter than num_timesteps are padded with value at the end.

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 is the default.

Arguments:

  • sequences: List of lists, where each element is a sequence.
  • maxlen: Int, maximum length of all sequences.
  • dtype: Type of the output sequences.
  • padding: String, 'pre' or 'post': pad either before or after each sequence.
  • truncating: String, 'pre' or 'post': remove values from sequences larger than maxlen, either at the beginning or at the end of the sequences.
  • value: Float, padding value.

Returns:

  • x: Numpy array with shape (len(sequences), maxlen)

Raises:

  • ValueError: In case of invalid values for truncating or padding, or in case of invalid shape for a sequences entry.

© 2018 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/sequence/pad_sequences