【Bugs】ValueError not enough values to unpack (expected 2, got 1)

【Bugs】ValueError not enough values to unpack (expected 2, got 1),第1张

【Bugs】ValueError not enough values to unpack (expected 2, got 1) 【Bugs】ValueError: not enough values to unpack (expected 2, got 1) 1. Bug 场景

Pytorch环境下用Huggingface获取bert.outputs代码output = model(**input)[0]的时候出现bug

>>>model(**input)
Traceback (most recent call last):
  File "", line 1, in 
  File "D:anaconda3envspt16libsite-packagestorchnnmodulesmodule.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:anaconda3envspt16libsite-packagestransformersmodelsbertmodeling_bert.py", line 949, in forward
    batch_size, seq_length = input_shape
ValueError: not enough values to unpack (expected 2, got 1)

input是shape形如1D的dict的形式

{'attention_mask': tensor([1, 1, 1], device='cuda:0'), 'input_ids': tensor([ 101, 2088, 102], device='cuda:0'), 'token_type_ids': tensor([0, 0, 0], device='cuda:0')}

但是我们加载的model是model = BertModel.from_pretrained(checkpoint)的形式,所以input_shape必须是形如2D的tensor.所以在此用.unsqueeze(0)方法实现。

2. Debug

将1D dict转化成2D dict

for k in input.keys():
    input[k]= input[k].unsqueeze(0)

此时model(input)[0]不再报错。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5480488.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存