基于Hugging Face官方文档编写,只需要以下几个入门门槛:
- Python基础
- 懂什么是训练集,什么是测试集
如果你会以上内容,请放心进入transformer的学习,如果你有良好的英语水平,可以直接从官方文档入手(https://huggingface.co/learn/nlp-course)
Transformer可以做什么?
Transformer经常用于处理NLP问题,包括以下几种常见任务:
-
对整个句子进行分类:
- 获取评论的情感倾向
- 检测电子邮件是否为垃圾邮件
- 判断句子是否语法正确
- 判断两个句子是否逻辑相关
-
对句子中的每个单词进行分类:
- 识别句子的语法组成部分(名词、动词、形容词等)
- 识别命名实体(人名、地名、组织机构等)
-
生成文本内容:
- 根据提示自动生成文本
- 填充文本中的空白单词
- 生成摘要或总结
-
从文本中提取答案:
- 在给定问题和上下文的情况下,从上下文中提取答案
- 基于上下文提供的信息回答问题
-
根据输入文本生成新的句子:
- 将文本翻译成另一种语言
- 对文本进行摘要或概括
Transformer是怎么做到的?
首先,让我们从Transformer的第一个方法入手:pipeline()
它有以下几个工具
feature-extraction
fill-mask
ner
question-answering
sentiment-analysis
summarization
text-generation
translation
zero-shot-classification
让我们一个一个地学习
sentiment-analysis(情感分析)
输入句子,返回句子的情感
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
classifier("I've been waiting for a HuggingFace course my whole life.")
[{'label': 'POSITIVE', 'score': 0.9598047137260437}]
Zero-shot classification(标签分类)
输入句子和你想分的种类,返回各个种类的score
from transformers import pipeline
classifier = pipeline("zero-shot-classification")
classifier(
"This is a course about the Transformers library",
candidate_labels=["education", "politics", "business"],
)
{'sequence': 'This is a course about the Transformers library',
'labels': ['education', 'business', 'politics'],
'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}
Text generation(补全句子)
输入不完整的句子,返回补全后的完整句子
from transformers import pipeline
generator = pipeline("text-generation")
generator("In this course, we will teach you how to")
[{'generated_text': 'In this course, we will teach you how to understand and use '
'data flow and data interchange when handling user data. We '
'will be working with one or more of the most commonly used '
'data flows — data flows of various types, as seen by the '
'HTTP'}]