#torch #huggingface-transformers
#факел #huggingface-трансформеры
Вопрос:
Я обучил и сохранил некоторые модели NER, используя
torch.save(model)
Мне нужно загрузить эти файлы модели (расширение .pt
) для оценки с использованием
torch.load('PATH_TO_MODEL.pt')
И я получаю следующую ошибку: 'BertConfig' object has no attribute 'return_dict'
Для того же я обновил свой пакет transformer до последней версии, но ошибка сохраняется.
Это трассировка стека:
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
До сих пор все работало довольно хорошо, но внезапно появляется эта ошибка. Приветствуется любая помощь или подсказка.
Ответ №1:
Попробуйте сохранить свою модель с помощью model.save_pretrained(output_dir)
. Затем вы можете загрузить свою модель с помощью, model = *.from_pretrained(output_dir)
где *
— класс модели (например, BertForTokenClassification).
Ответ №2:
Сохранение словаря модели, а не всей модели, немного отличается. Вместо того, чтобы torch.save(model)
использовать torch.save('path_to_the_model/model.pth')
и загружать с помощью torch.load('path_to_the_model/model.pth')
.