I get this error calculating the softmax() for my batch predictions:
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
Here’s code showing my correct predictions for single sentences. The print() statements indicate that I’m not handing the batch output tensor arrays properly.
CODE
# Skipping code so assume model=BertForSequenceClassification and tokenizer = BertTokenizerFast
sentences = ["Eat an apple", "Eat an orange"]
inputs_0 = tokenizer(sentences[0], return_tensors="pt")
inputs_1 = tokenizer(sentences[1], return_tensors="pt")
inputs_all = tokenizer(sentences, return_tensors="pt")
outputs_0 = model(**inputs_0)
outputs_1 = model(**inputs_1)
outputs_all = model(**inputs_all)
print(outputs_0[0])
print(outputs_1[0])
print("\n",outputs_all.logits[0:2])
prediction_0 = outputs_0[0].softmax(1).argmax().item()
prediction_1 = outputs_1[0].softmax(1).argmax().item()
prediction_all_0 = outputs_all[0][1].softmax(1).argmax().item()
prediction_all_1 = outputs_all[0][1].softmax(1).argmax().item()
OUTPUT
tensor([[-2.7620, 2.9584]], grad_fn=<AddmmBackward0>)
tensor([[-2.7471, 2.9272]], grad_fn=<AddmmBackward0>)
tensor([[-2.7620, 2.9584], [-2.7471, 2.9272]], grad_fn=<SliceBackward0>)
tensor([-2.7620, 2.9584], grad_fn=<SelectBackward0>)
tensor([-2.7471, 2.9272], grad_fn=<SelectBackward0>)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [278], in <cell line: 7>()
5 prediction_0 = outputs_0[0].softmax(1).argmax().item()
6 prediction_1 = outputs_1[0].softmax(1).argmax().item()
----> 7 prediction_all_0 = outputs_all[0][1].softmax(1).argmax().item()
8 prediction_all_1 = outputs_all[0][1].softmax(1).argmax().item()
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)