linux管道(pipeline)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux管道(pipeline)相关的知识,希望对你有一定的参考价值。

参考技术A 管道就是我们生活中看到的净水,它有两个水口,一个连接着进水管,一个连接着出水管,通过这个管道,我们就可以把水流一步步过滤处理,最终输出我们想要的净水。

linux中的管道也是同样的道理,它使用|表示。

比如我们经常看到统计排序的例子

为了避免死锁并利用并行性,通常,带有一个或多个新管道的Unix进程将调用fork()创建新进程。然后,每个过程将在产生或使用任何数据之前关闭将不使用的管道末端。或者,进程可以创建一个新线程并使用管道在它们之间进行通信。

也可以使用mkfifo()或创建命名管道mknod(),然后在调用它们时将它们作为输入或输出文件呈现给程序。它们允许创建多路径管道,并且在与标准错误重定向或结合使用时特别有效。

Transformers学习笔记3. HuggingFace管道函数Pipeline

Transformers学习笔记3. HuggingFace管道函数Pipeline

一、简介

Hugging face提供了管道函数——Pipeline,可以使用极少的代码快速开启一个NLP任务。

Pipeline 具备了数据预处理、模型处理、模型输出后处理等步骤,可以直接输入原始数据,然后给出预测结果,十分方便。

给定一个任务之后,pipeline会自动调用一个预训练好的模型,然后根据你给的输入执行下面三个步骤:

  1. 预处理输入文本,让它可被模型读取
  2. 模型处理
  3. 模型输出的后处理,让预测结果可读

虽然Pipeline使用很简单,但对于专业人士缺乏灵活性。

当前在下面网址查到当前有效的Pipeline:
https://huggingface.co/docs/transformers/main_classes/pipelines

本文介绍其中一些管道模型的使用。

二、一些管道模型示例

1. 情感分析

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I am happy.")

输出:

['label': 'POSITIVE', 'score': 0.9998760223388672]

也可以传列表作为参数。

2. 零样本文本分类

from transformers import pipeline

classifier = pipeline("zero-shot-classification")
classifier(
    ["This is a course about the Transformers library",
        "New policy mix to propel turnaround in China's economy"],
    candidate_labels=["education", "politics", "business"],
)

['sequence': 'This is a course about the Transformers library', 'labels': ['education', 'business', 'politics'], 'scores': [0.8445969820022583, 0.11197575181722641, 0.0434272475540638], 
'sequence': "New policy mix to propel turnaround in China's economy", 'labels': ['business', 'politics', 'education'], 'scores': [0.6015452146530151, 0.348330557346344, 0.05012420192360878]]

3. 实体名称识别

from transformers import pipeline

ner = pipeline("ner", grouped_entities=True)
print(ner("My name is Sylvain and I work at Hugging Face in Brooklyn."))

输出:

['entity_group': 'PER', 'score': 0.9981694, 'word': 'Sylvain', 'start': 11, 'end': 18, 
'entity_group': 'ORG', 'score': 0.9796019, 'word': 'Hugging Face', 'start': 33, 'end': 45, 
'entity_group': 'LOC', 'score': 0.9932106, 'word': 'Brooklyn', 'start': 49, 'end': 57]

4. 摘要

from transformers import pipeline

# use bart in pytorch
summarizer = pipeline("summarization")
summarizer("Sam Shleifer writes the best docstring examples in the whole world.", min_length=5, max_length=8)


输出:

# max_length=8
['summary_text': ' Sam Shleifer writes']
# max_length=12
['summary_text': ' Sam Shleifer writes the best docstring']

5. 文本生成

from transformers import pipeline

generator = pipeline('text-generation', model='liam168/chat-DialoGPT-small-zh')
print(generator('今天早上早点到公司,', max_length=100))

6. GPT2英文文本生成

from transformers import pipeline

generator = pipeline("text-generation", model="distilgpt2")
print(generator(
    "In this course, we will teach you how to",
    max_length=30,
    num_return_sequences=2,
))

结果:

['generated_text': 'In this course, we will teach you how to write a powerful and useful resource for your students.\\n\\n\\n\\nHow can you understand your own', 
'generated_text': 'In this course, we will teach you how to program a \\u202an–n\\u202f\\u202f\\u202f and learn how to use it.']

7. 遮挡字还原

from transformers import pipeline

unmasker = pipeline('fill-mask')

print(unmasker('What the <mask>?', top_k=3))

结果:

['score': 0.378376841545105, 'token': 17835, 'token_str': ' heck', 'sequence': 'What the heck?', 
'score': 0.32931089401245117, 'token': 7105, 'token_str': ' hell', 'sequence': 'What the hell?', 
'score': 0.1464540809392929, 'token': 26536, 'token_str': ' fuck', 'sequence': 'What the fuck?']

8. 问答

from transformers import pipeline

question_answerer = pipeline("question-answering")
print(question_answerer(
    question="Where do I work?",
    context="My name is Sylvain and I work at Hugging Face in Brooklyn",
))

输出:

'score': 0.6949763894081116, 'start': 33, 'end': 45, 'answer': 'Hugging Face'

9. 翻译

一个中文到广东话翻译器:

from transformers import pipeline

translator = pipeline("translation", model="botisan-ai/mt5-translate-zh-yue")
print(translator("今天吃早饭没有?"))

输出:

['translation_text': '今日食早飯未?']

以上是关于linux管道(pipeline)的主要内容,如果未能解决你的问题,请参考以下文章

Xilinx Linux V4L2视频管道(Video Pipeline)驱动程序分析

Pipeline in scala——给scala添加管道操作

Scrapy管道(pipeline)的使用

浅谈管道模型(Pipeline)

MongoDB 聚合管道(Aggregation Pipeline)

Transformers学习笔记3. HuggingFace管道函数Pipeline