【问题解决】| 对于代码访问transformers库报错,无法正常下载模型及其参数的问题

对于访问Transformer库的问题

  • 考虑用科学上网,直接进行下载
  • 把模型提前下好后放到本地

但是如果能用第一种方式会好很多,但是实际中可能会遇到如下问题

报这个错,原因是未开启科学上网

OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like distilbert-base-uncased-finetuned-sst-2-english is not the path to a directory containing a file named config.json.

科学上网打开了,有可能又会报这个错

requests.exceptions.ProxyError: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url

降级两个包即可(注意降级包的时候,报错很多黄色的WARNIG:Retrying的时候,记得把科学上网关掉再降级)

第一个包 requests降级到2.27.1,第二个包 urllib3 降到1.25.11

pip install requests==2.27.1
pip install urllib3==1.25.11

可以看到如下图,可以正常下载了

在这里插入图片描述

现在一个问题,模型下到了哪里?

模型默认会下载到 home/用户名/.cache/huggingface里面,不需要的话可以把他手动删除,Windows的话是C盘的.cache里面

如果想自定义下载路径,该怎么做?

可以在后面加一个cache_dir=

如下

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese", cache_dir="new_directory/")
model = AutoModel.from_pretrained("bert-base-chinese", cache_dir="new_directory/")

你可能感兴趣的:(Solve,Problems,问题解决,python,transform)