Training/Test accuracy exceeds 100

MERAH_Samia: <p>Hello!<br>
I try to implement the classification of images with bayesian CNN using dropout,  when I started the program I noticed that the test accuracy exceeds 100 which is not logical, I don’t see what the problem is I don’t know if it’s because of convolution and pooling layer parameters or what, Any idea, please?</p>
<pre><code class="lang-auto">import torch
import torchvision
import torchvision.transforms as transforms
batch_size = 4

train_transform = transforms.Compose(
    [
#   transforms.RandomCrop(32, padding=4),
#   transforms.RandomHorizontalFlip(),
     transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

test_transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data1', train=True,
                                        download=True, transform=train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
                                          shuffle=True)

testset = torchvision.datasets.CIFAR10(root='./data1', train=False,
                                       download=True, transform=test_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,shuffle=False)

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
print('train set size: {}'.format(len(trainset)))
log_freq = len(trainset)//batch_size
print('log freq: {}'.format(log_freq))
print('test set size: {}'.format(len(testset)))

import matplotlib.pyplot as plt
import numpy as np

def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()

dataiter = iter(trainloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images[:4]))

n_batches = len(dataiter)

import torch.nn as nn
import torch.nn.functional as F

class Net_MCDO(nn.Module):
    
   def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        self.dropout = nn.Dropout(p=0.3)

    def forward(self, x):
        x = self.pool(F.relu(self.dropout(self.conv1(x))))  # recommended to add the relu
        x = self.pool(F.relu(self.dropout(self.conv2(x))))  # recommended to add the relu
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(self.dropout(x)))
        x = self.fc3(self.dropout(x)) # no activation function needed for the last layer
        return x


mcdo=Net_MCDO()

import torch.optim as optim
from torch.autograd import Variable
CE = nn.CrossEntropyLoss()
learning_rate=0.001
optimizer=optim.SGD(mcdo.parameters(), lr=learning_rate, momentum=0.9)
epoch_num = 30
train_accuracies=np.zeros(epoch_num)
test_accuracies=np.zeros(epoch_num)


for epoch in range(epoch_num):
    average_loss = 0.0
    total=0
    success=0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data
        inputs, labels = Variable(inputs), Variable(labels)
        optimizer.zero_grad()
        outputs = mcdo.train()(inputs)
        loss=CE(outputs, labels)
        loss.backward()
        optimizer.step()
        average_loss += loss.item()
        _,predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        success += (predicted==labels.data).sum()
    #endfor
    train_accuracy = 100.0*success/total
    succes=0
    total=0
    for (inputs, labels) in testloader:
        inputs, labels = Variable(inputs), Variable(labels)
        outputs = mcdo.eval()(inputs)
        _,predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        success += (predicted==labels.data).sum()
    #endfor
test_accuracy = 100.0*success/total
    print(u"epoch{}, average_loss{}, train_accuracy{}, test_accuracy{}".format(
          epoch,
          average_loss/n_batches,
          train_accuracy,
          100*success/total
          ))
    #save
    train_accuracies[epoch] = train_accuracy
    test_accuracies[epoch] = 100.0*success/total
#end for

plt.plot(np.arange(1, epoch_num+1), train_accuracies)
plt.plot(np.arange(1, epoch_num+1), test_accuracies)
plt.show()
</code></pre>

Hmrishav_Bandyopadhy: <p>I would most likely guess you are using the variable <code>total</code> in the wrong way. It seems <code>total</code> is to represent the total shape of the <code>label</code> tensor, whereas <code>labels.size(0)</code> it would most likely get you the batch size. Try  <code>total+=labels.nelement()</code> as it would return the total number of elements in the tensor. Does that solve it?</p>

MERAH_Samia: <p>Nooo the problem persists :(((( Thank you</p>

Hmrishav_Bandyopadhy: <p>Does the problem persist in both training and testing accuracy?<br>
I do notice you have made a typo in</p>
<aside class="quote no-group" data-username="MERAH_Samia" data-post="1" data-topic="90519">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/merah_samia/48/53909_2.png" class="avatar"> MERAH_Samia:</div>
<blockquote>
<pre><code class="lang-auto">succes=0
total=0
for (inputs, labels) in testloader:
</code></pre>
</blockquote>
</aside>
<p>That might be the case?</p>

Sagnik_Mukherjee: <p>You have initialized success with  a single s. i.e. the variable name is “succes”, so “success” isnt initialized to 0 when you want it that way i guess. Fixing it will help you i think.</p>
<p>Please let me know if it worked.</p>

MERAH_Samia: <p>Yeaaaaaaaa, it worked, Thank you so muuuuch</p>

MERAH_Samia: <p>The problem was in the variable name  “succes”<br>
Thank youu soo muuuuch</p>