Training Loss not decreasing (CIFAR10 Tutorials Example)

username: kamathhrishi
cooked: <p>The loss is not decreasing after around 11 epochs. The model is a slight variant of the one in pytorch tutorial. It wasn’t working even for the tutorial’s model which I thought was because it was underfitting.</p>
<p>[10,  2000] loss: 0.873<br>
[10,  4000] loss: 0.901<br>
[10,  6000] loss: 0.890<br>
[10,  8000] loss: 0.927<br>
[10, 10000] loss: 0.910<br>
[10, 12000] loss: 0.939<br>
[11,  2000] loss: 0.828<br>
[11,  4000] loss: 0.867<br>
[11,  6000] loss: 0.883<br>
[11,  8000] loss: 0.902<br>
[11, 10000] loss: 0.917<br>
[11, 12000] loss: 0.917<br>
[12,  2000] loss: 0.818<br>
[12,  4000] loss: 0.848<br>
[12,  6000] loss: 0.883<br>
[12,  8000] loss: 0.861<br>
[12, 10000] loss: 0.871<br>
[12, 12000] loss: 0.903<br>
[13,  2000] loss: 0.799<br>
[13,  4000] loss: 0.807<br>
[13,  6000] loss: 0.864<br>
[13,  8000] loss: 0.850<br>
[13, 10000] loss: 0.888<br>
[13, 12000] loss: 0.910<br>
[14,  2000] loss: 0.777<br>
[14,  4000] loss: 0.836<br>
[14,  6000] loss: 0.843<br>
[14,  8000] loss: 0.832<br>
[14, 10000] loss: 0.863<br>
[14, 12000] loss: 0.873<br>
[15,  2000] loss: 0.759<br>
[15,  4000] loss: 0.819<br>
[15,  6000] loss: 0.817<br>
[15,  8000] loss: 0.834<br>
[15, 10000] loss: 0.847<br>
[15, 12000] loss: 0.873<br>
[16,  2000] loss: 0.756<br>
[16,  4000] loss: 0.810<br>
[16,  6000] loss: 0.830<br>
[16,  8000] loss: 0.834<br>
[16, 10000] loss: 0.860<br>
[16, 12000] loss: 0.846<br>
[17,  2000] loss: 0.762<br>
[17,  4000] loss: 0.785<br>
[17,  6000] loss: 0.811<br>
[17,  8000] loss: 0.828<br>
[17, 10000] loss: 0.849<br>
[17, 12000] loss: 0.848<br>
[18,  2000] loss: 0.779<br>
[18,  4000] loss: 0.817<br>
[18,  6000] loss: 0.793<br>
[18,  8000] loss: 0.815<br>
[18, 10000] loss: 0.852<br>
[18, 12000] loss: 0.848<br>
[19,  2000] loss: 0.739<br>
[19,  4000] loss: 0.769<br>
[19,  6000] loss: 0.816<br>
[19,  8000] loss: 0.813<br>
[19, 10000] loss: 0.834<br>
[19, 12000] loss: 0.859<br>
[20,  2000] loss: 0.738<br>
[20,  4000] loss: 0.789<br>
[20,  6000] loss: 0.826<br>
[20,  8000] loss: 0.834<br>
[20, 10000] loss: 0.867<br>
[20, 12000] loss: 0.855<br>
[21,  2000] loss: 0.772<br>
[21,  4000] loss: 0.795<br>
[21,  6000] loss: 0.793<br>
[21,  8000] loss: 0.805<br>
[21, 10000] loss: 0.834<br>
[21, 12000] loss: 0.871<br>
[22,  2000] loss: 0.757<br>
[22,  4000] loss: 0.801<br>
[22,  6000] loss: 0.831<br>
[22,  8000] loss: 0.823<br>
[22, 10000] loss: 0.840<br>
[22, 12000] loss: 0.878<br>
[23,  2000] loss: 0.771<br>
[23,  4000] loss: 0.776<br>
[23,  6000] loss: 0.811<br>
[23,  8000] loss: 0.809<br>
[23, 10000] loss: 0.844<br>
[23, 12000] loss: 0.859</p>
<pre><code class="lang-auto">import torch
import torchvision
import torchvision.transforms as transforms
import torchvision.models as models

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

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


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

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

import matplotlib.pyplot as plt
import numpy as np

# functions to show an image


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


# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

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


class Net(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.conv3 = nn.Conv2d(16, 16, 5, padding=2)
        self.conv4 = nn.Conv2d(16,32, 5, padding=2)
        self.fc1 = nn.Linear(800, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84,67)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = self.pool(F.relu(self.conv4(x)))
        x = x.view(-1,800)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x



net = Net()

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

for epoch in range(200):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0
            

print('Finished Training')
</code></pre>