tf.keras.preprocessing.sequence.pad_sequences
Заполняет последовательности до одинаковой длины.
tf.keras.preprocessing.sequence.pad_sequences(
sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre',
value=0.0
)
Эта функция преобразует список num_samples последовательностей (списков целых чисел) в 2D массив NumPy формы (num_samples, num_timesteps). num_timesteps — это либо аргумент maxlen, если он предоставлен, либо длина самой длинной последовательности в противном случае.
Последовательности, которые короче num_timesteps, дополняются value в начале или в конце, если padding='post'.
Последовательности, длиннее num_timesteps, усекаются, чтобы они соответствовали требуемой длине. Позиция, где происходит заполнение или усечение, определяется аргументами padding и truncating, соответственно.
По умолчанию используется заполнение в начале.
Аргументы
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.
Возвращаемое значение
x: Numpy array with shape `(len(sequences), maxlen)`
Исключения
ValueError: In case of invalid values for `truncating` or `padding`,
or in case of invalid shape for a `sequences` entry.
© 2020 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/versions/r1.15/api_docs/python/tf/keras/preprocessing/sequence/pad_sequences