Questions about parameter adjustment in pytorch When trying to apply the LeNet-5 model to cifar 10,

60 Views Asked by At

I ask a question because I do not have enough understanding of the number of parameters to be put into the cifar 10 model.

In the code immediately below, the batch size is set to 16.

%matplotlib inline

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))])

batchsize = 16 # this number cannnot change

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batchsize,
                                          shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=batchsize,
                                         shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

The following attempts were made to determine the LeNet-5 model.

import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        ###############fc layer################

        # convolution,  #kernal = 2 # i want to set stride=1, padding=1
        # input size (16, 3, 32, 32) #input = 3, output = 6, kernal = 5
        self.conv1 = nn.Conv2d(3, 6, 5, stride=1, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        #input feature, output feature
        self.fc1 = nn.Linear(16*16*8, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        self.relu = nn.ReLU()
        ########################################################
#flatten
    def forward(self, x):
        #x = self.conv1(x)
        #x = self.relu(x)
        #x = self.pool(x)
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = x.view(-1, 16*16*8)  
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.fc3(x)
        return x
        ###########################################
        return x
net = Net()

I created a model by writing the code as above, and ran the code below.

for epoch in range(2):
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data

        optimizer.zero_grad()


        **outputs = net(inputs)#<<<<<<<error**
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

I tried setting batch size=16 as above, but a runtimeerror occurs.

RuntimeError: shape '[-1, 2048]' is invalid for input of size 256

How should I model the numbers to fit batch size 16?

 def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5, stride=1, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        #input feature, output feature
        self.fc1 = nn.Linear(4*4*8, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        self.relu = nn.ReLU()

    def forward(self, x):
        #x = self.conv1(x)
        #x = self.relu(x)
        #x = self.pool(x)
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = x.view(-1, 4*4*8)  
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.fc3(x)
        return x

in this case, following error occurred:

ValueError: Expected input batch_size (50) to match target batch_size (16).

1

There are 1 best solutions below

3
Minh-Long Luu On

The problem is not of batch size 16, but at this line:

self.conv2 = nn.Conv2d(6, 16, 5)
#input feature, output feature
self.fc1 = nn.Linear(16*16*8, 120)

Conv2d gradually "shrinks" the input shape, so at that point, it is unlikely that the shape equals 2048, so 16*16*8 is probably the incorrect number. According to the log tract, it should be 256:

self.conv2 = nn.Conv2d(6, 16, 5)
#input feature, output feature
self.fc1 = nn.Linear(256, 120)

Don't forget to change x = x.view(-1, 16*16*8) to x = x.view(-1, 256) as well.