（徽标嗖嗖声） (logo whooshes)
- 如何批量一起输入。 - How to batch inputs together.
在本视频中，我们将看到 In this video, we'll 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 and 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 may 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
或并批在一起，但是，我们注意到 or batched together, however, we notice
我们没有得到相同的结果 that we don't get the same results
对于被填充的句子。 for the sentence that is padded.
在这里，第二个。 Here, the second one.
希望是 transformer 库中的单词？ Expect the word in the transformer library?
不。 No.
如果你还记得 Transformer 模型大量使用 If you remember that Transformer models make heavy use
注意力层，它应该不足为奇。 of attention layers, it 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 a 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 具有相同形状的 tensor a tensor with the same shape as the input IDs
用 0 和 1 。 with zeros and ones.
1 的 token 表示注意层 Ones indicate the tokens the attention layers
应该在上下文中考虑， should consider in the context,
并且 0 的 token 他们应该忽略。 and zeros, the tokens they should ignore.
现在，通过这个注意力掩码 Now, passing this attention mask
连同输入 ID 会给我们相同的结果 along with the input IDs 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.