hugging face使用模型出错(MaxRetryError(‘HTTPSConnectionPool(host=\‘huggingface.co\‘, port=443)

第一部分:问题描述

我直接登录的hugging face的官网,然后在线导入的代码。

出现错误:

requests.exceptions.ProxyError: (MaxRetryError('HTTPSConnectionPool(host=\'huggingface.co\', port=443): Max retries exceeded with url: /google-bert/bert-base-chinese/resolve/main/tokenizer_config.json (Caused by ProxyError(\'Unable to connect to proxy\', ReadTimeoutError("HTTPSConnectionPool(host=\'huggingface.co\', port=443): Read timed out. (read timeout=10)")))'), '(Request ID: ba036fd1-288d-41ff-b384-96fb3b6ecc02)')

第二部分:解决方法

直接在线下载出错,我们可以自己下载好模型到本地,然后再加载的方式使用这个模型。

把上面的这些内容无脑下载下来就可以了。

上面是下载之后的效果。

import transformers
MODEL_PATH = r"C:\\Users\\dell\\Desktop\\bert-base-chinese"

# 导入模型
tokenizer = transformers.BertTokenizer.from_pretrained(r"C:\\Users\\dell\\Desktop\\bert-base-chinese\\vocab.txt")
# 导入配置文件
model_config = transformers.BertConfig.from_pretrained(MODEL_PATH)
# 修改配置
model_config.output_hidden_states = True
model_config.output_attentions = True
# 通过配置和路径导入模型
model = transformers.BertModel.from_pretrained(MODEL_PATH,config = model_config)

直接导入即可。

你需要改的只有一个:

对应的就是:

效果:

我们可以加入下面两行代码,让它忽略警告。

from transformers import logging
logging.set_verbosity_error()

完整代码为:



import transformers
MODEL_PATH = r"C:\\Users\\dell\\Desktop\\bert-base-chinese"

# 导入模型
tokenizer = transformers.BertTokenizer.from_pretrained(r"C:\\Users\\dell\\Desktop\\bert-base-chinese\\vocab.txt")
# 导入配置文件
model_config = transformers.BertConfig.from_pretrained(MODEL_PATH)
# 修改配置
model_config.output_hidden_states = True
model_config.output_attentions = True
# 通过配置和路径导入模型
model = transformers.BertModel.from_pretrained(MODEL_PATH,config = model_config)

第三部分:资源获取

通过网盘分享的文件:bert-base-chinese.zip
链接: https://pan.baidu.com/s/1uwgHun_XjBxJtLIwv9vB4Q?pwd=7fjg 提取码: 7fjg
--来自百度网盘超级会员v5的分享

猜你喜欢

转载自blog.csdn.net/weixin_74009895/article/details/143270950