（轻过渡音乐） (light transition music)
- 让我们来看看问答 pipeline(管线) 的内部情况。 - Let's have a look inside the question answering pipeline.
问答管线 The question answering pipeline
可以提取问题的答案 can extract answers to questions
来自给定的上下文或文本段落 from a given context or passage of text
就像 transformer 仓库的 README 文件的这一部分。 like this part of the Transformers repo README.
它也适用于很长的上下文， It also works for very long context,
即使答案靠后，就像这个例子一样。 even if the answer is at the very end, like in this example.
在本视频中，我们将了解原因。 In this video, we'll see why.
问答管线 The question answering pipeline
遵循与其他管线相同的步骤。 follows the same steps as the other pipelines.
问题和上下文被标记为一个句子对， The question and context are tokenized as a sentence pair,
提供给模型，然后应用一些后处理。 fed to the model then some post-processing is applied.
所以分词化和模型步骤应该很熟悉。 So tokenization and model steps should be familiar.
我们使用适合问答的 auto 类 We use the auto class suitable for question answering
而不是序列分类， instead of sequence classification,
但与文本分类的一个关键区别 but one key difference with text classification
是我们的模型输出两个张量 is that our model outputs two tensors
命名 start logits 和 end logits。 named start logits and end logits.
这是为什么？ Why is that?
嗯，这就是模型找到答案的方式 Well, this is the way the model finds the answer
对这个问题。 to the question.
首先，让我们看一下模型输入。 First, let's have a look at the model inputs.
它是与问题分词化相关的数字 It's numbers associated with the tokenization of the question
, ,
对于该上下文 followed by the context
使用通常的 CLS 和 SEP 特殊 token 。 with the usual CLS and SEP special tokens.
答案是那些 token 的一部分。 The answer is a part of those tokens.
所以我们要求模型预测 So we ask the model to predict
哪个 token 开始回答 which token starts the answer
并结束答案。 and which ends the answer.
对于我们的两个 logit 输出， For our two logit outputs,
理论标签是粉色和紫色的向量。 the theoretical labels are the pink and purple vectors.
要将这些 logits 转换为概率， To convert those logits into probabilities,
我们需要应用 SoftMax， we will need to apply a SoftMax,
就像在文本分类管线中一样。 like in the text classification pipeline.
我们只是掩蔽 token We just mask the tokens
在这样做之前不属于上下文的一部分， that are not part of the context before doing that,
不掩蔽初始 CLS token leaving the initial CLS token unmasked
我们用它来预测一个不可能的答案。 as we use it to predict an impossible answer.
这就是它在代码方面的样子。 This is what it looks like in terms of code.
我们使用一个大的负数作为掩码 We use a large negative number for the masking
因为它的指数将为零。 since its exponential will then be zero.
现在每个开始和结束位置的概率 Now the probability for each start and end position
对应一个可能的答案 corresponding to a possible answer
会给一个分数是一个产品 will give a score that is a product
开始概率和结束概率 of the start probabilities and end probabilities
在这些位置。 at those position.
当然，开始索引大于结束索引 Of course, a start index greater than an end index
对应一个不可能的答案。 corresponds to an impossible answer.
这是找到最佳分数的代码 Here is the code to find the best score
对一个可能的答案。 for a possible answer.
一旦我们有了 token 的开始和结束位置， Once we have the start and end position for the tokens,
我们使用分词器提供的偏移量映射 we use the offset mappings provided by our tokenizer
找到初始上下文中的字符范围， to find the span of characters in the initial context,
我们得到了答案。 and we get our answer.
现在，当上下文很长时， Now, when the context is long,
它可能会被分词器截断。 it might get truncated by the tokenizer.
这可能会导致部分答案，或者更糟的是， This might result in part of the answer, or worse,
整个答案，被截断了。 the whole answer, being truncated.
所以我们不丢弃截断的 token So we don't discard the truncated tokens
但与他们一起构建新功能。 but build new features with them.
这些功能中的每一个都包含问题， Each of those features contains the question,
然后是上下文中的一大块文本。 then a chunk of text in the context.
如果我们采用不相交的文本块， If we take disjoint chunks of texts,
我们可能会得到答案 we might end up with the answer
被分成两个特征。 being split between two features.
因此，我们取而代之的是重叠的文本块 So instead, we take overlapping chunks of text
确保至少其中一个块 to make sure at least one of the chunks
将完整包含问题的答案。 will fully contain the answer to the question.
所以，分词器会自动为我们完成所有这些 So, tokenizers does all of this for us automatically
使用 return overflowing tokens 选项。 with the return overflowing tokens option.
步幅参数 The stride argument
控制重叠标记的数量。 controls the number of overlapping tokens.
这是我们很长的上下文 Here is how our very long context
在一些重叠的两个特征中被截断。 gets truncated in two features with some overlap.
通过应用相同的后处理 By applying the same post-processing
我们之前看到的每个功能， we saw before for each feature,
我们得到每个分数的答案， we get the answer with a score for each of them,
我们选择得分最高的答案 and we take the answer with the best score
作为最终解决方案。 as a final solution.
（轻过渡音乐） (light transition music)