RuntimeError: Error(s) in loading state_dict for BertForMaskedLM

在复现一个项目时,原项目使用的是英文词汇表,换成中文的bert模型之后报错:untimeError: Error(s) in loading state_dict for BertForMaskedLM:
        size mismatch for bert.embeddings.word_embeddings.weight: copying a param with shape torch.Size([30522, 768]) from checkpoint, the shape in current model is torch.Size([21128, 768]).
        size mismatch for cls.predictions.bias: copying a param with shape torch.Size([30522]) from checkpoint, the shape in current model is torch.Size([21128]).
        size mismatch for cls.predictions.decoder.weight: copying a param with shape torch.Size([30522, 768]) from checkpoint, the shape in current model is torch.Size([21128, 768]).
        size mismatch for cls.predictions.decoder.bias: copying a param with shape torch.Size([30522]) from checkpoint, the shape in current model is torch.Size([21128]).

根据错误提示可以看到就是vocab_size不一致。 

解决方案:

第一步:打开本地模型路径下的config.jason文件,将里面的"vocab_size"改为30522。改完之后报错如下:

第二步:根据错误提示,在from_pretrained方法中加入ignore_mismatched_sizes=True

重新运行程序,能跑了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值