任务总结


任务摘要

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

这些示例利用 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. HDU1247Hat’s Words(字典树)
  2. eclipse如何部署到tomcat上的
  3. js 判断 浏览器 是否为 微信 浏览器
  4. 【绿色版】飞鸽传书2011绿色版
  5. 新建工程后编译运行出现,无法找到*.obj问题解决方案
  6. 20155229《网络对抗技术》Exp2:后门原理与实践
  7. C语言 文件读写 ftell 函数 - C语言零基础入门教程
  8. linux下如何实现pgadmin备份,linux下pgAdmin4安装
  9. STVP烧录出现Verify failed at address 0x1000
  10. 传感器自学笔记第三章——LM393电压比较芯片+MQ_2烟雾传感器
  11. 特征工程之特征选择(3)----F检验和互信息法
  12. 数组分割 java_分割java数组
  13. JS数组ES3-ES6常用方法
  14. Qt中文字符串按照拼音排序
  15. 计算机基本信息分别代表什么意思,视频基本信息格式不正确是什么意思
  16. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.ecli
  17. Solr Filter过滤器
  18. 迅为i.MX6ULL终结者进程基础-进程创建
  19. ​LeetCode刷题实战276:栅栏涂色
  20. 旧手机利用(flutter+声网RTC+声网RTM)

热门文章

  1. (转载)一次Linux系统被攻击的分析过程
  2. 音频PA导致的功耗超标
  3. Html随机数小程序
  4. 基于springboot+vue的水果销售系统附代码
  5. MySQL数据库监控与调优(2)
  6. JS+CSS竖向折叠滑动菜单代码
  7. 如何拆宏碁(acer)笔记本--个人动手更换风扇、清理灰尘
  8. 在Windows 7中使用搜索连接器从您的桌面搜索网站
  9. java-net-php-python-jsp网络工程师在线测试系统计算机毕业设计程序
  10. Ubuntu环境下用docker从0到1部署Elasticsearch 7集群