加载保存的NER变压器模型会导致AttributeError吗?

时间:2020-09-17 13:25:58

标签: torch huggingface-transformers

我已经使用训练并保存了一些NER模型

torch.save(model)

我需要加载这些模型文件(扩展名.pt)以进行评估

torch.load('PATH_TO_MODEL.pt')

然后出现以下错误:'BertConfig' object has no attribute 'return_dict'

同样,我将变压器包更新为最新的,但错误仍然存​​在。

这是堆栈跟踪:

Traceback (most recent call last):
File "/home/systematicReviews/train_mtl_3.py", line 523, in <module>
test_loss, test_cr, test_cr_fine = evaluate_i(test_model, optimizer, scheduler, validation_dataloader, args, device)
File "/home/systematicReviews/train_mtl_3.py", line 180, in evaluate_i
e_loss_coarse, e_output, e_labels, e_loss_fine, e_f_output, e_f_labels, mask, e_cumulative_loss  = defModel(args, e_input_ids, attention_mask=e_input_mask, P_labels=e_labels, P_f_labels=e_f_labels)
File "/home/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/home/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 150, in forward
return self.module(*inputs[0], **kwargs[0])
File "/home/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/home/systematicReviews/models/mtl/model.py", line 122, in forward
attention_mask = attention_mask
File "/home/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/home/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/transformers/modeling_bert.py", line 784, in forward
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
File "/home/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/transformers/configuration_utils.py", line 219, in use_return_dict
return self.return_dict and not self.torchscript
AttributeError: 'BertConfig' object has no attribute 'return_dict'

以下是有关我的系统的更多信息:

- `transformers` version: 3.1.0
- Platform: Linux-4.4.0-186-generic-x86_64-with-debian-stretch-sid
- Python version: 3.6.9
- PyTorch version (GPU?): 1.3.1 (True)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

到目前为止,它的运行情况还不错,但是突然出现了这个错误。任何帮助或提示,不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试使用model.save_pretrained(output_dir)保存模型。然后,您可以使用model = *.from_pretrained(output_dir)加载模型,其中*是模型类(例如BertForTokenClassification)。

答案 1 :(得分:0)

保存模型字典而不是整个模型略有不同。使用 torch.save(model) 代替 torch.save('path_to_the_model/model.pth') 并使用 torch.load('path_to_the_model/model.pth') 加载。