TensorFlow2教程-掩码和填充
在构建深度学习模型,特别是进行序列的相关任务时,经常会用到掩码和填充技术
原文地址:https://doit-space.blog.csdn.net/article/details/102885635
tensorflow2.0完整教程:https://blog.csdn.net/qq_31456593/article/details/88606284
1 填充序列数据
处理序列数据时,常常会遇到不同长度的文本,例如处理某些句子时:
[
["The", "weather", "will", "be", "nice", "tomorrow"],
["How", "are", "you", "doing", "today"],
["Hello", "world", "!"]
]
[['The', 'weather', 'will', 'be', 'nice', 'tomorrow'],
['How', 'are', 'you', 'doing', 'today'],
['Hello', 'world', '!']]
计算机无法理解字符,我们一般将词语转换为对应的id
[
[83, 91, 1, 645, 1253, 927],
[73, 8, 3215, 55, 927],
[71, 1331, 4231]
]
[[83, 91, 1, 645, 1253, 927], [73, 8, 3215, 55, 927], [71, 1331, 4231]]
由于深度信息模型的输入一般是固定的张量,所以我们需要对短文本进行填充(或对长文本进行截断),使每个样本的长度相同。
Keras提供了一个API,可以通过填充和截断,获取等长的数据:
tf.keras.preprocessing.sequence.pad_sequences
raw_inputs = [
[83, 91, 1, 645, 1253, 927],
[73, 8, 3215, 55, 927],
[711, 632, 71]
]
# 默认左填充
padded_inputs = tf.keras.preprocessing.sequence.pad_sequences(raw_inputs)