1 keras.preprocessing.sequence.pad_sequences使用
keras.preprocessing.sequence.pad_sequences(sequences,
maxlen=None,
dtype='int32',
padding='pre',
truncating='pre',
value=0.)
其中,
sequences:浮点数或整数构成的两层嵌套列表
maxlen:None或整数,为序列的最大长度。大于此长度的序列将被截短,小于此长度的序列将在后部填0.
dtype:返回的numpy array的数据类型
padding:‘pre’或‘post’,确定当需要补0时,在序列的起始还是结尾补(默认为pre)
truncating:‘pre’或‘post’,确定当需要截断序列时,从起始还是结尾截断(默认为pre)
value:浮点数,此值将在填充时代替默认的填充值0
函数返回的是一个二维张量。
举个栗子:
test_list1=[[1,2,3,4]]
keras.preprocessing.sequence.pad_sequences(test_list1,maxlen=10)
输出为 array([[0, 0, 0, 0, 0, 0, 1, 2, 3, 4]])
,可以看出默认是在序列前面补充填充值默认值0。
2 target的one-hot
# target data convert to one - hot
def processing_target(train_target, test_target, num_classes):
train_y = tf.one_hot(train_target, depth = num_classes)
test_y = tf.one_hot(test_target, depth = num_classes)
return train_y, test_y