关于pytorch中参数调整的问题当尝试将LeNet-5模型应用于cifar 10时

qqrboqgw  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(112)

我问一个问题,因为我对cifar 10模型中的参数数量没有足够的了解。
在下面的代码中,批处理大小设置为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')

为了确定LeNet-5模型,进行了以下尝试。

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

我通过编写上面的代码创建了一个模型,并运行下面的代码。

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

我尝试如上所述设置批处理大小=16,但出现runtimeerror。
RuntimeError:形状“[-1,2048]”对于大小为256的输入无效
我应该如何对数字建模以适应批量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

在这种情况下,发生了以下错误:
ValueError:输入batch_size(50)应与目标batch_size(16)匹配。

nfs0ujit

nfs0ujit1#

问题不在于批量16,而在于此行:

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

Conv2d逐渐“缩小”输入形状,因此在该点上,形状不太可能等于2048,因此16*16*8可能是错误的数字。根据对数域,它应该是256

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

不要忘记将x = x.view(-1, 16*16*8)也更改为x = x.view(-1, 256)

相关问题