关键词:
Transformers学习笔记3. HuggingFace管道函数Pipeline
一、简介
Hugging face提供了管道函数——Pipeline,可以使用极少的代码快速开启一个NLP任务。
Pipeline 具备了数据预处理、模型处理、模型输出后处理等步骤,可以直接输入原始数据,然后给出预测结果,十分方便。
给定一个任务之后,pipeline会自动调用一个预训练好的模型,然后根据你给的输入执行下面三个步骤:
- 预处理输入文本,让它可被模型读取
- 模型处理
- 模型输出的后处理,让预测结果可读
虽然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': '今日食早飯未?']
pytorch学习笔记——图像处理(transforms.normalize归一化)(代码片段)
PyTorch学习笔记——图像处理transforms.Normalize归一化回顾torchvision.ToTensor归一化transforms.Normalize公式回顾torchvision.ToTensor在看这一片博客之前,需要先浏览以下我的上一篇博客PyTorch学习笔记——图像处理(torchvision.ToTensorÿ... 查看详情
spark的transformation和action的操作学习笔记
一、spark的transformation和action区别Spark有一些基本的transformation和action的操作,其中transformation形成各类型的RDD,action不形成RDD,而是对RDD进行累加、合并、保存操作。650)this.width=650;"src="http://s3.51cto.com/wyfs02/M02/87/C7/wKioL1fh3Knhq 查看详情
transformer综述(asurveyonvisiontransformer)阅读学习笔记--transformer应用的图像处理与视频处理的研究(代码片段)
...:文章主旨:在本文中,我们回顾了这些视觉Transformer模型,将它们分为不同的任务,并分析了它们的优缺点。我们探讨的主要类别包括主干网络、高/中级视觉、低级视觉和视频处理。我们还包括有效的Transfor... 查看详情
transformers学习笔记3.huggingface管道函数pipeline(代码片段)
Transformers学习笔记3.HuggingFace管道函数Pipeline一、简介二、一些管道模型示例1.情感分析2.零样本文本分类3.实体名称识别4.摘要5.文本生成6.GPT2英文文本生成7.遮挡字还原8.问答9.翻译一、简介Huggingface提供了管道函数——Pipeline,... 查看详情
深度学习中transformer的原理和方法(李宏毅视频课笔记)
文章目录0前言1RNNtoCNN2Self-Attention2.1BaseMethod2.2MatrixRepresentation2.3Multi-headSelf-attention2.4PositionalEncoding3Seq2SeqBasedonSelf-Attention3.1BaseMethod3.2Transformer4AttentionVisualization0前言本节学 查看详情
学习笔记李宏毅2021春机器学习课程第5.1节:transformer
...q2seqmodel的整体架构4编码器的具体架构1序列到序列的模型Transformer本质上就是一个Sequence-to-sequence的model,我们经常缩写为Seq2seq,所有我们就先来讨论一下什么是Se 查看详情
《real-timerendering》第四版学习笔记——chapter4transforms
...文章:《Real-TimeRendering》第四版学习笔记——Chapter4Transforms(一)四、顶点混合顶点混合(vertexblending)是为了解决静态物体无法产生柔和的关节变换的问题。顶点混合也称为线性混合蒙皮(linear-blendskinnin... 查看详情
pytorch模型训练实用教程学习笔记:一数据加载和transforms方法总结(代码片段)
前言最近在重温Pytorch基础,然而Pytorch官方文档的各种API是根据字母排列的,并不适合学习阅读。于是在gayhub上找到了这样一份教程《Pytorch模型训练实用教程》,写得不错,特此根据它来再学习一下Pytorch。仓库地... 查看详情
保姆级解读!css属性transform变形+2d转换+3d转换实例+代码+解析——web前端系列学习笔记(代码片段)
...果图元素变形原点3D转换3D转换——案例案例代码transfromtransform,翻译成中文的含义是“改变,使…变形;转换”,用于向元素应用2D或3D转换。transform属性的基本语法如下所示:transform:none|transform-functions; 查看详情
transformers学习笔记1.一些基本概念和编码器字典(代码片段)
Transformers学习笔记1.一些基本概念和编码器、字典一、基本概念1.HuggingFace简介2.Transformers(1)简介(1)预定义模型(2)使用方法3.Datasets查看有哪些公开数据集方法1:使用datasets包的list_datasets方法方法... 查看详情
transformers学习笔记1.一些基本概念和编码器字典(代码片段)
Transformers学习笔记1.一些基本概念和编码器、字典一、基本概念1.HuggingFace简介2.Transformers(1)简介(1)预定义模型(2)使用方法3.Datasets查看有哪些公开数据集方法1:使用datasets包的list_datasets方法方法... 查看详情
dom解析器解析增删改学习笔记
importjava.io.File;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.transform.Result;importjavax.xml.transform.Source;importjavax.xml.transform.Tr 查看详情
《real-timerendering》第四版学习笔记——chapter4transforms
引言线性变换:保持向量加法和缩放操作:f(x)+f(y)=f(x+y),kf(x)=f(kx)\\mathbff(\\mathbfx)+\\mathbff(\\mathbfy)=\\mathbff(\\mathbfx+\\mathbfy),\\\\k\\mathbff(\\mathbfx)=\\mathbf 查看详情
transformer多模态综述学习笔记《multimodallearningwithtransformers:asurvey》
我的目标:调研目前transformer在多模态领域的应用,内容也以此为重点(不全文解析)。论文标题:MultimodalLearningwithTransformers:ASurvey论文地址:https://arxiv.org/abs/2206.06488作者:PengXu,XiatianZhu,andDavidA.Clifton... 查看详情
css3学习笔记二
接着是对图形移动、旋转、倾斜、放缩的处理。 -moz-transform:translateX(xdeg)translateY(xdeg);/*图形会沿着XY轴移动*/ -moz-transform:rotate(xdeg);/*图形绕着原点旋转当 查看详情
如何搭建适合时间序列预测的transformer模型?
...”图学习“等获取各个领域干货算法笔记~今天又是一篇Transformer梳理文章,这次应用场景是时间序列预测。Transformer的序列建模能力,让其天然 查看详情
如何搭建适合时间序列预测的transformer模型?
...”图学习“等获取各个领域干货算法笔记~今天又是一篇Transformer梳理文章,这次应用场景是时间序列预测。Transformer的序列建模能力,让其天然 查看详情
图学习?transformer:我也行
...0c;持续更新NLP、CV、搜推广干货笔记和业内前沿工作解读~Transformer诞生于NLP领域,目前已经成为NLP中的主力模型。同时,Transformer在CV领域也逐渐显示出其超强的能力,诸如ViT、SwinTransformer、BEiT等VisionTransformer模型验证... 查看详情