在torch.hub加载bert-base-chinese模型的时候出错rate limit exceeded

参考:https://github.com/pytorch/vision/issues/4156

一、问题1:rate limit exceeded

问题的产生代码

model = torch.hub.load('huggingface/pytorch-transformers', 'model', 'bert-base-chinese')

环境

pytorch 1.9.0

torchvision 0.10.0

问题

urllib.error.HTTPError: HTTP Error 403: rate limit exceeded

解决办法:

以下方法是可以尝试的方法

1. 更改torch版本

更改torch版本从1.9.0+cpu转到1.7.0

2. 注释错误代码行

注释掉hub.py的162行的方法

    _validate_not_a_forked_repo

3. 在加载错误代码之前添加代码:

    torch.hub._validate_not_a_forked_repo=lambda a,b,c: True

4. 设置github的token来增加api访问次数

        应该没用,因为【torch.hub.load】我们访问的是:https://github.com/...。而不是https://api.github.com/  

如果需要改变可以参考:

https://blog.csdn.net/caroline_wendy/article/details/109337216

如何增加GitHub api的访问次数(从每小时60到每小时5000)

如何让github中API的请求达到5000次

二、问题2:URLError

如果再次运行代码,报错:

我将torch的版本从1.9.0变成了1.8.1,但是报错:

 Downloading: "https://github.com/huggingface/pytorch-transformers/archive/master.zip" to /root/.cache/torch/hub/master.zip
    urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:841)>

原因:请求github.com请求超时

解决:

windows或Ubuntu中请求github.com请求超时,或在下载GitHub文件出现:<urlopen error [Errno 110] Connection timed out>

三、问题3:Missing dependencies

新的错误:

RuntimeError: Missing dependencies: sentencepiece

这个时候已经下载下来了

原因:请求github.com请求超时

解决:

 pip install --user transformers

 pip install --user sentencepiece

如果安装出错,请参考:Python - 安装sentencepiece异常

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
BERT(Bidirectional Encoder Representations from Transformers)是一种预训练的语言模型,它可以用于各种自然语言处理任务,例如文本分类、命名实体识别、问答等等。bert-base-chinese是针对中文语言的BERT预训练模型。 使用bert-base-chinese模型,可以按照以下步骤进行: 1. 安装相应的Python库,例如transformers库,可以使用pip install transformers进行安装。 2. 加载bert-base-chinese模型,可以使用transformers库中的BertModel类和BertTokenizer类。BertTokenizer类可以将输入文本转换为模型输入的格式,BertModel类则是BERT模型的实现。 3. 对文本进行预处理,包括分词、截断、填充等操作,将文本转换为模型输入的格式。 4. 调用BertModel类进行预测,得到模型的输出结果。 以下是一个使用bert-base-chinese模型进行文本分类的示例代码: ```python from transformers import BertTokenizer, BertModel import torch # 加载bert-base-chinese模型和tokenizer tokenizer = BertTokenizer.from_pretrained('bert-base-chinese') model = BertModel.from_pretrained('bert-base-chinese') # 输入文本 text = "这是一段测试文本" # 对文本进行预处理 inputs = tokenizer(text, return_tensors='pt') input_ids = inputs['input_ids'] attention_mask = inputs['attention_mask'] # 调用BertModel类进行预测 outputs = model(input_ids, attention_mask=attention_mask) ``` 在以上示例代码中,我们首先加载bert-base-chinese模型和tokenizer,然后将文本转换为模型输入的格式,最后调用BertModel类进行预测。在预测过程中,我们可以得到模型的输出结果outputs,可以使用这些输出结果进行各种自然语言处理任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值