当前位置:   article > 正文

Transformers 基础函数pipeline()

Transformers 基础函数pipeline()

Transformers 库将目前的 NLP 任务归纳为几下几类:

  • 文本分类:例如情感分析、句子对关系判断等;
  • 对文本中的词语进行分类:例如词性标注 (POS)、命名实体识别 (NER) 等;
  • 文本生成:例如填充预设的模板 (prompt)、预测文本中被遮掩掉 (masked) 的词语;
  • 从文本中抽取答案:例如根据给定的问题从一段文本中抽取出对应的答案;
  • 根据输入文本生成新的句子:例如文本翻译、自动摘要等。

Transformers 库最基础的对象就是 pipeline() 函数,它封装了预训练模型和对应的前处理和后处理环节。我们只需输入文本,就能得到预期的答案。目前常用的 pipelines 有:

  • feature-extraction (获得文本的向量化表示)
  • fill-mask (填充被遮盖的词、片段)
  • ner(命名实体识别)
  • question-answering (自动问答)
  • sentiment-analysis (情感分析)
  • summarization (自动摘要)
  • text-generation (文本生成)
  • translation (机器翻译)
  • zero-shot-classification (零训练样本分类)

下面我们以常见的几个 NLP 任务为例,展示如何调用这些 pipeline 模型。

情感分析

借助情感分析 pipeline,我们只需要输入文本,就可以得到其情感标签(积极/消极)以及对应的概率:

  1. from transformers import pipeline
  2. classifier = pipeline("sentiment-analysis")
  3. result = classifier("I've been waiting for a HuggingFace course my whole life.")
  4. print(result)
  5. results = classifier(
  6. ["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"]
  7. )
  8. print(results)
  1. No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english)
  2. [{'label': 'POSITIVE', 'score': 0.9598048329353333}]
  3. [{'label': 'POSITIVE', 'score': 0.9598048329353333}, {'label': 'NEGATIVE', 'score': 0.9994558691978455}]

pipeline 模型会自动完成以下三个步骤:

  1. 将文本预处理为模型可以理解的格式;
  2. 将预处理好的文本送入模型;
  3. 对模型的预测值进行后处理,输出人类可以理解的格式。

pipeline 会自动选择合适的预训练模型来完成任务。例如对于情感分析,默认就会选择微调好的英文情感模型 distilbert-base-uncased-finetuned-sst-2-english

注意:

Transformers 库会在创建对象时下载并且缓存模型,只有在首次加载模型时才会下载,后续会直接调用缓存好的模型。

零训练样本分类

零训练样本分类 pipeline 允许我们在不提供任何标注数据的情况下自定义分类标签。

  1. from transformers import pipeline
  2. classifier = pipeline("zero-shot-classification")
  3. result = classifier(
  4. "This is a course about the Transformers library",
  5. candidate_labels=["education", "politics", "business"],
  6. )
  7. print(result)

 分析:我们把输入的一句话,分为三个分类【教育,政治,商业】,最后模型给出结果,这句话分类到教育的概率是0.844597。这个类似新闻多标签分类

  1. No model was supplied, defaulted to facebook/bart-large-mnli (https://huggingface.co/facebook/bart-large-mnli)
  2. {'sequence': 'This is a course about the Transformers library',
  3. 'labels': ['education', 'business', 'politics'],
  4. 'scores': [0.8445973992347717, 0.11197526752948761, 0.043427325785160065]}

可以看到,pipeline 自动选择了预训练好的 facebook/bart-large-mnli 模型来完成任务。 

文本生成

我们首先根据任务需要构建一个模板 (prompt),然后将其送入到模型中来生成后续文本。注意,由于文本生成具有随机性,因此每次运行都会得到不同的结果

  1. from transformers import pipeline
  2. generator = pipeline("text-generation")
  3. results = generator("In this course, we will teach you how to")
  4. print(results)
  5. results = generator(
  6. "In this course, we will teach you how to",
  7. num_return_sequences=2,
  8. max_length=50
  9. )
  10. print(results)
代码解释:传入一个句子,最大返回文本结果数量是2,允许最大长度是50
  1. results = generator(
  2. "In this course, we will teach you how to",
  3. num_return_sequences=2,
  4. max_length=50
  5. )
  1. No model was supplied, defaulted to gpt2 (https://huggingface.co/gpt2)
  2. [{'generated_text': "In this course, we will teach you how to use data and models that can be applied in any real-world, everyday situation. In most cases, the following will work better than other courses I've offered for an undergrad or student. In order"}]
  3. [{'generated_text': 'In this course, we will teach you how to make your own unique game called "Mono" from scratch by doing a game engine, a framework and the entire process starting with your initial project. We are planning to make some basic gameplay scenarios and'}, {'generated_text': 'In this course, we will teach you how to build a modular computer, how to run it on a modern Windows machine, how to install packages, and how to debug and debug systems. We will cover virtualization and virtualization without a programmer,'}]

 pipeline 自动选择了预训练好的 gpt2 模型来完成任务。我们也可以指定要使用的模型。对于文本生成任务,我们可以在 Model Hub 页面左边选择 Text Generation tag 查询支持的模型。例如,我们在相同的 pipeline 中加载 distilgpt2 模型:

 也可以自定模型,来进行文本生成,

generator = pipeline("text-generation", model="distilgpt2")
  1. from transformers import pipeline
  2. generator = pipeline("text-generation", model="distilgpt2")
  3. results = generator(
  4. "In this course, we will teach you how to",
  5. max_length=30,
  6. num_return_sequences=2,
  7. )
  8. print(results)
  1. [{'generated_text': 'In this course, we will teach you how to use React in any form, and how to use React without having to worry about your React dependencies because'},
  2. {'generated_text': 'In this course, we will teach you how to use a computer system in order to create a working computer. It will tell you how you can use'}]

还可以通过左边的语言 tag 选择其他语言的模型。例如加载专门用于生成中文古诗的 gpt2-chinese-poem 模型:

  1. from transformers import pipeline
  2. generator = pipeline("text-generation", model="uer/gpt2-chinese-poem")
  3. results = generator(
  4. "[CLS] 万 叠 春 山 积 雨 晴 ,",
  5. max_length=40,
  6. num_return_sequences=2,
  7. )
  8. print(results)

  1. [{'generated_text': '[CLS] 万 叠 春 山 积 雨 晴 , 孤 舟 遥 送 子 陵 行 。 别 情 共 叹 孤 帆 远 , 交 谊 深 怜 一 座 倾 。 白 日 风 波 身 外 幻'},
  2. {'generated_text': '[CLS] 万 叠 春 山 积 雨 晴 , 满 川 烟 草 踏 青 行 。 何 人 唤 起 伤 春 思 , 江 畔 画 船 双 橹 声 。 桃 花 带 雨 弄 晴 光'}]

遮盖词填充

给定一段部分词语被遮盖掉 (masked) 的文本,使用预训练模型来预测能够填充这些位置的词语。

  1. from transformers import pipeline
  2. unmasker = pipeline("fill-mask")
  3. results = unmasker("This course will teach you all about <mask> models.", top_k=2)
  4. print(results)

 设置top_k=2,返回俩个sequence(序列),填充了俩个不同的字,但是对填充的字,有score评分。可以看到,pipeline 自动选择了预训练好的 distilroberta-base 模型来完成任务。

  1. No model was supplied, defaulted to distilroberta-base (https://huggingface.co/distilroberta-base)
  2. [{'sequence': 'This course will teach you all about mathematical models.',
  3. 'score': 0.19619858264923096,
  4. 'token': 30412,
  5. 'token_str': ' mathematical'},
  6. {'sequence': 'This course will teach you all about computational models.',
  7. 'score': 0.04052719101309776,
  8. 'token': 38163,
  9. 'token_str': ' computational'}]

命名实体识别

命名实体识别 (NER) pipeline 负责从文本中抽取出指定类型的实体,例如人物、地点、组织等等。

  1. from transformers import pipeline
  2. ner = pipeline("ner", grouped_entities=True)
  3. results = ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
  4. print(results)
  1. No model was supplied, defaulted to dbmdz/bert-large-cased-finetuned-conll03-english (https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english)
  2. [{'entity_group': 'PER', 'score': 0.9981694, 'word': 'Sylvain', 'start': 11, 'end': 18},
  3. {'entity_group': 'ORG', 'score': 0.97960186, 'word': 'Hugging Face', 'start': 33, 'end': 45},
  4. {'entity_group': 'LOC', 'score': 0.99321055, 'word': 'Brooklyn', 'start': 49, 'end': 57}]

 可以看到,模型正确地识别出了 Sylvain 是一个人物【PER】,Hugging Face 是一个组织【ORG】,Brooklyn 是一个地名【LOC】。

【这里通过设置参数 grouped_entities=True,使得 pipeline 自动合并属于同一个实体的多个子词 (token),例如这里将“Hugging”和“Face”合并为一个组织实体,实际上 Sylvain 也进行了子词合并,因为分词器会将 Sylvain 切分为 S##yl 、##va 和 ##in 四个 token。】

自动问答

自动问答 pipeline 可以根据给定的上下文回答问题,例如:

  1. from transformers import pipeline
  2. question_answerer = pipeline("question-answering")
  3. answer = question_answerer(
  4. question="Where do I work?",
  5. context="My name is Sylvain and I work at Hugging Face in Brooklyn",
  6. )
  7. print(answer)
  1. No model was supplied, defaulted to distilbert-base-cased-distilled-squad (https://huggingface.co/distilbert-base-cased-distilled-squad)
  2. {'score': 0.6949771046638489, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}

可以看到,pipeline 自动选择了在 SQuAD 数据集上训练好的 distilbert-base 模型来完成任务。这里的自动问答 pipeline 实际上是一个抽取式问答模型,即从给定的上下文中抽取答案,而不是生成答案。

根据形式的不同,自动问答 (QA) 系统可以分为三种:

  • 抽取式 QA (extractive QA):假设答案就包含在文档中,因此直接从文档中抽取答案;
  • 多选 QA (multiple-choice QA):从多个给定的选项中选择答案,相当于做阅读理解题;
  • 无约束 QA (free-form QA):直接生成答案文本,并且对答案文本格式没有任何限制。

】 

自动摘要

自动摘要 pipeline 旨在将长文本压缩成短文本,并且还要尽可能保留原文的主要信息,例如:

  1. from transformers import pipeline
  2. summarizer = pipeline("summarization")
  3. results = summarizer(
  4. """
  5. America has changed dramatically during recent years. Not only has the number of
  6. graduates in traditional engineering disciplines such as mechanical, civil,
  7. electrical, chemical, and aeronautical engineering declined, but in most of
  8. the premier American universities engineering curricula now concentrate on
  9. and encourage largely the study of engineering science. As a result, there
  10. are declining offerings in engineering subjects dealing with infrastructure,
  11. the environment, and related issues, and greater concentration on high
  12. technology subjects, largely supporting increasingly complex scientific
  13. developments. While the latter is important, it should not be at the expense
  14. of more traditional engineering.
  15. Rapidly developing economies such as China and India, as well as other
  16. industrial countries in Europe and Asia, continue to encourage and advance
  17. the teaching of engineering. Both China and India, respectively, graduate
  18. six and eight times as many traditional engineers as does the United States.
  19. Other industrial countries at minimum maintain their output, while America
  20. suffers an increasingly serious decline in the number of engineering graduates
  21. and a lack of well-educated engineers.
  22. """
  23. )
  24. print(results)

  1. No model was supplied, defaulted to sshleifer/distilbart-cnn-12-6 (https://huggingface.co/sshleifer/distilbart-cnn-12-6)
  2. [{'summary_text': ' America has changed dramatically during recent years . The number of engineering graduates in the U.S. has declined in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering . Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance engineering .'}]

可以看到,pipeline 自动选择了预训练好的 distilbart-cnn-12-6 模型来完成任务。与文本生成类似,我们也可以通过 max_length 或 min_length 参数来控制返回摘要的长度。 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号