Pytorch环境下用Huggingface获取bert.outputs代码output = model(**input)[0]的时候出现bug
>>>model(**input) Traceback (most recent call last): File "", line 1, inFile "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]不再报错。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)