NSDT工具推荐Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

HuggingFace transformers 是一个整合了跨语言、视觉、音频和多模式模态与最先进的预训练模型并且提供用户友好的 API 的AI开发库。 它由 170 多个预训练模型组成,支持 PyTorch、TensorFlow 和 JAX 等框架,能够在代码之间进行互操作。 这个库还易于部署,因为它允许将模型转换为 ONNX 和 TorchScript 格式。

在这篇文章中,我们将特别探讨可轻松用于推理的transformer的管道(pipeline)功能。 管道提供复杂代码的抽象,并为文本摘要、问答、命名实体识别、文本生成和文本分类等多项任务提供简单的 API。 这些 API 最好的一点是,从预处理到模型评估的所有任务都可以只用几行代码来执行,而不需要大量的计算资源。

现在,让我们开始吧!

第一步是使用以下命令安装 transformers 包:

pip install transformers

接下来,我们将使用流水线管道结构来实现不同的任务:

from transformers import pipeline

管道允许指定多个参数,例如任务、模型、设备、批量大小和其他任务特定参数。

让我们从第一个任务开始。

1、文本摘要

这个任务的输入是一个文本语料库,模型将根据参数中提到的预期长度输出它的摘要。 在这里,我们将最小长度保持为 5,将最大长度保持为 30。

summarizer = pipeline(
    "summarization", model="t5-base", tokenizer="t5-base", framework="tf"
)

input = "Parents need to know that Top Gun is a blockbuster 1980s action thriller starring Tom Cruise that's chock full of narrow escapes, chases, and battles. But there are also violent and upsetting scenes, particularly the death of a main character, which make it too intense for younger kids. There's also one graphic-for-its-time sex scene (though no explicit nudity) and quite a few shirtless men in locker rooms and, in one iconic sequence, on a beach volleyball court. Winning is the most important thing to all the pilots, who try to intimidate one another with plenty of posturing and banter -- though when push comes to shove, loyalty and friendship have important roles to play, too. While sexism is noticeable and almost all characters are men, two strong women help keep some of the objectification in check."

summarizer(input, min_length=5, max_length=30)

输出如下:

[
    {
        "summary_text": "1980s action thriller starring Tom Cruise is chock-full of escapes, chases, battles "
    }
]

还可以从针对摘要任务进行微调的模型的其他选项中进行选择 - bart-large-cnn、t5-small、t5-large、t5-3b、t5-11b。 可以在此处查看可用模型的完整列表。

2、问答任务

在这个任务中,我们提供了一个问题和一个上下文。 该模型将根据最高概率得分从上下文中选择答案。 它还提供文本的开始和结束位置。

qa_pipeline = pipeline(model="deepset/roberta-base-squad2")

qa_pipeline(
    question="Where do I work?",
    context="I work as a Data Scientist at a lab in University of Montreal. I like to develop my own algorithms.",
)

输出如下:

{
    "score": 0.6422629356384277,
    "start": 39,
    "end": 61,
    "answer": "University of Montreal",
}

请参阅此处查看问答任务可用模型的完整列表。

3、命名实体识别

命名实体识别处理基于人名、组织名、位置名等的词的识别和分类。 输入基本上是一个句子,模型将确定命名实体及其类别及其在文本中的相应位置。

ner_classifier = pipeline(
    model="dslim/bert-base-NER-uncased", aggregation_strategy="simple"
)
sentence = "I like to travel in Montreal."
entity = ner_classifier(sentence)
print(entity)

输出如下:

[
    {
        "entity_group": "LOC",
        "score": 0.9976745,
        "word": "montreal",
        "start": 20,
        "end": 28,
    }
]

此处查看可用模型的其他选项。

4、词性标注

PoS 标记可用于对文本进行分类并提供其相关词性,例如一个词是否是名词、代词、动词等。 该模型返回 PoS 标记的单词及其概率分数和各自的位置。

pos_tagger = pipeline(
    model="vblagoje/bert-english-uncased-finetuned-pos",
    aggregation_strategy="simple",
)
pos_tagger("I am an artist and I live in Dublin")

输出如下:

[
    {
        "entity_group": "PRON",
        "score": 0.9994804,
        "word": "i",
        "start": 0,
        "end": 1,
    },
    {
        "entity_group": "VERB",
        "score": 0.9970591,
        "word": "live",
        "start": 2,
        "end": 6,
    },
    {
        "entity_group": "ADP",
        "score": 0.9993111,
        "word": "in",
        "start": 7,
        "end": 9,
    },
    {
        "entity_group": "PROPN",
        "score": 0.99831414,
        "word": "dublin",
        "start": 10,
        "end": 16,
    },
]

5、文本分类

我们将执行情感分析并根据语气对文本进行分类。

text_classifier = pipeline(
    model="distilbert-base-uncased-finetuned-sst-2-english"
)
text_classifier("This movie is horrible!")

输出如下:

[{'label': 'NEGATIVE', 'score': 0.9997865557670593}]

让我们再举几个例子。

text_classifier("I loved the narration of the movie!")

输出如下:

[{'label': 'POSITIVE', 'score': 0.9998612403869629}] 

可以在此处找到完整的文本分类模型列表。

6、文本生成

text_generator = pipeline(model="gpt2")
text_generator("If it is sunny today then ", do_sample=False)

输出如下:

[
    {
        "generated_text": "If it is sunny today then \xa0it will be cloudy tomorrow."
    }
]

此处访问文本生成模型的完整列表。

7、文本翻译

在这里,我们会将文本的语言从一种语言翻译成另一种语言。 例如,我们选择了从英语到法语的翻译。 我们使用了基本的 t5-small 模型,但你可以在此处访问其他高级模型。

en_fr_translator = pipeline("translation_en_to_fr", model='t5-small')
en_fr_translator("Hi, How are you?")

输出如下:

[{'translation_text': 'Bonjour, Comment êtes-vous ?'}]

原文链接:Simple NLP Pipelines with HuggingFace Transformers

BimAnt翻译整理,转载请标明出处