Post

pipeline(huggingface)

Pipeline

sentiment-analysis(情感分析)

1
2
3
from transformers import pipeline
classifier=pipeline("sentiment-analysis")
classifier("I've been waitting for a long time.")

输出:

1
[{'label': 'NEGATIVE', 'score': 0.9956257343292236}]

zero-shot-classification(零样本学习)

1
2
3
4
5
6
classifier=pipeline("zero-shot-classification")
classifier(
    ["A helicopter is flying in the sky",
    "A bird is flying in the sky"],# 前提
    candidate_labels=['animal','machine'] # 选项,表示各个假设
)

输出:

1
2
3
4
5
6
7
# 各个假设的概率
[{'sequence': 'A helicopter is flying in the sky',
'labels': ['machine', 'animal'], 
'scores': [0.9938627481460571, 0.006137238349765539]}, 
{'sequence': 'A bird is flying in the sky', 
'labels': ['animal', 'machine'], 
'scores': [0.9987970590591431, 0.0012029367499053478]}]

text-generation(文本生成器)

1
2
generator=pipeline("text-generation")
generator("In this course,we will teach you how to")

输出:

1
[{'generated_text': 'In this course,we will teach you how to use your smartphone device and computer to read your emails, text messages and even your calls. We also explain to you the right way to do it, using the simple steps described above.\n\nYou'}]

与特定的模型一起使用

1
2
3
4
5
6
generator=pipeline("text-generation",model="distilgpt2")
generator(
    "In this course,we will teach you how to",
    max_length=30, # 每句话的最大长度
    num_return_sequences=2, # 返回多少种结果(句子)
)

fill-mask(掩码填充)

1
2
unmasker=pipeline("fill-mask") #
unmasker=("This course will teach you all about <mask> models.",top_k=2)# 猜测<mask>的词语,次数为top_k

输出:

1
2
3
4
5
6
7
8
[{'score': 0.19619765877723694, 
'token': 30412, 
'token_str': ' mathematical', 
'sequence': 'This course will teach you all about mathematical models.'}, 
{'score': 0.040527235716581345, 
'token': 38163, 
'token_str': ' computational', 
'sequence': 'This course will teach you all about computational models.'}]

ner(命名实体识别)

1
2
ner=pipeline("ner",grouped_entities=True) #
ner("My name is Alice and I work at Hugging Face int Brooklyn.")

输出:

1
2
3
4
[{'entity_group': 'PER', 'score': 0.99816, 'word': 'Sylvain', 'start': 11, 'end': 18}, 
 {'entity_group': 'ORG', 'score': 0.97960, 'word': 'Hugging Face', 'start': 33, 'end': 45}, 
 {'entity_group': 'LOC', 'score': 0.99321, 'word': 'Brooklyn', 'start': 49, 'end': 57}
]

question-answering(阅读理解)

1
2
3
4
5
question_answerer = pipeline("question-answering")
question_answerer(
    question="Where do I work?",#问题
    context="My name is Sylvain and I work at Hugging Face in Brooklyn",#答案
)

输出:

1
{'score': 0.6385916471481323, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}

summarization(文本摘要)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
summarizer = pipeline("summarization")# 可以指定max_length,min_length
summarizer(
    """
    America has changed dramatically during recent years. Not only has the number of 
    graduates in traditional engineering disciplines such as mechanical, civil, 
    electrical, chemical, and aeronautical engineering declined, but in most of 
    the premier American universities engineering curricula now concentrate on 
    and encourage largely the study of engineering science. As a result, there 
    are declining offerings in engineering subjects dealing with infrastructure, 
    the environment, and related issues, and greater concentration on high 
    technology subjects, largely supporting increasingly complex scientific 
    developments. While the latter is important, it should not be at the expense 
    of more traditional engineering.

    Rapidly developing economies such as China and India, as well as other 
    industrial countries in Europe and Asia, continue to encourage and advance 
    the teaching of engineering. Both China and India, respectively, graduate 
    six and eight times as many traditional engineers as does the United States. 
    Other industrial countries at minimum maintain their output, while America 
    suffers an increasingly serious decline in the number of engineering graduates 
    and a lack of well-educated engineers.
"""
)

输出:

1
2
3
4
5
6
7
[{'summary_text': ' America has changed dramatically during recent years . The '
                  'number of engineering graduates in the U.S. has declined in '
                  'traditional engineering disciplines such as mechanical, civil '
                  ', electrical, chemical, and aeronautical engineering . Rapidly '
                  'developing economies such as China and India, as well as other '
                  'industrial countries in Europe and Asia, continue to encourage '
                  'and advance engineering .'}]

translation(翻译)

1
2
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en") #寻找对应的模型,这里是法文到英文,也可以通过指定translation_en_to_fr
translator("Ce cours est produit par Hugging Face.")

输出:

1
[{'translation_text': 'This course is produced by Hugging Face.'}]

原理

$ Tokenizer \to Model \to Postprocessing $
使用分词器将输入转换成数字序列,然后传输到模型,生成逻辑,最后将逻辑转换为结果

This post is licensed under CC BY 4.0 by the author.