As the title clearly describes, the accuracy of my CNN stays the same when the criterion is selected as CrossEntropyLoss. I especially selected CrossEntropyLoss since only it achieves the test loss close to the training loss. No issues at all for the other loss functions.
Here is the overview of the constructed CNN model:
MyNet(
(activation_fn): ReLU(inplace)
(conv1): Sequential(
(0): Conv2d(3, 16, kernel_size=(5, 5), stride=(1, 1))
(1): ReLU(inplace)
(2): Dropout2d(p=0.5)
)
(conv2): Sequential(
(0): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1))
(1): ReLU(inplace)
(2): Dropout2d(p=0.5)
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(conv3): Sequential(
(0): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))
(1): ReLU(inplace)
(2): Dropout2d(p=0.5)
)
(conv4): Sequential(
(0): Conv2d(64, 128, kernel_size=(5, 5), stride=(1, 1))
(1): ReLU(inplace)
(2): Dropout2d(p=0.5)
)
(conv5): Sequential(
(0): Conv2d(128, 256, kernel_size=(5, 5), stride=(1, 1))
(1): ReLU(inplace)
(2): Dropout2d(p=0.5)
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(fc1): Sequential(
(0): Linear(in_features=4096, out_features=1600, bias=True)
(1): Dropout2d(p=0.5)
)
(fc2): Sequential(
(0): Linear(in_features=1600, out_features=400, bias=True)
(1): Dropout2d(p=0.5)
)
(fc3): Sequential(
(0): Linear(in_features=400, out_features=100, bias=True)
(1): Dropout2d(p=0.5)
)
(fc4): Sequential(
(0): Linear(in_features=100, out_features=8, bias=True)
)
)
Here is my test function:
def test():
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
output = output.to(device)
test_loss += criterion(output, target).item()
_, predicted = torch.max(output.data, 1)
correct += (predicted == target).sum().item()
test_loss /= math.ceil((len(test_loader.dataset) / test_batch_size))
test_losses.append(test_loss)
acc = 100. * correct / len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(
test_loss, correct, len(test_loader.dataset), acc))