huggingface使用预训练模型入门

huggingface使用预训练模型入门

huggingface的官方文档的地址:https://huggingface.co/docs/transformers/quicktour。在官方文档中提供了两种加载预训练模型的方式:一是pipline,二是为pytorch/tensorflow/flax框架加载的预训练模型,本文档只对pytorch进行介绍,其他架构除了名字使用方法一样。

pipline

官方的pipline是针对任务的,具体有哪些任务如下所示,不做过多介绍,比较简单。

使用方式就是:classfier = pipline(task=task_name, model=model_name), classfier(inputs),huggingface提供的
在这里插入图片描述

pytorch的预训练模型加载

都是在AutoClass模块下进行加载。分成三种:Generic model classes (无头纯模型),Generic pretraining classes(模型进行预训练), NLP/CV/AUDIO/MultiModle。

AutoModel加载无头模型

huggingface文档中加载AutoModel有两种方式:from_config/from_pretrain

from_config
from transformers import AutoConfig, AutoModel

# Download configuration from huggingface.co and cache.
config = AutoConfig.from_pretrained("bert-base-cased")
model = AutoModel.from_config(config)
from_pretrained(model_name or model_path)
from transformers import AutoConfig, AutoModel

# Download model and configuration from huggingface.co and cache.
model = AutoModel.from_pretrained("bert-base-cased")

# Update configuration during loading,也就是说可以传入自己定义的一些关键字,会更新到model_config中
model = AutoModel.from_pretrained("bert-base-cased", output_attentions=True)
model.config.output_attentions

# Loading from a TF checkpoint file instead of a PyTorch model (slower)
config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
model = AutoModel.from_pretrained(
    "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
)
AutoModelForPretraining 加载用于预训练的模型

两种方式:from_config/from_pretrain

from_config
from transformers import AutoConfig, AutoModelForPreTraining

# Download configuration from huggingface.co and cache.
config = AutoConfig.from_pretrained("bert-base-cased")
model = AutoModelForPreTraining.from_config(config)
from_pretrained
from transformers import AutoConfig, AutoModelForPreTraining

# Download model and configuration from huggingface.co and cache.
model = AutoModelForPreTraining.from_pretrained("bert-base-cased")

# Update configuration during loading
model = AutoModelForPreTraining.from_pretrained("bert-base-cased", output_attentions=True)
model.config.output_attentions

# Loading from a TF checkpoint file instead of a PyTorch model (slower) , 可以加载TensorFlow模型
config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
model = AutoModelForPreTraining.from_pretrained(
    "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
)
AutoModel加载带头模型

huggingface提供了很管带有任务头的模型,这里只举个典型例子,加载方式和上述一样,都是from_config/from_pretrain

from_config
from transformers import AutoConfig, AutoModelForMaskedLM

# Download configuration from huggingface.co and cache.
config = AutoConfig.from_pretrained("bert-base-cased")
model = AutoModelForMaskedLM.from_config(config)
from_pretrained
from transformers import AutoConfig, AutoModelForMaskedLM

# Download model and configuration from huggingface.co and cache.
model = AutoModelForMaskedLM.from_pretrained("bert-base-cased")

# Update configuration during loading
model = AutoModelForMaskedLM.from_pretrained("bert-base-cased", output_attentions=True)
model.config.output_attentions

# Loading from a TF checkpoint file instead of a PyTorch model (slower)
config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
model = AutoModelForMaskedLM.from_pretrained(
    "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
)







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 Hugging Face 模型,你可以按照以下步骤进行: 1. 在 Hugging Face 模型库(https://huggingface.co/models)中选择你需要的预训练模型,并下载模型文件。你可以从模型文件中获取所需的框架(如 TensorFlow、PyTorch)模型文件和配置文件等。 2. 了解模型的功能和性能。你可以查看模型介绍(Model Card)文档,该文档提供了模型的详细信息,包括模型的功能和性能。 3. 使用提供的代码样例(Use in Transformers)来了解如何使用该模型。你可以直接拷贝代码到你的项目中,并按照示例进行修改和使用。 4. 如果需要测试模型,你可以使用模型的测试模块(Hosted inference API)。这使你能够直接在该模块上测试你自己的数据,同时 Hugging Face 还提供了 Http API 可以调用,这样就不需要本地部署了。 5. 如果 Hugging Face 提供的模型无法满足你的需求,你可以进行自己的模型训练。在这种情况下,你可以使用 Hugging Face 提供的预训练模型进行迁移学习。详情请参考 Hugging Face 提供的迁移学习文档,了解如何使用预训练模型进行迁移学习。 总结来说,你可以通过在 Hugging Face 模型库中选择和下载预训练模型,查看模型介绍和代码样例,使用模型的测试模块进行测试,并根据需要进行自己的模型训练和迁移学习。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [hugging face 模型库的使用加载 Bert 预训练模型](https://blog.csdn.net/IT__learning/article/details/120741368)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Hugging Face快速入门(重点讲解模型(Transformers)和数据集部分(Datasets))](https://blog.csdn.net/zhaohongfei_358/article/details/126224199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值