Running model.eval() function

ssalome: <p>Hi<br>
I have this doubt<br>
I want to create a confusion matrix and I’m a beginner. I’m getting as error at<br>
<strong>This is evaluation function</strong></p>
<blockquote>
<p>Blockquote<br>
pred = torch.tensor(<span class="chcklst-box fa fa-square-o fa-fw"></span>)<br>
confusion_matrix = torch.zeros(num_classes, num_classes)<br>
def evaluation(dataloader, model):<br>
total, correct = 0,0<br>
for data in dataloader:<br>
inputs, labels = data<br>
inputs, labels = inputs.to(device), labels.to(device)<br>
# inputs are put in the gpu<br>
outputs = model(inputs)<br>
_, pred = torch.max(outputs.data,1)<br>
for t, p in zip(classes.view(-1), preds.view(-1)):<br>
confusion_matrix[t.long(), p.long()] +=1<br>
total += labels.size(0)<br>
correct +=(pred==labels).sum().item()<br>
return 100 * correct/total</p>
</blockquote>
<p><strong>This is the training loop</strong></p>
<blockquote>
<p>Blockquote<br>
loss_epoch_arr = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
max_epochs = 1<br>
min_loss = 1000</p>
</blockquote>
<p>n_iters = np.ceil(1050/batch_size)</p>
<p>for epoch in range(max_epochs):<br>
for i, data in enumerate(trainloader, 0):</p>
<pre><code>    inputs, labels = data
    inputs, labels = inputs.to(device), labels.to(device)

    #print(inputs.shape)
    opt.zero_grad()

    outputs = resnet(inputs)
    loss = loss_fn(outputs, labels)
    loss.backward()
    opt.step()

    if min_loss&gt; loss.item():
        min_loss = loss.item()
        best_model = copy.deepcopy(resnet.state_dict())
        print('Min loss %0.2f' % min_loss)

    if i%100 == 0:
        print('Iteration : %d/%d, Loss : %0.2f' % (i, n_iters, loss.item()))

    del inputs, labels, outputs
    torch.cuda.empty_cache()

loss_epoch_arr.append(loss.item())

print('Epoch: %d/%d, Test acc: %0.2f, Train acc : %0.2f' % (epoch, max_epochs, evaluation(testloader,resnet), 
                                                            evaluation(trainloader, resnet)))    
</code></pre>
<p>plt.plot(loss_epoch_arr)<br>
plt.show()</p>
<p><strong>I’m getting the following error</strong></p>
<blockquote>
<p>Blockquote</p>
</blockquote>
<hr>
<p>AttributeError                            Traceback (most recent call last)<br>
 in <br>
32     loss_epoch_arr.append(loss.item())<br>
33<br>
—&gt; 34     print(‘Epoch: %d/%d, Test acc: %0.2f, Train acc : %0.2f’ % (epoch, max_epochs, evaluation(testloader,resnet),<br>
35                                                                 evaluation(trainloader, resnet)))<br>
36 plt.plot(loss_epoch_arr)</p>
<p> in evaluation(dataloader, model)<br>
11         # from net to model - 2 change from lenet<br>
12         _, pred = torch.max(outputs.data,1)<br>
—&gt; 13         for t, p in zip(classes.view(-1), preds.view(-1)):<br>
14             confusion_matrix[t.long(), p.long()] +=1<br>
15         total += labels.size(0)</p>
<p>AttributeError: ‘tuple’ object has no attribute ‘view’</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
<p>Can someone help me with this?</p>

phan_phan: <p>In the line</p>
<pre><code class="lang-auto">for t, p in zip(classes.view(-1), preds.view(-1)):
</code></pre>
<p>either <code>classes</code> or <code>preds</code> (or both) is a tuple, when it should be a tensor.</p>
<p>It might be <code>preds</code>, that should maybe be <code>pred</code>, because on the line just before we have <code>_, pred = torch.max(outputs.data,1)</code></p>
<p>Or it might be <code>classes</code>, that should maybe be <code>labels</code>, because a few lines before we get the tensor <code>labels</code> that is never used.</p>
<p>Or maybe both ?</p>

ssalome: <p>yeah u were right it is supposed to be labels, labels is tensor, preds was already tensor.<br>
But i have a doubt i rougly ran it for 1 epoch to see if the code for confusion matrix works…I had defined it in evaluation function…like on loading the model and then running the evaluation function on<br>
train and test loader it should give some non- zero values but I’m getting all zeros only. What could be the error ?<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/c/6c899e323353496272797636832cfd306f430a78.png" data-download-href="https://discuss.pytorch.org/uploads/default/6c899e323353496272797636832cfd306f430a78" title="Screenshot (1324)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/c/6c899e323353496272797636832cfd306f430a78.png" alt="Screenshot (1324)" data-base62-sha1="fuapxoEw85foQUsDnWICtwZrGLS" width="690" height="237" data-dominant-color="F5F6F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (1324)</span><span class="informations">1443×497 9.82 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ssalome: <p>This is the evaluation function<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/a/fabcd4360d434ad97614eab5919dbe41adad6746.png" data-download-href="https://discuss.pytorch.org/uploads/default/fabcd4360d434ad97614eab5919dbe41adad6746" title="Screenshot (1326)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/a/fabcd4360d434ad97614eab5919dbe41adad6746.png" alt="Screenshot (1326)" data-base62-sha1="zM7VLJbSyBsMzdYIKMysUDEfDTM" width="690" height="186" data-dominant-color="F0F1F1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (1326)</span><span class="informations">1328×358 23.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

phan_phan: <p><code>preds</code>, defined at the first line, is an empty tensor<br>
<code>pred</code>, defined in your function, is the one containing the predictions<br>
To compute the confusion matrix, only <code>preds</code> is used.<br>
I think the confusion lies there</p>

ssalome: <p>Yeah the confusion was probably that only. Thank you:)</p>

ssalome: <p>hi<br>
Now Im able to get the probabilities but there’ s a problem i have only 1493 images in total and 1045 go for training and 448 for testing…but the confusion matrix rows sum up to 5100…what can be the error.</p>
<p><span class="hashtag-raw">#preds</span> = torch.tensor(<span class="chcklst-box fa fa-square-o fa-fw"></span>)<br>
confusion_matrix = torch.zeros(num_classes, num_classes)<br>
def evaluation(dataloader, model):</p>
<h1><a name="model-added-as-argument-1-change-from-lenet-1" class="anchor" href="#model-added-as-argument-1-change-from-lenet-1"></a>model added as argument - 1 change from lenet</h1>
<pre><code>total, correct = 0,0
for data in dataloader:
    inputs, labels, imgindex  = data
    inputs, labels, imgindex = inputs.to(device), labels.to(device), imgindex
    # inputs are put in the gpu
    outputs = model(inputs)
    # from net to model - 2 change from lenet
    _, pred = torch.max(outputs.data,1)
    for t, p in zip(labels.view(-1), pred.view(-1)):
        confusion_matrix[t.long(), p.long()] +=1
    total += labels.size(0)
    correct +=(pred==labels).sum().item()
return 100 * correct/total
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e6e3ea8bea52cc06707adb25c6a36e4e23c54b8.png" data-download-href="https://discuss.pytorch.org/uploads/default/9e6e3ea8bea52cc06707adb25c6a36e4e23c54b8" title="Screenshot (1379)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e6e3ea8bea52cc06707adb25c6a36e4e23c54b8.png" alt="Screenshot (1379)" data-base62-sha1="mBxDAWY38pHuIvb8kvh0YKQisdO" width="640" height="500" data-dominant-color="F6F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (1379)</span><span class="informations">903×705 24.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>