pytorch 中加载 bert 模型
发布日期:2021-05-04 21:01:54 浏览次数:34 分类:原创文章

本文共 1215 字,大约阅读时间需要 4 分钟。

import torchfrom transformers import BertTokenizer, BertModel#MODELNAME='hfl/chinese-bert-wwm-ext' #ok#MODELNAME ='hfl/chinese-bert-wwm' #ok#MODELNAME='hfl/rbt3'#ok#MODELNAME='hfl/rbtl3'#ok#MODELNAME='hfl/chinese-roberta-wwm-ext-large' #okMODELNAME='hfl/chinese-roberta-wwm-ext'  # oktokenizer = BertTokenizer.from_pretrained(MODELNAME)roberta = BertModel.from_pretrained(MODELNAME)

可以根据需要选择不同的模型。

如果它自动下载时出错,报如下异常:

Exception has occurred: OSError
Unable to load weights from pytorch checkpoint file. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.

 

清除   ~/.cache/torch/transformers下的所有文件后重试。  ~表示你的 home目录,如root用户就是 /root, 其它用户是 /home/username

 

 

模型来源于哈工大  ,需要在pytorch中安装transformers

 

pip install transformers

 

 

使用Huggingface-Transformers依托于Huggingface-Transformers 2.2.2,可轻松调用以上模型。tokenizer = BertTokenizer.from_pretrained("MODEL_NAME")model = BertModel.from_pretrained("MODEL_NAME")注意:本目录中的所有模型均使用BertTokenizer以及BertModel加载,请勿使用RobertaTokenizer/RobertaModel!其中MODEL_NAME对应列表如下:模型名	MODEL_NAMERoBERTa-wwm-ext-large	hfl/chinese-roberta-wwm-ext-largeRoBERTa-wwm-ext	hfl/chinese-roberta-wwm-extBERT-wwm-ext	hfl/chinese-bert-wwm-extBERT-wwm	hfl/chinese-bert-wwmRBT3	hfl/rbt3RBTL3	hfl/rbtl3

 

上一篇:转换google 官方 bert向量为Pytorch可用版本
下一篇:屏幕挂灯的智商税- 小米真不香

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年03月28日 04时24分09秒