Training/Test accuracy exceeds 100

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?

import torch
import torchvision
import torchvision.transforms as transforms
batch_size = 4

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

test_transform = transforms.Compose(
     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 =, batch_size=batch_size,

testset = torchvision.datasets.CIFAR10(root='./data1', train=False,
                                       download=True, transform=test_transform)
testloader =, 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)))

dataiter = iter(trainloader)
images, labels =

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


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

for epoch in range(epoch_num):
    average_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data
        inputs, labels = Variable(inputs), Variable(labels)
        outputs = mcdo.train()(inputs)
        loss=CE(outputs, labels)
        average_loss += loss.item()
        _,predicted = torch.max(, 1)
        total += labels.size(0)
        success += (
    train_accuracy = 100.0*success/total
    for (inputs, labels) in testloader:
        inputs, labels = Variable(inputs), Variable(labels)
        outputs = mcdo.eval()(inputs)
        _,predicted = torch.max(, 1)
        total += labels.size(0)
        success += (
test_accuracy = 100.0*success/total
    print(u"epoch{}, average_loss{}, train_accuracy{}, test_accuracy{}".format(
    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)

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

Nooo the problem persists :(((( Thank you

Does the problem persist in both training and testing accuracy?
I do notice you have made a typo in

That might be the case?

1 Like

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.

Please let me know if it worked.

1 Like

Yeaaaaaaaa, it worked, Thank you so muuuuch

The problem was in the variable name “succes”
Thank youu soo muuuuch