做机器学习比赛的网站免费crm系统手机版
- 作者: 五速梦信息网
- 时间: 2026年04月18日 10:01
当前位置: 首页 > news >正文
做机器学习比赛的网站,免费crm系统手机版,网上商城app开发,手机网站 底部菜单文章目录 一、什么是Pipeline二、查看PipeLine支持的任务类型三、Pipeline的创建和使用3.1 根据任务类型#xff0c;直接创建Pipeline#xff0c;默认是英文模型3.2 指定任务类型#xff0c;再指定模型#xff0c;创建基于指定模型的Pipeline3.3 预先加载模型#xff0c;再… 文章目录 一、什么是Pipeline二、查看PipeLine支持的任务类型三、Pipeline的创建和使用3.1 根据任务类型直接创建Pipeline默认是英文模型3.2 指定任务类型再指定模型创建基于指定模型的Pipeline3.3 预先加载模型再创建Pipeline3.4 使用Gpu进行推理3.5 查看Device3.6 测试一下耗时3.7 确定的Pipeline的参数 四、Pipeline的背后实现 本文为 https://space.bilibili.com/21060026/channel/collectiondetail?sid1357748的视频学习笔记
项目地址为https://github.com/zyds/transformers-code 一、什么是Pipeline
将数据预处理、模型调用、结果后处理三部分组装成的流水线如下流程图使我们能够直接输入文本便获得最终的答案不需要我们关注细节 #mermaid-svg-RldLTOonQtWmbnFI {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-RldLTOonQtWmbnFI .error-icon{fill:#552222;}#mermaid-svg-RldLTOonQtWmbnFI .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RldLTOonQtWmbnFI .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-RldLTOonQtWmbnFI .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RldLTOonQtWmbnFI .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RldLTOonQtWmbnFI .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RldLTOonQtWmbnFI .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RldLTOonQtWmbnFI .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RldLTOonQtWmbnFI .marker.cross{stroke:#333333;}#mermaid-svg-RldLTOonQtWmbnFI svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RldLTOonQtWmbnFI .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-RldLTOonQtWmbnFI .cluster-label text{fill:#333;}#mermaid-svg-RldLTOonQtWmbnFI .cluster-label span{color:#333;}#mermaid-svg-RldLTOonQtWmbnFI .label text,#mermaid-svg-RldLTOonQtWmbnFI span{fill:#333;color:#333;}#mermaid-svg-RldLTOonQtWmbnFI .node rect,#mermaid-svg-RldLTOonQtWmbnFI .node circle,#mermaid-svg-RldLTOonQtWmbnFI .node ellipse,#mermaid-svg-RldLTOonQtWmbnFI .node polygon,#mermaid-svg-RldLTOonQtWmbnFI .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RldLTOonQtWmbnFI .node .label{text-align:center;}#mermaid-svg-RldLTOonQtWmbnFI .node.clickable{cursor:pointer;}#mermaid-svg-RldLTOonQtWmbnFI .arrowheadPath{fill:#333333;}#mermaid-svg-RldLTOonQtWmbnFI .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RldLTOonQtWmbnFI .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RldLTOonQtWmbnFI .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-RldLTOonQtWmbnFI .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-RldLTOonQtWmbnFI .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RldLTOonQtWmbnFI .cluster text{fill:#333;}#mermaid-svg-RldLTOonQtWmbnFI .cluster span{color:#333;}#mermaid-svg-RldLTOonQtWmbnFI div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RldLTOonQtWmbnFI :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;} ToKenizer Model PostProcessing Raw text Input IDs Logits Predictions #mermaid-svg-2HqHscPx4X3cMubR {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-2HqHscPx4X3cMubR .error-icon{fill:#552222;}#mermaid-svg-2HqHscPx4X3cMubR .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2HqHscPx4X3cMubR .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-2HqHscPx4X3cMubR .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2HqHscPx4X3cMubR .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2HqHscPx4X3cMubR .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2HqHscPx4X3cMubR .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2HqHscPx4X3cMubR .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2HqHscPx4X3cMubR .marker.cross{stroke:#333333;}#mermaid-svg-2HqHscPx4X3cMubR svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2HqHscPx4X3cMubR .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-2HqHscPx4X3cMubR .cluster-label text{fill:#333;}#mermaid-svg-2HqHscPx4X3cMubR .cluster-label span{color:#333;}#mermaid-svg-2HqHscPx4X3cMubR .label text,#mermaid-svg-2HqHscPx4X3cMubR span{fill:#333;color:#333;}#mermaid-svg-2HqHscPx4X3cMubR .node rect,#mermaid-svg-2HqHscPx4X3cMubR .node circle,#mermaid-svg-2HqHscPx4X3cMubR .node ellipse,#mermaid-svg-2HqHscPx4X3cMubR .node polygon,#mermaid-svg-2HqHscPx4X3cMubR .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2HqHscPx4X3cMubR .node .label{text-align:center;}#mermaid-svg-2HqHscPx4X3cMubR .node.clickable{cursor:pointer;}#mermaid-svg-2HqHscPx4X3cMubR .arrowheadPath{fill:#333333;}#mermaid-svg-2HqHscPx4X3cMubR .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2HqHscPx4X3cMubR .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2HqHscPx4X3cMubR .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-2HqHscPx4X3cMubR .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-2HqHscPx4X3cMubR .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2HqHscPx4X3cMubR .cluster text{fill:#333;}#mermaid-svg-2HqHscPx4X3cMubR .cluster span{color:#333;}#mermaid-svg-2HqHscPx4X3cMubR div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2HqHscPx4X3cMubR :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;} 我觉得不太行 101, 2769, 6230, 2533, 679, 1922, 6121, 8013, 102 0.9736, 0.0264 Positive:0.9736 二、查看PipeLine支持的任务类型
from transformers.pipelines import SUPPORTED_TASKS
from pprint import pprint
for k, v in SUPPORTED_TASKS.items():print(k, v)输出但其概念PipeLine支持的任务类型以及可以调用的 举例输出
audio-classification {impl: class transformers.pipelines.audio_classification.AudioClassificationPipeline, tf: (), pt: (class transformers.models.auto.modeling_auto.AutoModelForAudioClassification,), default: {model: {pt: (superb/wav2vec2-base-superb-ks, 372e048)}}, type: audio}key: 任务的名称如音频分类v关于任务的实现如具体哪个Pipeline有没有TF模型有没有pytorch模型 模型具体是哪一个
三、Pipeline的创建和使用
3.1 根据任务类型直接创建Pipeline默认是英文模型
from transformers import pipeline
pipe pipeline(text-classification) # 根据pipeline直接创建一个任务类
pipe(very good) # 测试一个句子输出结果3.2 指定任务类型再指定模型创建基于指定模型的Pipeline
注这里我已经将模型离线下载到本地了
https://huggingface.co/models
pipe pipeline(text-classification, model./models/roberta-base-finetuned-dianping-chinese)3.3 预先加载模型再创建Pipeline rom transformers import AutoModelForSequenceClassification, AutoTokenizer# 这种方式必须同时指定model和tokenizer model AutoModelForSequenceClassification.from_pretrained(./models_roberta-base-finetuned-dianping-chinese) tokenizer AutoTokenizer.from_pretrained(./models_roberta-base-finetuned-dianping-chinese) pipe pipeline(text-classification, modelmodel, tokenizertokenizer)3.4 使用Gpu进行推理 pipe pipeline(text-classification, model./models_roberta-base-finetuned-dianping-chinese, device0)3.5 查看Device pipe.model.device3.6 测试一下耗时 import torch import time times [] for i in range(100):torch.cuda.synchronize()start time.time()pipe(我觉得不太行)torch.cuda.synchronize()end time.time()times.append(end - start) print(sum(times) / 100)3.7 确定的Pipeline的参数
先创建一个pipeline
qa_pipe pipeline(question-answering, model../../models/models) qa_pipe输出 QuestionAnsweringPipeline 查看定义会告诉我们这个pipeline该如何使用 class QuestionAnsweringPipeline(ChunkPipeline):Question Answering pipeline using any ModelForQuestionAnswering. See the question answeringexamples for more information.Example:python from transformers import pipeline oracle pipeline(modeldeepset/roberta-base-squad2) oracle(questionWhere do I live?, contextMy name is Wolfgang and I live in Berlin){score: 0.9191, start: 34, end: 40, answer: Berlin}Learn more about the basics of using a pipeline in the pipeline tutorialThis question answering pipeline can currently be loaded from [pipeline] using the following task identifier:question-answering.The models that this pipeline can use are models that have been fine-tuned on a question answering task. See theup-to-date list of available models onhuggingface.co/models.进入pipeline看call查看可以支持的更多的参数 列出了更多的参数 def call(self, *args, **kwargs):Answer the question(s) given as inputs by using the context(s).Args:args ([SquadExample] or a list of [SquadExample]):One or several [SquadExample] containing the question and context.X ([SquadExample] or a list of [SquadExample], optional):One or several [SquadExample] containing the question and context (will be treated the same way as ifpassed as the first positional argument).data ([SquadExample] or a list of [SquadExample], optional):One or several [SquadExample] containing the question and context (will be treated the same way as ifpassed as the first positional argument).question (str or List[str]):One or several question(s) (must be used in conjunction with the context argument).context (str or List[str]):One or several context(s) associated with the question(s) (must be used in conjunction with thequestion argument).topk (int, optional, defaults to 1):The number of answers to return (will be chosen by order of likelihood). Note that we return less thantopk answers if there are not enough options available within the context.doc_stride (int, optional, defaults to 128):If the context is too long to fit with the question for the model, it will be split in several chunkswith some overlap. This argument controls the size of that overlap.max_answer_len (int, optional, defaults to 15):The maximum length of predicted answers (e.g., only answers with a shorter length are considered).max_seq_len (int, optional, defaults to 384):The maximum length of the total sentence (context question) in tokens of each chunk passed to themodel. The context will be split in several chunks (using doc_stride as overlap) if needed.max_question_len (int, optional, defaults to 64):The maximum length of the question after tokenization. It will be truncated if needed.handle_impossible_answer (bool, optional, defaults to False):Whether or not we accept impossible as an answer.align_to_words (bool, optional, defaults to True):Attempts to align the answer to real words. Improves quality on space separated langages. Might hurt onnon-space-separated languages (like Japanese or Chinese)Return:A dict or a list of dict: Each result comes as a dictionary with the following keys:- score (float) – The probability associated to the answer.- start (int) – The character start index of the answer (in the tokenized version of the input).- end (int) – The character end index of the answer (in the tokenized version of the input).- answer (str) – The answer to the question.如下面的例子 我们输出问题中国的首都是哪里 给的上下文是中国的首都是北京 qa_pipe(question中国的首都是哪里, context中国的首都是北京)如果通过 max_answer_len参数来限定输出的最大长度会进行强行截断 qa_pipe(question中国的首都是哪里, context中国的首都是北京, max_answer_len1)四、Pipeline的背后实现 step1 初始化组件Tokenizermodel
step1 初始化tokenizer model
tokenizer AutoTokenizer.from_pretrained(../../models/models_roberta-base-finetuned-dianping-chinese) model AutoModelForSequenceClassification.from_pretrained(../../models/models_roberta-base-finetuned-dianping-chinese)step2 预处理
预处理返回pytorch的tensor是一个dict
input_text 我觉得不太行 inputs tokenizer(input_text, return_tensorspt) inputsstep3 模型预测 res model(**inputs) res预测的结果包括的内容有点多如loss,logits等 step4 结果后处理 logits res.logits logits torch.softmax(logits, dim-1) pred torch.argmax(logits).item() result model.config.id2label.get(pred) result
- 上一篇: 做机器设备的网站网页游戏开服表送首充
- 下一篇: 做机械产品用什么网站安阳
