Why max() received an invalid combination of arguments - got (list, dim=int), but expected one of:?

NagaYu: <pre><code class="lang-auto">model.eval()
with torch.no_grad():
    #step = 99999
    #for i in range(0, len(test), step):
    #    test_data = test.tolist()[i: i+step]
        
    for i, (images, targets, ImageIDs) in enumerate(train_loader):

        images = list(image.to(device) for image in images)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
        
        outputs = model(images)
        preds = torch.max(outputs, dim=1)
        
        id_list.append(images)
        pred_list.append(pred.item()) 
    
submissions = pd.DataFrame({'ImageID': id_list, 'predictions': pread_list})
submissions.to_csv('test_data/sample_submission.csv', index=False, header=True)
</code></pre>
<p>TypeError                                 Traceback (most recent call last)<br>
Input In [11], in &lt;cell line: 2&gt;()<br>
10 targets = [{k: v.to(device) for k, v in t.items()} for t in targets]<br>
12 outputs = model(images)<br>
—&gt; 13 preds = torch.max(outputs, dim=600)<br>
15 id_list.append(images)<br>
16 pred_list.append(pred.item())</p>
<p>TypeError: max() received an invalid combination of arguments - got (list, dim=int), but expected one of:</p>
<ul>
<li>(Tensor input)</li>
<li>(Tensor input, Tensor other, *, Tensor out)</li>
<li>(Tensor input, int dim, bool keepdim, *, tuple of Tensors out)</li>
<li>(Tensor input, name dim, bool keepdim, *, tuple of Tensors out)</li>
</ul>

thecho7: <p><code>max</code> takes a tensor as an input.<br>
Try this</p>
<pre><code class="lang-auto">preds = torch.max(torch.tensor(outputs), dim=1)
</code></pre>
<p>Be careful of <strong>outputs</strong> has a dimension more than 2. (Because you call dim=1 in max function)</p>
<p><a class="mention" href="/u/nagayu">@NagaYu</a> Is this solved?</p>

AlphaBetaGamma96: <p>Be careful using <code>torch.tensor</code> as that will break your computation graph and no gradients will flow from <code>outputs</code> to your <code>params</code>. You’ll want to use <code>torch.stack</code> instead which has a <code>grad_fn</code>.</p>

NagaYu: <p>I didn’t solve it.</p>
<blockquote>
<p>RuntimeError                              Traceback (most recent call last)<br>
Input In [11], in &lt;cell line: 2&gt;()<br>
10 targets = [{k: v.to(device) for k, v in t.items()} for t in targets]<br>
12 outputs = model(images)<br>
—&gt; 13 preds = torch.max(torch.tensor(outputs), dim=1)<br>
15 id_list.append(images)<br>
16 pred_list.append(pred.item())<br>
RuntimeError: Could not infer dtype of dict</p>
</blockquote>

thecho7: <p>Could you print out <code>outputs</code>?</p>

NagaYu: <p>yes… My error is ‘RuntimeError: Could not infer dtype of dict’.</p>

thecho7: <p>Nope, I meant the variable <code>outputs</code><br>
Does it contain a dictionary or something else cannot cast into a torch tensor?</p>

NagaYu: <p>There was no error there.</p>

AlphaBetaGamma96: <p>Can you share it though? Does your model return a Tensor or a list of Tensors?</p>

NagaYu: <p>I haven’t done that yet.</p>

thecho7: <p>Please <code>print(outputs)</code> if you want any help.</p>

NagaYu: <pre><code class="lang-auto">print(output)
</code></pre>
<p>[{‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}, {‘boxes’: tensor([], size=(0, 4)), ‘scores’: tensor([]), ‘labels’: tensor([], dtype=torch.int64)}]<br>
In [ ]:</p>