这个bug发生于写BERT的在线inference代码。错误如下:
Traceback (most recent call last):
File "classifier.py", line 94, in <module>
result = clf.predict(content)
File "classifier.py", line 77, in predict
outputs = self.model(ids, mask, token_type_ids)
File "/data/angchen/env/torch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "../src/bert.py", line 29, in forward
_, output_1 = self.l1(ids, attention_mask = mask, token_type_ids=token_type_ids)
File "/data/angchen/env/torch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "/data/angchen/env/torch/lib/python3.6/site-packages/transformers/models/bert/modeling_bert.py", line 950, in forward
batch_size, seq_length = input_shape
ValueError: not enough values to unpack (expected 2, got 1)
inference部分代码如下:
46 def tokenize(self, text):
47 text = text.strip()
48 inputs = self.tokenizer.encode_plus(
49 text,
50 None,
51 add_special_tokens=True,
52 max_length=self.max_len,
53 pad_to_max_length=True, #不足部分填充
54 return_token_type_ids=True,
55 truncation=True #超过部分截断
56 )
57
58 #提取id, attention mask, token type id
59 ids = torch.tensor(inputs['input_ids'], dtype=torch.long).to(device, dtype=torch.long)
60 mask = torch.tensor(inputs['attention_mask'], dtype=torch.long).to(device, dtype=torch.long)
61 token_type_ids = torch.tensor(inputs['token_type_ids'], dtype=torch.long).to(device, dtype=torch.long)
62
63 return ids, mask, token_type_ids
64
65
66 def predict(self, content):
67 result = {}
68 #如果content为空 (文本分类器可以预测不包含汉字的文本) 则直接返回结果
69 if not content:
70 result["category"] = -1
71 result["catScores"] = []
72 return result
73 ids, mask, token_type_ids = self.tokenize(content)
74 print("ids:\n", ids, "size:\n", ids.size())
75 print("mask:\n", mask)
76 print("token_type_ids:\n", token_type_ids)
77 outputs = self.model(ids, mask, token_type_ids)
78 print(outputs)
79 """
后来发现原因是预测部分喂给模型的是一个长度为 [200]的tensor,而模型要求tensor至少还有一个维度,比如[1, 200],所以导致了 batch_size, seq_length = input_shape
ValueError: not enough values to unpack (expected 2, got 1)
解决方案:
因为缺少一个维度,所以要增加一个维度,这里用unsqueeze函数。修改tokenize函数代码如下:
46 def tokenize(self, text):
47 text = text.strip()
48 inputs = self.tokenizer.encode_plus(
49 text,
50 None,
51 add_special_tokens=True,
52 max_length=self.max_len,
53 pad_to_max_length=True, #不足部分填充
54 return_token_type_ids=True,
55 truncation=True #超过部分截断
56 )
57
58 #提取id, attention mask, token type id
59 ids = torch.tensor(inputs['input_ids'], dtype=torch.long).to(device, dtype=torch.long).unsqueeze(0)
60 mask = torch.tensor(inputs['attention_mask'], dtype=torch.long).to(device, dtype=torch.long).unsqueeze(0)
61 token_type_ids = torch.tensor(inputs['token_type_ids'], dtype=torch.long).to(device, dtype=torch.long).unsqueeze(0)
62
63 return ids, mask, token_type_ids
之后就能跑通了