任务总结


任务摘要

此页面显示了使用库时最常见的用例。可用的模型允许许多不同的配置,并且在用例中有很大的通用性。这里展示了最简单的方法,展示了问题回答、序列分类、命名实体识别等任务的用法。

这些示例利用 auto-models,这些类将根据给定的检查点实例化一个模型,并自动选择正确的模型体系结构。有关更多信息,请查看 AutoModel 文档。您可以随意修改代码,使其更加具体,并根据您的具体用例对其进行调整。

为了使模型能够很好地执行任务,必须从与该任务相对应的检查点加载模型。这些检查点通常是针对大量数据进行预先训练,并针对特定任务进行微调。这意味着:

并非所有的模型都对所有任务进行了微调。如果希望对特定任务的模型进行微调,可以利用示例目录中的 run_$TASK.py 脚本之一。微调模型针对特定的数据集进行微调。此数据集可能与您的用例和域重叠,也可能不重叠。正如前面提到的,您可以利用示例脚本来微调您的模型,或者您可以创建自己的培训脚本。

为了对一个任务进行推理,该库提供了几种机制:

  • 管道: 非常易于使用的抽象,只需要两行代码。
  • 直接模型使用: 更少的抽象,但通过直接访问 tokenizer (PyTorch/TensorFlow)和完整的推理能力,可以获得更多的灵活性和功能。

这里展示了这两种方法。

这里介绍的所有任务都利用了针对具体任务进行微调的预先培训的检查点。加载一个没有针对特定任务进行微调的检查点将只加载基本变压器层,而不加载用于该任务的额外磁头,随机初始化该磁头的权重。这将产生随机输出。

序列分类

序列分类是根据给定数量的类对序列进行分类的任务。序列分类的一个例子是 GLUE 数据集,它完全基于该任务。如果您想在 GLUE 序列分类任务上微调模型,可以利用 run_GLUE.py、 run_tf_GLUE.py、 run_tf_text_classification.py 或 run_xnli.py 脚本。

这里有一个使用管道进行情绪分析的例子: 识别一个序列是正的还是负的。它利用 sst2上的一个经过优化的模型,这是一个 GLUE 任务。
这将返回一个标签(“ POSITIVE”或“ NEGATIVE”)和一个分数,如下所示:

from transformers import pipeline
classifier = pipeline("sentiment-analysis")
result = classifier("I hate you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
label: NEGATIVE, with score: 0.9991result = classifier("I love you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
label: POSITIVE, with score: 0.9999

下面是一个使用模型进行序列分类的示例,以确定两个序列是否相互转述。整个过程如下:

  1. 打印结果
    Pytorch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torchtokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")classes = ["not paraphrase", "is paraphrase"]sequence_0 = "The company HuggingFace is based in New York City"sequence_1 = "Apples are especially bad for your health"sequence_2 = "HuggingFace's headquarters are situated in Manhattan"# The tokenizer will automatically add any model specific separators (i.e. <CLS> and <SEP>) and tokens to# the sequence, as well as compute the attention masks.paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="pt")
not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="pt")paraphrase_classification_logits = model(**paraphrase).logits
not_paraphrase_classification_logits = model(**not_paraphrase).logitsparaphrase_results = torch.softmax(paraphrase_classification_logits, dim=1).tolist()[0]
not_paraphrase_results = torch.softmax(not_paraphrase_classification_logits, dim=1).tolist()[0]# Should be paraphrasefor i inrange(len(classes)):
... print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
not paraphrase: 10%
is paraphrase: 90%# Should not be paraphrasefor i inrange(len(classes)):
... print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")
not paraphrase: 94%
is paraphrase: 6%

TensorFlow

from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
import tensorflow as tftokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")
model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")classes = ["not paraphrase", "is paraphrase"]sequence_0 = "The company HuggingFace is based in New York City"sequence_1 = "Apples are especially bad for your health"sequence_2 = "HuggingFace's headquarters are situated in Manhattan"# The tokenizer will automatically add any model specific separators (i.e. <CLS> and <SEP>) and tokens to# the sequence, as well as compute the attention masks.paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="tf")
not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="tf")paraphrase_classification_logits = model(paraphrase).logits
not_paraphrase_classification_logits = model(not_paraphrase).logitsparaphrase_results = tf.nn.softmax(paraphrase_classification_logits, axis=1).numpy()[0]
not_paraphrase_results = tf.nn.softmax(not_paraphrase_classification_logits, axis=1).numpy()[0]# Should be paraphrasefor i inrange(len(classes)):
... print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
not paraphrase: 10%
is paraphrase: 90%# Should not be paraphrasefor i inrange(len(classes)):
... print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")
not paraphrase: 94%
is paraphrase: 6%

提取性问题回答

提取性问题回答的任务是从给定的问题的文本中提取答案。问题回答数据集的一个例子是小队数据集,它完全基于该任务。如果您想在一个 SQuAD 任务上微调一个模型,那么您可以利用 run_qa.py 并运行_tf_squade.py 脚本。

下面是一个使用管道进行问答的示例: 从给定的问题的文本中提取答案。它利用了对 SQuAD 进行了优化的模型。

from transformers import pipeline
question_answerer = pipeline("question-answering")
context = r"""
... Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a
... question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune
... a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script.
... """

这将返回一个从文本中提取的答案,一个置信度评分,以及“开始”和“结束”值,这两个值是文本中提取的答案的位置。

result = question_answerer(question="What is extractive question answering?", context=context)
print(
... f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}"... )
Answer: 'the task of extracting an answer from a text given a question', score: 0.6177, start: 34, end: 95result = question_answerer(question="What is a good example of a question answering dataset?", context=context)
print(
... f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}"... )
Answer: 'SQuAD dataset', score: 0.5152, start: 147, end: 160

下面是一个使用模型和标记器回答问题的例子,过程如下:

  1. 打印结果
    Pytorch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torchtokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")text = r"""
... 												

抱抱脸(hugging face)教程-中文翻译-任务总结相关推荐

  1. 抱抱脸(hugging face)教程-中文翻译-模型概要

    模型概要 这是一个总结的模型可在Transformers.假设您熟悉最初的Transformers模型.或者温柔的介绍,看看有注释的Transformers.在我们关注模特之间的高度差异之前.你可以在 ...

  2. 抱抱脸(hugging face)教程-中文翻译-分享一个模型

    分享一个模型 最后两个教程展示了如何使用 PyTorch. Keras 和 Accelerate 优化分布式设置的模型.下一步就是把你的模型公之于众!我们相信公开分享知识和资源,使人工智能大众化.我们 ...

  3. 抱抱脸(hugging face)教程-中文翻译-预处理

    预处理 在您可以在模型中使用数据之前,需要将数据处理为模型可接受的格式.模型不理解原始文本.图像或音频.这些输入需要转换成数字并组装成张量.在本教程中,您将: 用tokenizer处理文本. 用特征提 ...

  4. 抱抱脸(hugging face)教程-中文翻译-创建一个自定义架构

    创建一个自定义架构 AutoClass 自动推导模型架构,并下载预先训练的配置和权重.通常,我们建议使用 AutoClass 生成与检查点无关的代码.但是,想要更多地控制特定模型参数的用户可以从几个基 ...

  5. MMpose 教程中文翻译-tutorial 0:学习配置文件

    这是一个对mmpose docs的中文翻译,自己在阅读的时候整理的,后续会继续翻译tutorial的内容.欢饮大佬们提建议,我也只是个学习中的小菜鸡 以下是mmpose教程链接 教程 0:学习 配置文 ...

  6. CMake官方教程中文翻译

    看见一博主翻译的一篇官方cmke教程,觉得很不错就转载并稍作一些细小修改,我提供的3.16版本的文档是最新的,博主之前翻译的是3.7的,内容比3.16少一点点,想看3.16完整内容,下面也有链接. 提 ...

  7. 《Swift编程语言教程》中文翻译及读书笔记page21

    <The Swift Programming Language>中文翻译及读书笔记,附件中为英文原版教程 因21页之前内容和技术关系不大,不做翻译整理,从第21页开始 第21页 1 swi ...

  8. LAN8742 教程(2) 数据手册 中文翻译(2)

    LAN8742 教程(2) 数据手册 中文翻译(2) LAN8742 教程(1) 数据手册 中文翻译(1) LAN8742 教程(2) 数据手册 中文翻译(2) 文章目录 LAN8742 教程(2) ...

  9. LAN8742 教程(1) 数据手册 中文翻译

    LAN8742 教程(1) 数据手册 中文翻译 LAN8742 教程(1) LAN8742 教程(1) 数据手册 中文翻译 文章目录 LAN8742 教程(1) 数据手册 中文翻译 前言 1.0 介绍 ...

最新文章

  1. mysql 安装dso命令_使用tengine DSO 来动态编译安装第三方模块(Lua
  2. oracle 取今日0时,Oracle 取得当天0时0分0秒和23时59分59秒
  3. 百练OJ:1013:Counterfeit Dollar(假币)
  4. Ubuntu下添加开机启动脚本
  5. 网站不大但加载很慢怎么优化_博客网站首页加载优化
  6. python flask框架教程_Flask框架从入门到实战
  7. springboot-RequestMappingHandlerMapping
  8. python中location_使用python请求模块时的LocationValueError
  9. RDS 设置 group_concat的长度限制 1024 改为 102400
  10. [摘录]第2章 中场谈判技巧
  11. 「极点日历」小程序插件
  12. InstallShield Build错误:Internal build error 6041
  13. Java 生成随机中文、英文姓名(上)
  14. Ubuntu 18.04 桌面卡死
  15. 阿里云国际版如何将ECS云服务器中的数据备份到本地
  16. Python 处理一对多考勤表
  17. Mat对象使用at<uchar>报错
  18. C++学习笔记(更新)
  19. 主机调优20141226
  20. Hi Generator

热门文章

  1. Boss直聘使用技巧 – 求职面试 – 被面技巧
  2. 粉丝不足5万的公众号,怎么在60秒内变现?
  3. 副业案例一:变现100万美元的国外网课销售漏斗模型案例
  4. string.Format()之格式化数值类型数据
  5. iOS中开发者账号分类
  6. MySQL 的统计直方图
  7. 如何处理高并发和单点故障
  8. 计算机网络 有效数据率,在计算机网络中,表征数据传输有效性的指标是( ) A.误码率 B.频带利用率 C.信道容量 D.传输速率...
  9. Multisim学习(一)电路的绘制
  10. 如何判断电脑电源故障