原文地址:https://blog.csdn.net/w5688414/article/details/103666409
今天需要用到pytorch-pretained-bert,但是下载预训练的模型花费了好长时间,这里来分享以下解决方法,其安装过程为:
pip install pytorch-pretrained-bert
如果调用BertModel等模型的时候,需要下载相应的预先训练模型,下载后的文件存放在cache文件夹:~/.pytorch_pretrained_bert/
但是这个下载的过程我是等到了绝望.
后面就自己手动下载了该模型,放到了自己的一个目录文件夹下(../temp/bert-base-uncased,cache_dir可以不管),然后直接调用:
model = BertModel.from_pretrained('../temp/bert-base-uncased', cache_dir=temp_dir)
然后就可以正常加载自己下载的模型了。预训练模型的下载链接为:
-
PRETRAINED_MODEL_ARCHIVE_MAP = {
-
'bert-base-uncased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased.tar.gz",
-
'bert-large-uncased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-uncased.tar.gz",
-
'bert-base-cased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-cased.tar.gz",
-
'bert-base-multilingual':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual.tar.gz",
-
'bert-base-chinese':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-chinese.tar.gz",
-
}
如果自己下载很慢,可以求助一些下载代理或者找国外的朋友帮你下载。
相应的vocab的文件下载地址为:
-
PRETRAINED_VOCAB_ARCHIVE_MAP = {
-
'bert-base-uncased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt",
-
'bert-large-uncased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-uncased-vocab.txt",
-
'bert-base-cased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-cased-vocab.txt",
-
'bert-large-cased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-cased-vocab.txt",
-
'bert-base-multilingual-uncased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual-uncased-vocab.txt",
-
'bert-base-multilingual-cased':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual-cased-vocab.txt",
-
'bert-base-chinese':
"https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-chinese-vocab.txt",
-
}
[1]. BERT-Pytorch demo初探. https://zhuanlan.zhihu.com/p/50773178