huggingface 笔记:pipeline

1 介绍 

  • pipeline() 是使用预训练模型进行推理的最简单和最快速的方式。
  • 可以针对不同模态的许多任务直接使用 pipeline()

2 举例:情感分析

2.1 创建pipeline实例

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
#首先创建一个 pipeline() 实例,并指定您想要使用它的任务
#pipeline() 将下载并缓存情感分析的默认预训练模型和分词器

2.2 使用pipeline实例 

然后就可以在目标文本上使用分类器了:

# 单个文本
classifier('Today is a shiny day!')
'''
[{'label': 'POSITIVE', 'score': 0.9992596507072449}]
'''
# 多个文本
classifier(['Today is a shiny day!','What a bad day it is!'])
'''
[{'label': 'POSITIVE', 'score': 0.9992596507072449},
 {'label': 'NEGATIVE', 'score': 0.999808132648468}]
'''

3 使用别的模型和分词器

以下两种方式都可以

from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline

model_name = "nlptown/bert-base-multilingual-uncased-sentiment"



classifier = pipeline("sentiment-analysis", model=model_name)


classifier("Nous sommes très heureux de vous présenter la bibliothèque Transformers.")

#[{'label': '5 stars', 'score': 0.7236300706863403}]
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_name = "nlptown/bert-base-multilingual-uncased-sentiment"

model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)



classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

classifier("Nous sommes très heureux de vous présenter la bibliothèque Transformers.")
#[{'label': '5 stars', 'score': 0.7236300706863403}]

4 device相关

  • 使用 device=n——>pipeline会自动将模型放置在指定的设备上
classifier = pipeline("sentiment-analysis", model=model_name,device=2)
  • 如果模型对于单个 GPU 来说太大,并且使用 PyTorch——>可以设置 device_map="auto",以自动确定如何加载和存储模型权重
classifier = pipeline("sentiment-analysis", model=model_name,device_map="auto")
  • 如果传递了 device_map="auto",在实例化pipeline时不需要添加参数 device=device

5 batch相关

  • 默认情况下,pipeline不会进行批量推理。
  • 原因是批处理不一定更快,实际上在某些情况下可能会更慢。
  • 如果真的需要用的话,可以是:
classifier = pipeline("sentiment-analysis", 
                      "nlptown/bert-base-multilingual-uncased-sentiment",
                     batch_size=2)

6 量化技术降低精度

  • 可以通过使用“量化”技术将精度降低至 16 位以下,这是一种有损压缩模型权重的方法
  • 允许将每个参数压缩至 8 位、4 位
  • ***在 4 位时,模型的输出可能会受到负面影响***
from transformers import pipeline, BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(load_in_8bit=True)  
# 也可以尝试 load_in_4bit
pipe = pipeline("text-generation",
                 "meta-llama/Meta-Llama-3-8B-Instruct",
                 device_map="auto", 
                 model_kwargs={"quantization_config": quantization_config})

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值