What does Keras.io.preprocessing.sequence.pad_sequences do?

Keras documentation can be improved here. After reading this, I still don't understand what exactly this is doing: Keras.io.preprocessing.sequence.pad_sequence

Can someone highlight what this function does and ideally give an example?

+32
source share
1 answer

pad_sequencesused to ensure that all sequences in the list have the same length. By default, this is done by adding 0at the beginning of each sequence until each sequence has the same length as the longest sequence.

for example

>>> pad_sequences([[1, 2, 3], [3, 4, 5, 6], [7, 8]])
array([[0, 1, 2, 3],
       [3, 4, 5, 6],
       [0, 0, 7, 8]], dtype=int32)

[3, 4, 5, 6] - , 0 , [3, 4, 5, 6].

, padding='post'.

, maxlen. , maxlen.

>>> pad_sequences([[1, 2, 3], [3, 4, 5, 6], [7, 8]], maxlen=3)
array([[1, 2, 3],
       [4, 5, 6],
       [0, 7, 8]], dtype=int32)

3.

+44

Source: https://habr.com/ru/post/1015925/


All Articles