当前位置:   article > 正文

Huggingface Transformers库学习笔记(二):使用Transformers(上)(Using Transformers Part 1)_transformer的run_clm是干啥的

transformer的run_clm是干啥的

前言

本部分是Transformer库的基础部分的上半部分,主要包括任务汇总、模型汇总和数据预处理三方面内容,由于许多模型我也不太了解,所以多为机器翻译得到,错误再所难免,内容仅供参考。

Huggingface Transformers库学习笔记(二):使用Transformers(Using Transformers Part 1)

使用Transformers(Using Transformers)

任务汇总(Summary of the tasks)

该部分介绍了使用该库时最常见的用例。可用的模型允许许多不同的配置,并且在各个实际用例中具有很大的通用性。可用的模型允许许多不同的配置,并且在用例中具有很大的通用性。

这些例子利用了auto-models,这些类将根据给定的checkpoint实例化一个模型,自动地选择正确的模型架构。

为了让模型在任务上良好地执行,它必须从与该任务对应的checkpoint加载。这些checkpoint通常针对大量数据进行预先训练,并针对特定任务进行微调。
这意味着以下内容

  • 并不是所有的模型都对所有的任务进行了微调。如果想对特定任务的模型进行微调,可以利用示例目录中的run_$ task .py脚本之一。
  • 微调模型是在特定数据集上进行微调的。这个数据集可能与我们要做的用例和域重叠,也可能不重叠。如前所述,可以利用示例脚本来微调模型,或者可以创建自己的训练脚本。

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

  • Pipelines: 非常容易使用的抽象,只需要两行代码。
  • 直接使用模型:抽象较少,但通过直接访问分词器(PyTorch/TensorFlow)和充分的推理能力,更灵活和强大。

下面的具体应用中展示了这两种方法。

序列分类(Sequence Classification)

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

下面是一个使用Pipeline进行情感分析的例子:识别一个序列是积极的还是消极的。它在sst2上利用了一个经过微调的模型,这是一个GLUE任务。

这将在分数旁边返回一个标签(正的或负的),如下所示

from transformers import pipeline
nlp = pipeline("sentiment-analysis")
result = nlp("I hate you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
result = nlp("I love you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出为

label: NEGATIVE, with score: 0.9991
label: POSITIVE, with score: 0.9999
  • 1
  • 2

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

  1. 从checkpoint名称实例化分词器和模型。该模型被识别为BERT模型,并将存储在checkpoint中的权值加载到该模型中。
  2. 从这两个句子构建一个序列,使用正确的特定于模型的分隔符、token类型id和注意掩码(encode()和__call__()负责这一点)。
  3. 将这个序列传递到模型中,以便将其分类为两个可用类中的一个:0(不是释义)和1(是释义)。
  4. 计算结果的softmax以得到所有类的概率。
  5. 打印结果。

相关代码如下:

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"

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 paraphrase
for i in range(len(classes)):
    print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
print("---"*24)
# Should not be paraphrase
for i in range(len(classes)):
    print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")
  • 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
  • 27

得到输出如下

not paraphrase: 10%
paraphrase: 90%
------------------------------------------------------------------------
not paraphrase: 94%
paraphrase: 6%
  • 1
  • 2
  • 3
  • 4
  • 5

提取式问答(Extractive Question Answering)

提取式问答是指从给定的文本中提取一个答案的任务。关于QA数据集的一个例子是SQuAD数据集,它完全基于该任务。如果想对一个任务的模型在SQuAD数据集上进行微调,可以使用run_qa.py和run_tf_team .py脚本。

下面是一个使用pipeline进行问题回答的示例:从给定问题的文本中提取答案。它利用了一个在SQuAD上微调过的模型。

from transformers import pipeline
nlp = 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/question-answering/run_squad.py script.
"""

result = nlp(question="What is extractive question answering?", context=context)
print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")
print("---"*24)
result = nlp(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']}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出如下

Answer: 'the task of extracting an answer from a text given a question', score: 0.6226, start: 34, end: 95
------------------------------------------------------------------------
Answer: 'SQuAD dataset', score: 0.5053, start: 147, end: 160
  • 1
  • 2
  • 3

下面是一个使用模型和分词器回答问题的示例。流程如下:

  1. 从checkpoint名称实例化一个分词器和模型。该模型被识别为BERT模型,并将存储在checkpoint中的权值加载到该模型中。
  2. 定义一篇文章和一些问题。
  3. 迭代问题并从文本和当前问题构建一个序列,使用正确的特定于模型的分隔符、token类型id和attention mask。
  4. 将此序列传递给模型。这将在整个序列token(问题和文本)中输出开始位置和结束位置的分数范围。
  5. 计算结果的softmax以获得token上的概率。
  6. 从标识的start和stop值中获取token,将这些token转换为字符串。
  7. 打印结果。

相关代码如下:


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/小蓝xlanll/article/detail/345517
推荐阅读
相关标签