当前位置:   article > 正文

抱抱脸(hugging face)教程-中文翻译-任务总结_中文到法语 hugging face

中文到法语 hugging face

任务总结


任务摘要

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

这些示例利用 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

  1. 打印结果
    Pytorch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

tokenizer = 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).logits

paraphrase_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%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

TensorFlow

from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
import tensorflow as tf

tokenizer = 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).logits

paraphrase_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%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

提取性问题回答

提取性问题回答的任务是从给定的问题的文本中提取答案。问题回答数据集的一个例子是小队数据集,它完全基于该任务。如果您想在一个 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.
... """
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

  1. 打印结果
    Pytorch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch

tokenizer = 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"""
... 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/585449
推荐阅读
相关标签