Single vs Batch Prediction: IndexError: Dimension out of range

mystera: <p>I get this error calculating the softmax() for my batch predictions:</p>
<p><code>IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)</code></p>
<p>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.</p>
<p><strong>CODE</strong></p>
<pre><code class="lang-auto"># 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()
</code></pre>
<p><strong>OUTPUT</strong></p>
<pre><code class="lang-auto">tensor([[-2.7620,  2.9584]], grad_fn=&lt;AddmmBackward0&gt;)
tensor([[-2.7471,  2.9272]], grad_fn=&lt;AddmmBackward0&gt;)

tensor([[-2.7620,  2.9584], [-2.7471,  2.9272]], grad_fn=&lt;SliceBackward0&gt;)
tensor([-2.7620,  2.9584], grad_fn=&lt;SelectBackward0&gt;)
tensor([-2.7471,  2.9272], grad_fn=&lt;SelectBackward0&gt;)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [278], in &lt;cell line: 7&gt;()
      5 prediction_0 = outputs_0[0].softmax(1).argmax().item()
      6 prediction_1 = outputs_1[0].softmax(1).argmax().item()
----&gt; 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)
</code></pre>

ptrblck: <p>In this line of code:</p>
<pre><code class="lang-python">outputs_all[0][1].softmax(1)
</code></pre>
<p>you are expecting <code>outputs_all[0][1]</code> to have at least 2 dimensions, which is not the case.<br>
Check your indexing logic and make sure you are working on the desired dimensions.</p>

mystera: <p>Yes, <a class="mention" href="/u/ptrblck">@ptrblck</a>.  That’s what I’m doing wrong.  From what I’ve seen others do, my inputs to tokenizer() are correct.  I.e. an array of sentences.  So seems to me the issue is how I’m handling the output of model().</p>
<p>When passing the two sentences one-at-a-time, I get this<br>
tensor([[-2.7620,  2.9584]], grad_fn=)<br>
tensor([[-2.7471,  2.9272]], grad_fn=)</p>
<p>So I would expect passing an array of sentences to return this.  I.e. an array of the prior results like below:<br>
tensor([[[-2.7620,  2.9584]], [[-2.7471,  2.9272]]] , grad_fn=&lt; SliceBackward0 &gt;)</p>
<p>So I’m still confused how to access each result individually.  Wondering if the change from “AddmmBackward0” to “SliceBackward0” is a hint as to what I’m doing wrong.=</p>

mystera: <p>Thanks for the encouragement, <a class="mention" href="/u/ptrblck">@ptrblck</a>.  Found my answer to be that I needed to properly slice the 2d tensor.  I still have much more to learn about Tensors as now the grad_fn has changed yet again.</p>
<p><strong>CODE</strong></p>
<pre><code class="lang-auto">prediction_all_0 = outputs_all[0][0:1, 0:2].softmax(1).argmax().item()
print(prediction_all_0)
print(outputs_all[0][0:1, 0:2])
</code></pre>
<p><strong>OUTPUT</strong></p>
<pre><code class="lang-auto">1
tensor([[-2.7620,  2.9584]], grad_fn=&lt;SliceBackward0&gt;)
</code></pre>