sequence.pad_sequences 的用法举例

>>> from tensorflow.keras.preprocessing import sequence
>>> help(sequence.pad_sequences)
>>> import numpy as np
>>> a=np.array([[1,2],[1,4,5],[3]])
>>> ap = sequence.pad_sequences(a,5)
>>> ap
array([[0, 0, 0, 1, 2],
       [0, 0, 1, 4, 5],
       [0, 0, 0, 0, 3]], dtype=int32)
>>> a
array([list([1, 2]), list([1, 4, 5]), list([3])], dtype=object)
>>> help(sequence.pad_sequences)

>>> ap = sequence.pad_sequences(a,5,padding='post')
>>> ap
array([[1, 2, 0, 0, 0],
       [1, 4, 5, 0, 0],
       [3, 0, 0, 0, 0]], dtype=int32)
>>> ap = sequence.pad_sequences(a,4,padding='post')
>>> ap
array([[1, 2, 0, 0],
       [1, 4, 5, 0],
       [3, 0, 0, 0]], dtype=int32)
>>> 
说明:  sequence.pad_sequences 就是把不等长的list变成等长. 默认填充是0, padding = 'pre'是前填充

padding='post'是后填充

sequence.pad_sequences 的用法举例_第1张图片

 

 

你可能感兴趣的:(深度学习,tensorflow)