当前位置:网站首页>hugging face tutorial-Chinese translation-pipeline-based reasoning
hugging face tutorial-Chinese translation-pipeline-based reasoning
2022-08-09 16:48:00 【wwlsm_zql】
基于pipeline的推理
Pipeline ()使得使用 Model Hub It becomes very simple to infer any model in various tasks,比如文本生成、Image segmentation and audio classification.Even if you have no experience with a particular pattern or understand the code that supports the model,You can still use them with pipeline一起使用() !This tutorial will teach you:
- 使用 pipeline ()进行推理.
- Use a specific tokenizer or model.
- 使用pipeline()Perform audio and visual tasks.
查看pipeline()文档,to get supported taska 的完整列表.
pipeline的使用
Although each task has an associated onepipeline(),But use contains all specific taskspipeline的通用pipeline()Abstraction is simpler.Pipeline ()Automatically loads a default model and tokenizer,Ability to infer your tasks.
- 首先创建一个 pipeline ()and specify an inference task:
from transformers import pipeline
generator = pipeline(task="text-generation")
- Pass the input text topipeline():
generator("Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone")
if you have more than one input,Pass your input as a list:
generator(
... [
... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone",
... "Nine for Mortal Men, doomed to die, One for the Dark Lord on his dark throne",
... ]
... )
Any other parameters of the task can also be includedpipeline()中.The text generation task has one with multiple parameters generate ()method to control the output.例如,If you want to generate multiple outputs,请设置 num_return_sequences 参数:
generator(
... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone",
... num_return_sequences=2,
... )
- Select a model and tokenizer
Pipeline ()接受来自 modelhub any model of.Model Hub There are some marks on it,They allow you to filter the models you wish to use for the task.Once you have selected a suitable model,加载相应的 autoodelfor 和[‘ AutoTokenizer’]类.例如,Loaded for a causal language modeling task automatodelforcausallm 类:
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
Create one for your task pipeline () ,and specify your loaded model and tokenizer:
from transformers import pipeline
generator = pipeline(task="text-generation", model=model, tokenizer=tokenizer)
Pass the input text to pipeline ()to generate some text:
generator("Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone")
[{
'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Seven for the Dragon-lords (for them to rule in a world ruled by their rulers, and all who live within the realm'}]
音频pipeline
流水线()The flexibility means it can also be extended to audio tasks.
例如,Let's start with John·f·肯尼迪(johnf.Kennedy)著名的“我们选择登月”A short clip of a speech to categorize emotions.在 Model Hub Find an audio classification model for emotion recognition on ,并将其加载到pipeline中() :
from transformers import pipeline
audio_classifier = pipeline(
... task="audio-classification", model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"... )
Pass the audio file to pipeline() :
audio_classifier("jfk_moon_speech.wav")
[{
'label': 'calm', 'score': 0.13856211304664612},
{
'label': 'disgust', 'score': 0.13148026168346405},
{
'label': 'happy', 'score': 0.12635163962841034},
{
'label': 'angry', 'score': 0.12439591437578201},
{
'label': 'fearful', 'score': 0.12404385954141617}]
愿景pipeline
最后,使用pipeline()Performing vision tasks is virtually the same.
Specify your vision task and pass the image to the classifier.An image image can be a link or a local path to the image.例如,Which feline is shown below?
from transformers import pipeline
vision_classifier = pipeline(task="image-classification")
vision_classifier(
... images="
"... )
[{
'label': 'lynx, catamount', 'score': 0.4403027892112732},
{
'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor',
'score': 0.03433405980467796},
{
'label': 'snow leopard, ounce, Panthera uncia',
'score': 0.032148055732250214},
{
'label': 'Egyptian cat', 'score': 0.02353910356760025},
{
'label': 'tiger cat', 'score': 0.023034192621707916}]
This article is a translation study of English content,仅学习使用
边栏推荐
猜你喜欢
随机推荐
抱抱脸(hugging face)教程-中文翻译-模型概要
(精中求精) rem适配布局
【The sword refers to Offer II 091. Paint the house】
【研究生工作周报】(第五周)
It is deeply recognized that the compiler can cause differences in the compilation results
众所周知亚马逊是全球最大的在线电子商务公司。如今,它已成为全球商品种类最多的在线零售商,日活跃买家约为20-25亿。另一方面,也有大大小小的企业,但不是每个人都能赚到刀! 做网店的同学都知道,
Candide3人脸动画模型
基于MTCNN和FaceNet的实时人脸检测识别系统
抱抱脸(hugging face)教程-中文翻译-对预先训练过的模特进行微调
WebGL:BabylonJS入门——初探:数据展示
九、【Vue-Router】缓存路由组件 keep-alive标签
Example of file operations - downloading and merging streaming video files
【Postgraduate Work Weekly】
.Net Core 技巧小结
Basic principles and common methods of digital image processing
Qt control - QTextEdit usage record
【深度学习】attention机制
WebGL:BabylonJS入门——初探:我的世界
C language operator precedence
如何选择可靠的亚马逊代运营