Confusion matrix output not printing correctly

gamebo5000: <p>I am trying to make a confusion matrix for my model, but it is not coming outright. There are 3 classes but my matrix is displaying 2x2 instead of 3x3.  Another issue I am having is that the numbers in my matrix do not equal the total number of images in my test set. Can someone help em figure out my error?</p>
<blockquote>
<h1>Run the testing batches</h1>
<pre><code>with torch.no_grad():
    for b, (pics, names) in enumerate(test_loader):
        
        if torch.cuda.is_available():
            pics = pics.cuda()
            names = names.cuda()
            
        b+=1

        # Apply the model
        y_val = MobileNet(pics)
        val_loss = criterion(y_val, names)
        # Tally the number of correct predictions
        test_predicted = torch.max(y_val.data, 1)[1] 
        tst_corr += (test_predicted == names).sum()
        
        test_correct.append(tst_corr)
        Val_accuracy = tst_corr.item()*100/(b*batch)
        # Print Validation results
        if b%7 == 0:
            print(f'test_epoch: {i:2}  batch: {b:4}  Val_loss: {val_loss.item():10.8f}  \Val_accuracy: {Val_accuracy:7.3f}%')
            test_acc.append(Val_accuracy)
            test_losses.append(val_loss)
            print("\n")
</code></pre>
<p>names = names.cpu()<br>
test_predicted = test_predicted.cpu()<br>
arr = confusion_matrix(names.view(-1), test_predicted.view(-1))<br>
<span class="hashtag-raw">#df_cm</span> = pd.DataFrame(arr, class_names, class_names)<br>
plt.figure(figsize = (9,6))<br>
sn.heatmap(df_cm, annot=True, fmt=“d”, cmap=‘BuGn’)<br>
plt.xlabel(“prediction”)<br>
plt.ylabel(“label (ground truth)”)<br>
plt.show();</p>
</blockquote>

ptrblck: <p>Could you post the code for <code>confusion_matrix</code>?<br>
If you are using <code>unique</code> inside this method, the created confusion matrix might be smaller, if your model does not predict all classes.</p>

gamebo5000: <p>I did the text underneath the code snippet is the rest of the code.</p>

PresidentDoggo: <p>How many samples of each class is in your test dataset?</p>

PresidentDoggo: <p>I think it’s the scikit-learn libraries <code>confusion_matix</code>, seaborn libraries <code>heatmap</code> then plotted with matplot lib.</p>

gamebo5000: <p>class 1: 31<br>
class 2 &amp; 3: 100</p>

ptrblck: <p>Check the unique values in both tensors for the current batch via <code>print(torch.unique(names))</code> and <code>print(torch.unique(test_predicted))</code>. If the current batch does not contain all classes, the confusion matrix will be created by only using the found class indices.</p>
<p>You could force the output of the non-existent classes via the <code>labels</code> argument:</p>
<pre><code class="lang-python">x = torch.randint(0, 2, (10,))
y = torch.randint(0, 2, (10,))

confusion_matrix(x, y, labels=[0, 1, 2])
&gt; array([[4, 2, 0],
         [1, 3, 0],
         [0, 0, 0]], dtype=int64)
</code></pre>

gamebo5000: <p>Thanks I figure out what I was doing wrong. I was using test_predicted and names in the confusion matrix calculations these values keep changing at each iteration of the data loader after each batch. to fix this I created a list to store each of them and attempted<br>
value = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
valuename = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
with torch.no_grad():<br>
for b, (pics, names) in enumerate(test_loader):</p>
<pre><code>        if torch.cuda.is_available():
            pics = pics.cuda()
            names = names.cuda()
            
        b+=1

        # Apply the model
        y_val = MobileNet(pics)
        val_loss = criterion(y_val, names)
        # Tally the number of correct predictions
        test_predicted = torch.max(y_val.data, 1)[1] 
        value.append(test_predicted)
        valuename.append(names)
        tst_corr += (test_predicted == names).sum()
        
        test_correct.append(tst_corr)
</code></pre>
<blockquote>
<p>confusion_matrix(value, valuename)<br>
Unfortunately, that gives me this:  <strong>ValueError: unknown is not supported</strong></p>
</blockquote>

harsha_g: <p><code>names</code> is on <code>cuda</code>. So, you might to want to detach it and then convert to numpy array before feeding it into <code>confusion_matrix</code>.</p>