（徽标呼啸而过） (logo whooshing)
- pipeline 函数内部发生了什么？ *[译者注: pipeline 作为 流水线 的意思] - What happens inside the pipeline function?
在这段视频中，我们将看看实际发生了什么 In this video, we will look at what actually happens
当我们使用 Transformers 库的 when we use the pipeline function
pipeline 函数时 of the Transformers library.
详细来讲，我们将看 More specifically, we will look
在情绪分析的 pipeline 中， at the sentiment analysis pipeline,
它是如何从以下两个句子开始的， and how it went from the two following sentences,
将正负标签 to the positive and negative labels
加上各自的分数。 with their respective scores.
正如我们在 pipeline 展示中看到的那样， As we have seen in the pipeline presentation,
pipeline 分为三个阶段。 there are three stages in the pipeline.
首先，我们将原始文本转换为数字 First, we convert the raw texts to numbers
该模型可以通过使用分词器理解。 the model can make sense of using a tokenizer.
然后这些数字通过模型， Then those numbers go through the model,
输出 logits 。 which outputs logits.
最后，后处理步骤转换 Finally, the post-processing steps transforms
那些 logits 包含标签和分数。 those logits into labels and scores.
让我们详细看看这三个步骤 Let's look in detail at those three steps
以及如何使用 Transformers 库复制它们， and how to replicate them using the Transformers library,
从第一阶段开始，分词化。 beginning with the first stage, tokenization.
分词化过程有几个步骤。 The tokenization process has several steps.
首先，文本被分成小块, 称之为 token。 *[译者注: 后面 token-* 均翻译成 分词-*] First, the text is split into small chunks called tokens.
它们可以是单词、单词的一部分或标点符号。 They can be words, parts of words or punctuation symbols.
然后分词器将有一些特殊的 token ， Then the tokenizer will had some special tokens,
如果模型期望它们。 if the model expect them.
这里的模型在开头使用期望 CLS token Here the model uses expects a CLS token at the beginning
以及用于分类的句子末尾的 SEP token。 and a SEP token at the end of the sentence to classify.
最后，分词器将每个 token 与其唯一 ID 匹配 Lastly, the tokenizer matches each token to its unique ID
在预训练模型的词汇表中。 in the vocabulary of the pretrained model.
要加载这样的分词器， To load such a tokenizer,
Transformers 库提供了 AutoTokenizer API。 the Transformers library provides the AutoTokenizer API.
这个类最重要的方法是 from_pretrained， The most important method of this class is from_pretrained,
这将下载并缓存配置 which will download and cache the configuration
以及与给定检查点相关联的词汇表。 and the vocabulary associated to a given checkpoint.
这里默认使用的 checkpoint Here the checkpoint used by default
用于情绪分析的 pipeline for the sentiment analysis pipeline
是 distilbert-base-uncased-finetuned-sst-2-English。 is distilbert-base-uncased-finetuned-sst-2-English.
（模糊） (indistinct)
我们实例化一个与该检查点关联的分词器， We instantiate a tokenizer associated with that checkpoint,
然后给它输入两个句子。 then feed it the two sentences.
由于这两个句子的大小不同， Since those two sentences are not of the same size,
我们需要填充最短的一个 we will need to pad the shortest one
能够构建一个数组。 to be able to build an array.
这是由标记器使用选项 padding=True 完成的。 This is done by the tokenizer with the option, padding=True.
使用 truncation=True，我们确保任何句子 With truncation=True, we ensure that any sentence
超过模型可以处理的最大值的长度将被截断。 longer than the maximum the model can handle is truncated.
最后， return_tensors 选项 Lastly, the return_tensors option
告诉分词器返回一个 PyTorch 张量。 tells the tokenizer to return a PyTorch tensor.
查看结果，我们看到我们有一本字典 Looking at the result, we see we have a dictionary
和两个主键 with two keys.
输入 ID 包含两个句子的 ID， Input IDs contains the IDs of both sentences,
应用填充的位置为零。 with zero where the padding is applied.
第二个键值，注意力掩码， The second key, attention mask,
指示已应用填充的位置， indicates where padding has been applied,
所以模型不会关注它。 so the model does not pay attention to it.
这就是分词化步骤中的全部内容。 This is all what is inside the tokenization step.
现在，让我们来看看第二步，模型。 Now, let's have a look at the second step, the model.
至于分词器， As for the tokenizer,
有一个带有 from_pretrained 方法的 AutoModel API。 there is an AutoModel API with a from_pretrained method.
它将下载并缓存模型的配置 It will download and cache the configuration of the model
以及预训练的权重。 as well as the pretrained weights.
然而，AutoModel API However, the AutoModel API
只会实例化模型的主体， will only instantiate the body of the model,
那是模型剩下的部分 that is the part of the model that is left
一旦预训练头被移除。 once the pretraining head is removed.
它会输出一个高维张量 It will output a high-dimensional tensor
这是通过的句子的表示， that is a representation of the sentences passed,
但这不是直接有用的 but which is not directly useful
对于我们的分类问题。 for our classification problem.
这里的张量有两个句子，每个句子有 16 个 token ， Here the tensor has two sentences, each of 16 tokens,
最后一个维度是我们模型的隐藏大小，768。 and the last dimension is the hidden size of our model, 768.
要获得与我们的分类问题相关的输出， To get an output linked to our classification problem,
我们需要使用 AutoModelForSequenceClassification 类。 we need to use the AutoModelForSequenceClassification class.
它与 AutoModel 类完全一样工作， It works exactly as the AutoModel class,
除了它会建立一个模型 except that it will build a model
带分类头。 with a classification head.
每个常见的 NLP 任务在 Transformers 库中 There is one auto class for each common NLP task
都有一个自动类 in the Transformers library.
在给我们的模型两个句子之后， Here after giving our model the two sentences,
我们得到一个大小为二乘二的张量， we get a tensor of size two by two,
每个句子和每个可能的标签都有一个结果。 one result for each sentence and for each possible label.
这些输出还不是概率， Those outputs are not probabilities yet,
我们可以看到它们的总和不为 1。 we can see they don't sum to 1.
这是因为 Transformers 库中 This is because each model
每个模型都会返回 logits 。 of the Transformers library returns logits.
为了理解这些 logits ， To make sense of those logits,
我们需要深入研究管道的第三步也是最后一步。 we need to dig into the third and last step of the pipeline.
后处理，将 logits 转换为概率， Post-processing, to convert logits into probabilities,
我们需要对它们应用 SoftMax 层。 we need to apply a SoftMax layers to them.
正如我们所见， As we can see,
这会将它们转换为正数 this transforms them into positive number
总结为一个。 that sum up to one.
最后一步是知道哪些对应 The last step is to know which of those corresponds
正面或负面的标签。 to the positive or the negative label.
这是由模型配置的 id2label 字段给出的。 This is given by the id2label field of the model config.
第一概率，指数零， The first probabilities, index zero,
对应负标签， correspond to the negative label,
然后第二个，索引一， and the seconds, index one,
对应正标签。 correspond to the positive label.
这就是我们的分类器的构建方式 This is how our classifier built
使用 pipeline 功能选择了那些标签 with the pipeline function picked those labels
并计算出这些分数。 and computed those scores.
既然你知道每个步骤是如何工作的， Now that you know how each steps works,
你可以轻松地根据需要调整它们。 you can easily tweak them to your needs.
（徽标呼啸而过） (logo whooshing)