tf.keras.preprocessing.sequence.pad_sequences
Stay organized with collections
Save and categorize content based on your preferences.
Pads sequences to the same length.
tf.keras.preprocessing.sequence.pad_sequences(
sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre',
value=0.0
)
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 beginning or the end
if padding='post.
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.
To pad sequences with variable length strings, you can use `object`.
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 or String, 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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.preprocessing.sequence.pad_sequences\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/preprocessing/sequence/pad_sequences) |\n\nPads sequences to the same length.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.preprocessing.sequence.pad_sequences`](/api_docs/python/tf/keras/preprocessing/sequence/pad_sequences)\n\n\u003cbr /\u003e\n\n tf.keras.preprocessing.sequence.pad_sequences(\n sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre',\n value=0.0\n )\n\nThis function transforms a list of\n`num_samples` sequences (lists of integers)\ninto a 2D Numpy array of shape `(num_samples, num_timesteps)`.\n`num_timesteps` is either the `maxlen` argument if provided,\nor the length of the longest sequence otherwise.\n\nSequences that are shorter than `num_timesteps`\nare padded with `value` at the beginning or the end\nif padding='post.\n\nSequences longer than `num_timesteps` are truncated\nso that they fit the desired length.\nThe position where padding or truncation happens is determined by\nthe arguments `padding` and `truncating`, respectively.\n\nPre-padding is the default.\n\nArguments\n=========\n\n sequences: List of lists, where each element is a sequence.\n maxlen: Int, maximum length of all sequences.\n dtype: Type of the output sequences.\n To pad sequences with variable length strings, you can use `object`.\n padding: String, 'pre' or 'post':\n pad either before or after each sequence.\n truncating: String, 'pre' or 'post':\n remove values from sequences larger than\n `maxlen`, either at the beginning or at the end of the sequences.\n value: Float or String, padding value.\n\nReturns\n=======\n\n x: Numpy array with shape `(len(sequences), maxlen)`\n\nRaises\n======\n\n ValueError: In case of invalid values for `truncating` or `padding`,\n or in case of invalid shape for a `sequences` entry."]]