本文主要是介绍【Hugging Face】解决BART模型调用时KeyError: ‘new_zeros‘的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错误代码:
tokenizer = AutoTokenizer.from_pretrained("philschmid/bart-large-cnn-samsum")
model = AutoModelForSeq2SeqLM.from_pretrained("philschmid/bart-large-cnn-samsum")model.eval()
model.to("cuda")
loss = 0
for i in range(len(self.dataset)):batch = tokenizer([self.dataset[i]["source"]], return_tensors="pt", padding=True).to("cuda")labels = tokenizer([self.dataset[i]["target"]], return_tensors="pt", padding=True).to("cuda")print(batch)outputs = model(**batch, labels=labels)print(outputs.loss.item())
报错内容:
Traceback (most recent call last):File "D:\anaconda\envs\supTextDebug\lib\site-packages\transformers\tokenization_utils_base.py", line 266, in __getattr__return self.data[item]
KeyError: 'new_zeros'During handling of the above exception, another exception occurred:Traceback (most recent call last):File "E:\supTextDebug\supTextDebugCode\textDebugger.py", line 360, in <module>debugger.run_baselines()File "E:\supTextDebug\supTextDebugCode\textDebugger.py", line 299, in run_baselinesloss.get_loss()File "E:\supTextDebug\supTextDebugCode\lossbased.py", line 26, in get_lossoutputs = model(**batch, labels=labels)File "D:\anaconda\envs\supTextDebug\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_implreturn forward_call(*input, **kwargs)File "D:\anaconda\envs\supTextDebug\lib\site-packages\transformers\models\bart\modeling_bart.py", line 1724, in forwarddecoder_input_ids = shift_tokens_right(File "D:\anaconda\envs\supTextDebug\lib\site-packages\transformers\models\bart\modeling_bart.py", line 104, in shift_tokens_rightshifted_input_ids = input_ids.new_zeros(input_ids.shape)File "D:\anaconda\envs\supTextDebug\lib\site-packages\transformers\tokenization_utils_base.py", line 268, in __getattr__raise AttributeError
AttributeError
解决方案:
错误行:outputs = model(**batch, labels=labels)
直接使用模型的forward
方法,而不是将所有参数传递给 model
:
tokenizer = AutoTokenizer.from_pretrained("philschmid/bart-large-cnn-samsum")
model = AutoModelForSeq2SeqLM.from_pretrained("philschmid/bart-large-cnn-samsum")model.eval()
model.to("cuda")
loss = 0
for i in range(len(self.dataset)):batch = tokenizer([self.dataset[i]["source"]], return_tensors="pt", padding=True).to("cuda")labels = tokenizer([self.dataset[i]["target"]], return_tensors="pt", padding=True).to("cuda")print(batch)outputs = model(input_ids=batch["input_ids"], attention_mask=batch["attention_mask"], labels=labels["input_ids"])print(outputs.loss.item())
这篇关于【Hugging Face】解决BART模型调用时KeyError: ‘new_zeros‘的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!