❤️觉得内容不错的话,欢迎点赞收藏加关注😊😊😊,后续会继续输入更多优质内容❤️

👉有问题欢迎大家加关注私戳或者评论(包括但不限于NLP算法相关,linux学习相关,读研读博相关......)👈

Pipeline

(封面图由文心一格生成)

深入理解Transformers库的Pipeline

在自然语言处理领域,Transformers库是一个备受欢迎的工具。这个库提供了各种各样的模型和工具,可以用于许多任务,如文本分类、命名实体识别、问答系统等。其中最为方便的工具之一就是Pipeline。

Pipeline是Transformers库的一个高级API,可以轻松地将多个处理步骤(如分词、实体识别、文本分类等)组合成一个管道,从而实现一条指令完成多个自然语言处理任务。在这篇文章中,我们将深入了解Pipeline的原理,并且通过代码演示,展示它的用法和优势。

1. Pipeline的介绍

在自然语言处理中,常常需要执行多个任务,例如将一段文本分词、去除停用词、提取关键词等。使用Pipeline可以将这些任务连接成一个完整的处理流程。Pipeline的API简单易用,可以自动选择最合适的模型,并且在处理时实现自动批处理。具体来说,Pipeline提供了一种简单的方法,可以将以下任务链接在一起:

  • 文本预处理(分词、停用词处理、标点符号去除等)
  • 特征提取(向量化、TF-IDF权重计算等)
  • 模型选择和调参
  • 输出结果(如文本分类、情感分析等)

使用Pipeline,只需要提供文本输入,即可轻松地获取输出结果。这种API非常适合快速开发原型,并且在生产环境中提供了方便的部署方式。

2. Pipeline的原理

在Pipeline内部,首先对输入文本进行预处理。这通常涉及到将文本转换成tokens序列,并且进行停用词处理、标点符号去除等操作。接着,Pipeline将序列输入到模型中,这个模型通常是由Transformer架构组成的。

对于每个任务,Pipeline都会选择一个适当的模型和适当的参数。例如,对于文本分类任务,Pipeline可能会选择一个适当的分类器,并且使用适当的参数来优化这个分类器。这个过程通常是由预先定义的管道组件组成的。

最后,Pipeline将模型的输出转换为特定任务的输出格式。例如,对于文本分类,输出可能是一个字符串,表示文本属于哪个类别。对于命名实体识别,输出可能是一个包含实体名称和实体类型的列表。

3. Pipeline的代码演示

让我们来看一个实际的例子,使用Pipeline对一些电影评论进行情感分析。

首先,我们需要安装Transformers库,这可以通过以下命令完成:

!pip install transformers
transformers
huggingface/transformers: 是一个基于 Python 的自然语言处理库,它使用了 PostgreSQL 数据库存储数据。适合用于自然语言处理任务的开发和实现,特别是对于需要使用 Python 和 PostgreSQL 数据库的场景。特点是自然语言处理库、Python、PostgreSQL 数据库。

然后,我们可以使用以下代码来加载Pipeline:

from transformers import pipeline

classifier = pipeline('sentiment-analysis')

这个Pipeline是一个情感分类器,可以根据输入文本的情感进行分类。

接下来,我们可以使用以下代码来进行测试:

text = "I love this movie, it's so good!"
result = classifier(text)

print(result)

这个代码将打印出以下输出:

[{'label': 'POSITIVE', 'score': 0.9998704795837402}]

这个输出表示,输入的文本是“积极”的,并且对应的置信度非常高。

这里的Pipeline自动选择了适当的模型和参数,以最大化分类的准确性。我们可以通过更改分类器来尝试不同的模型,例如:

classifier = pipeline('text-classification', model='distilbert-base-uncased-finetuned-sst-2-english')

这个分类器使用了一个不同的模型,并且针对情感分类任务进行了微调。

4. Pipeline的优势

Pipeline的一个优点是,它提供了一个简单易用的API,可以快速地开发原型,从而快速地检测模型的性能。另一个优点是,Pipeline自动选择适当的模型和参数,从而提高了模型的准确性。Pipeline还可以通过将多个任务组合在一起来实现复杂的自然语言处理任务。

此外,Pipeline还可以在CPU和GPU上运行,从而实现更高的处理速度。对于大型数据集,Pipeline可以自动进行批处理,从而提高了处理速度。最后,Pipeline提供了一种简单的方式,可以将模型部署到生产环境中。

5. 结论

在这篇文章中,我们深入了解了Transformers库中Pipeline的原理,并且通过代码演示,展示了它的用法和优势。Pipeline是Transformers库中的一个强大工具,可以帮助我们快速开发原型,并且在生产环境中提供了方便的部署方式。如果您正在寻找一种快速开发自然语言处理应用程序的方法,那么Pipeline可能是一个不错的选择。


❤️觉得内容不错的话,欢迎点赞收藏加关注😊😊😊,后续会继续输入更多优质内容❤️

👉有问题欢迎大家加关注私戳或者评论(包括但不限于NLP算法相关,linux学习相关,读研读博相关......)👈

推荐内容
阅读全文
AI总结
GitHub 加速计划 / tra / transformers
73
5
下载
huggingface/transformers: 是一个基于 Python 的自然语言处理库,它使用了 PostgreSQL 数据库存储数据。适合用于自然语言处理任务的开发和实现,特别是对于需要使用 Python 和 PostgreSQL 数据库的场景。特点是自然语言处理库、Python、PostgreSQL 数据库。
最近提交(Master分支:7 个月前 )
d1b92369 - 1 天前
25b7f272 * remove one of the last deps * update fast image processor after refactor * styling * more quality of life improvements * nit * update * cleanups * some cleanups * vllm updates * update fake image token * [convert] Fix typo * [convert] Strip extraneous bytes from shards * [convert] Minor fixes * [convert] Use num_experts * multi-image fixes in modeling + processor * fixup size * 128 experts * Use default rope * Unfuse mlp * simplify a lot inputs embeds merging * remove .item() :eyes: * fix from review * Address feedback * Use None "default" for rope_scaling. Add eot. * set seed * return aspect ratios and bug fixes * Moe 128 rebased (#8) * 128 experts * Use default rope * Unfuse mlp * Address feedback * Use None "default" for rope_scaling. Add eot. * Meta/llama quant compat (#7) * add quant compatible model & conversion code for llama4 * fix a few issues * fix a few issues * minor type mapping fix --------- Co-authored-by: Lu Fang <fanglu@fb.com> * use a new config parameter to determine which model definition to use for MoE --------- Co-authored-by: Pedro Cuenca <pedro@huggingface.co> Co-authored-by: Lu Fang <fanglu@fb.com> * un-comment write_tokenizer from converting script * remove un-used imports * [llama4] Pop aspect_ratios from image processor output in Llama4Processor Signed-off-by: Jon Swenson <jmswen@gmail.com> * Fix parameter_count name * Update src/transformers/models/llama4/configuration_llama4.py * nit * Add changes for no_rope, moe_layers, chunked attention. Just need to test all * Update src/transformers/models/llama4/image_processing_llama4_fast.py * nit * fix post merge with main * support flex attention * fixes * fix * add layer * small updates * rebase and delete llm_compressor * nit * [llama4/mm] Add back <|image|> token that delimits global tile * [llama4/mm] Fix Llama 4 image processing unit tests * add explicit dtype Signed-off-by: Jon Swenson <jmswen@gmail.com> * sdpa works * comment todo small * fix model loading Signed-off-by: Zijing Liu <liuzijing2014@gmail.com> * revert * nits * small fix for TP on 1 node * Read new params from config * Add <|eom|> * lol don't know how this got here * adding fp8 * Save processor, fix chat template * style * Add boi/eoi tokens We don't use them. * fixes for now flex seems to work :) * updates * nits * updates * missking keys * add context parallel * update * update * fix * nits * add worldsize and make eager attn work for vision * Ignore new key present in base models * add tp_plan * fix nope Signed-off-by: Zijing Liu <liuzijing2014@gmail.com> * minor fix Signed-off-by: Zijing Liu <liuzijing2014@gmail.com> * Clean up Llama4 vision model * current updates * add support for `attn_temperature_tuning` * add floor scale * add missing attn scales * push what works, dirty trick for the device synch * oups * Fix pad_token_id See https://huggingface.co/ll-re/Llama-4-Scout-17B-16E/discussions/2/files Confirmed in the original codebase. * fix causallml loading * rm * fix tied-weights * fix sdpa * push current version * should work with both short and long * add compressed_tensos & fix fbgemm tp * Fix flex impl * style * chunking * try to revert the potentially breaking change * fix auto factory * fix shapes in general * rm processing * commit cache utils cleanup * Fix context length * fix * allocate * update tp_plan * fix SDPA! * Add support for sparse `Llama4TextMoe` layer from the kernel hub * cleanup * better merge * update * still broken fixing now * nits * revert print * Write max_position_embeddings and max_model_length * Update modeling_llama4.py * Save attention_chunk_size * Sync eos terminators * Read initializer_range * style * remove `dict` * fix * eager should use `chunked_attention_mask` * revert * fixup * fix config * Revert "Merge pull request #36 from huggingface/sparse-llama4-moe" This reverts commit ccda19f050867dd42ea143c5de60f3dec81375f0, reversing changes made to a515579aed8c0fe9bf529b6c40446a289406d5d6. * Fix typo and remove warning with compiled flex and chunked prefill * Fix MoE vs FF (#41) * fix * Use correct no_rope_layers if provided one is empty list * update tests * fix * skipping some tests * fix fp8 loading Signed-off-by: Zijing Liu <liuzijing2014@gmail.com> * fix text geneartion pipeline Signed-off-by: Zijing Liu <liuzijing2014@gmail.com> * eager needs 4D mask * fix * Some cleanup * fix * update * fix * replace correctly module * patch * modulelist * update * update * clean up * Don't move to `cuda:0` in distributed mode * restrict to compressed tensors for now * rm print * Docs! * Fixes * Update docs/source/en/model_doc/llama4.md Co-authored-by: Pedro Cuenca <pedro@huggingface.co> * Fixes * cuda graph fix * revert some stuff * fixup * styling * Update src/transformers/models/llama4/modeling_llama4.py Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * fixup * commit licence, cleanup here and there and style * more styling changes * fix dummies * fix and clean docstrings * remove comment * remove warning * Only fast image processor is supported * nit * trigger CI * fix issue with flex encoder * fix dynamic cache * Code quality * Code quality * fix more tests for now * Code quality * Code quality * Nuke bunch of failing stuff * Code quality * Code quality * cleanup removal of slow image processor * ruff fix fast image processor * fix * fix styling * Docs * Repo consistency * Repo consistency * fix sliding window issue * separate llama cache * styling * Repo consistency * Repo consistency * push waht works * L4 Repo consistency * Docs * fix last last alst alst alst alstsaltlsltlaslt --------- Signed-off-by: Jon Swenson <jmswen@gmail.com> Signed-off-by: Zijing Liu <liuzijing2014@gmail.com> Co-authored-by: yonigozlan <yoni.gozlan10@gmail.com> Co-authored-by: Pedro Cuenca <pedro@huggingface.co> Co-authored-by: Pablo Montalvo <pablo.montalvo.leroux@gmail.com> Co-authored-by: Pablo Montalvo <39954772+molbap@users.noreply.github.com> Co-authored-by: Keyun Tong <tongkeyun@gmail.com> Co-authored-by: Zijing Liu <liuzijing2014@users.noreply.github.com> Co-authored-by: Lu Fang <fanglu@fb.com> Co-authored-by: Zijing Liu <liuzijing2014@gmail.com> Co-authored-by: Jon Swenson <jmswen@gmail.com> Co-authored-by: jmswen <jmswen@users.noreply.github.com> Co-authored-by: MekkCyber <mekk.cyber@gmail.com> Co-authored-by: Mohamed Mekkouri <93391238+MekkCyber@users.noreply.github.com> Co-authored-by: Mohit Sharma <mohit21sharma.ms@gmail.com> Co-authored-by: Yong Hoon Shin <yhshin@meta.com> Co-authored-by: Marc Sun <marc@huggingface.co> Co-authored-by: drisspg <drisspguessous@gmail.com> Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com> Co-authored-by: Daniël de Kok <me@danieldk.eu> Co-authored-by: Lysandre <hi@lysand.re> Co-authored-by: Ye (Charlotte) Qi <ye.charlotte.qi@gmail.com> Co-authored-by: ydshieh <ydshieh@users.noreply.github.com> 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐