Skip to content
Advertisement

ML model not loading full batch

I tried to build a machine learning model using CIFAR 10 dataset, but I am encountering a bug that my model stops training past i = 78 (looped 78 times, see code for more).

import torch
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
from torchvision.transforms import ToTensor
from torch.utils.data.dataloader import DataLoader
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 
0.5, 0.5))])
classes = ('plane', 'car', 'bird', 'cat','deer', 'dog', 'frog', 'horse', 'ship', 'truck')

train_dataset = CIFAR10(root = './data', train = True, download = True, transform = transform)
train_loader = DataLoader(train_dataset, batch_size = 4, shuffle = True, num_workers = 2)

test_dataset = CIFAR10(root = './data', train = False, download = True, transform = transform)
test_loader = DataLoader(test_dataset, batch_size = 128, shuffle = False, num_workers = 2)
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.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


net = Net()

optimiser = torch.optim.SGD(model.parameters(), lr = 0.001, momentum=0.9)
loss_fn = nn.CrossEntropyLoss()
for epoch in range(2):
  running_loss = 0
  for i, data in enumerate(test_loader, 0):
    images, labels = data
    outputs = model(images)

    loss = loss_fn(outputs, labels)

    optimiser.zero_grad()
    loss.backward()
    optimiser.step()

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

Sorry, I had to post the entire code because I cannot spot the mistake I made. Moreover, since I could not make it work, I tried copying the tutorial’s exact code, and it works as intended! I am posting that code too below,

import torch
import torchvision
import torchvision.transforms as transforms

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 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.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(-1, 16 * 5 * 5)
    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(2):  # 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')

Please help me find the bug!

Advertisement

Answer

Look at your main loop. you’ll notice you are using the test_loader instead of train_loader . This

for epoch in range(2):
  running_loss = 0
  for i, data in enumerate(test_loader, 0):
    images, labels = data
    outputs = model(images)

should look like this:

for epoch in range(2):
  running_loss = 0
  for i, data in enumerate(train_loader, 0):
    images, labels = data
    outputs = model(images)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement