文章目录
1、介绍
本篇博客内容以及后续内容均来自b站up主你可是处女座啊
什么是pipeline?
-
将数据预处理、模型调用,结果后处理三部分组装成的流水线
-
是我们能够直接输入文本就能够获得最终答案
非常类似于torch.nn中的sequencial
能够做什么?
支持任务类型:
- text-classification
- token-classification
- question-answering
- fill-mask
- translation
- text2text-generation
- text-generation
- conversatinal
- table-question-answering
- zero-shot-classification
2、代码实战
2.1、查看pipeline支持的任务类型
from transformers.pipelines import SUPPORTED_TASKS
for k,v in SUPPORTED_TASKS.items():
print(k,v['type'])
2.2、pipeline的创建与使用方式
from transformers import pipeline
#根据任务类型直接创建pipeline,默认都是英文模型
pipe = pipeline("text-classification")
pipe('very sad')
2.3、指定任务类型,指定模型创建pipeline
这里需要科学上网
pipe = pipeline("text-classification",model='uer/roberta-base-finetuned-dianping-chinese')
pipe('今天天气真好')
2.4、预先加载模型,在创建pipeline
from transformers import AutoModelForSequenceClassification,AutoTokenizer,pipeline
model = AutoModelForSequenceClassification.from_pretrained('uer/roberta-base-finetuned-dianping-chinese')
tokenizer = AutoTokenizer.from_pretrained('uer/roberta-base-finetuned-dianping-chinese')
pipe = pipeline('text-classification',model=model,tokenizer=tokenizer,device=0)
进行测试
pipe('我觉得不太行')
2.5、背后实现原理
2.5.1、导包
from transformers import AutoModelForSequenceClassification,AutoTokenizer,pipeline
model = AutoModelForSequenceClassification.from_pretrained('uer/roberta-base-finetuned-dianping-chinese')
tokenizer = AutoTokenizer.from_pretrained('uer/roberta-base-finetuned-dianping-chinese')
pipe = pipeline('text-classification',model=model,tokenizer=tokenizer)
2.5.2、对输入进行处理
input_text = "我觉得不太行"
inputs = tokenizer(input_text,return_tensors='pt')
inputs
2.5.3、获得模型输出
res = model(**inputs)
res
2.5.4、计算逻辑得分
logits = res.logits
logits = torch.softmax(logits,dim=-1)
logits
2.5.4、获得预测结果
pred = torch.argmax(logits).item()
pred
2.5.5、转化为真实标签
model.config.id2label
result = model.config.id2label.get(pred)
result