（微妙的爆炸） (subtle blast)
- 如何一起批量输入。 - How to batch inputs together.
在本视频中，我们将看到如何 In this video, we will see how
将输入序列一起批处理。 to batch input sequences together.
一般来说，我们想要输入模型的句子 In general, the sentences we want to pass through our model
不会都有相同的长度。 won't all have the same lengths.
在这里，我们使用模型 Here, we are using the model we saw
在情绪分析 pipeline 中讲的 in the sentiment analysis pipeline
并想对两个句子进行分类。 and want to classify two sentences.
将它们分词化并映射每个分词时 *[译者注: token, tokenization, tokenizer 等词均译成了 分词*, 实则不翻译最佳] When tokenizing them and mapping each token
到其相应的输入 ID， to its corresponding input IDs,
我们得到两个不同长度的列表。 we get two lists of different lengths.
尝试创建 tensor 或 NumPy 数组 Trying to create a tensor or a NumPy array
从这两个列表中, 将导致错误， from those two lists will result in an error,
因为所有数组和张量都应该是矩形的。 because all arrays and tensors should be rectangular.
突破此限制的一种方法 One way to overcome this limit
是让第二句 is to make the second sentence
与第一个长度相同 the same length as the first
通过根据需要多次添加特殊分词。 by adding a special token as many times as necessary.
另一种方法是截断第一个序列 Another way would be to truncate the first sequence
到第二个的长度， to the length of the second,
但我们会失去很多信息 but we would then lose a lot of information
而这可能是正确分类句子所必需的。 that might be necessary to properly classify the sentence.
一般来说，我们只截断句子 In general, we only truncate sentences
当它们长于最大长度时 when they are longer than the maximum length
该模型可以处理。 the model can handle.
用于填充第二句的值 The value used to pad the second sentence
不应被随意挑选； should not be picked randomly;
该模型已经用特定的填充 ID 进行了预训练， the model has been pretrained with a certain padding ID,
你可以在 tokenizer.pad_token_id 中找到它。 which you can find in tokenizer.pad_token_id.
现在我们已经填充了句子， Now that we have padded our sentences,
我们可以和他们做成一批。 we can make a batch with them.
如果我们分别将两个句子传递给模型 If we pass the two sentences to the model separately
和并批在一起，然而 and batched together however,
我们注意到我们没有得到相同的结果 we notice that we don't get the same results
对于被填充的句子， for the sentence that is padded,
在这里，第二个。 here, the second one.
是 Transformers 有问题？不。 It's at the backend in the Transformers Library? No.
如果你还记得 Transformer 模型 If you remember that Transformer models
大量使用注意力层， make heavy use of attention layers,
这应该不足为奇； this should not come as a total surprise;
在计算每个分词的上下文表示时， when computing the contextual representation of each token,
注意层查看所有其他词 the attention layers look at all the other words
在句子中。 in the sentence.
如果我们只有这句话 If we have just the sentence
或者添加了几个填充 token 的句子， or the sentence with several padding tokens added,
我们没有得到相同的值是合乎逻辑的。 it's logical we don't get the same values.
要在有或没有填充的情况下获得相同的结果， To get the same results with or without padding,
我们需要向注意力层表明 we need to indicate to the attention layers
他们应该忽略那些填充 token 。 that they should ignore those padding tokens.
这是通过创建一个注意力掩码来完成的， This is done by creating an attention mask,
与输入 ID 具有相同形状的张量， a tensor with the same shape as the input IDs,
用 0 和 1 。 with zeros and ones.
1 的分词表示注意层 Ones indicate the tokens the attention layers
应该结合上下文考虑 should consider in the context
并且 0 的分词他们应该忽略。 and zeros the tokens they should ignore.
现在，将这个注意掩码与输入 ID 一起传入 Now, passing this attention mask along with the input ID
会给我们相同的结果 will give us the same results
就像我们将两个句子单独发送给模型一样。 as when we sent the two sentences individually to the model.
这一切都是由分词器在幕后完成的 This is all done behind the scenes by the tokenizer
当你将它应用于几个句子时 when you apply it to several sentences
设参数 padding=True。 with the flag padding=True.
它将应用具有适当值的填充 It will apply the padding with the proper value
对较小的句子 to the smaller sentences
并创建适当的注意掩码。 and create the appropriate attention mask.
（微妙的爆炸） (subtle blast)