做视频有赚钱的网站有哪些,网页设计教程,seo文章是什么意思,河北大良网站建设1 介绍
pipeline() 是使用预训练模型进行推理的最简单和最快速的方式。可以针对不同模态的许多任务直接使用 pipeline() 2 举例#xff1a;情感分析
2.1 创建pipeline实例
from transformers import pipelineclassifier pipeline(sentiment-analysis)
#首先创…1 介绍
pipeline() 是使用预训练模型进行推理的最简单和最快速的方式。可以针对不同模态的许多任务直接使用 pipeline() 2 举例情感分析
2.1 创建pipeline实例
from transformers import pipelineclassifier 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 使用别的模型和分词器
pipeline() 可以容纳来自 Models - Hugging Face的任何模型eg:能够处理法语文本的模型
以下两种方式都可以
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipelinemodel_name nlptown/bert-base-multilingual-uncased-sentimentclassifier pipeline(sentiment-analysis, modelmodel_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, AutoModelForSequenceClassificationmodel_name nlptown/bert-base-multilingual-uncased-sentimentmodel AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer AutoTokenizer.from_pretrained(model_name)classifier pipeline(sentiment-analysis, modelmodel, tokenizertokenizer)classifier(Nous sommes très heureux de vous présenter la bibliothèque Transformers.)
#[{label: 5 stars, score: 0.7236300706863403}]4 device相关
使用 devicen——pipeline会自动将模型放置在指定的设备上
classifier pipeline(sentiment-analysis, modelmodel_name,device2)
如果模型对于单个 GPU 来说太大并且使用 PyTorch——可以设置 device_mapauto以自动确定如何加载和存储模型权重
classifier pipeline(sentiment-analysis, modelmodel_name,device_mapauto)
如果传递了 device_mapauto在实例化pipeline时不需要添加参数 devicedevice