简体   繁体   English

PyTorch线性层中的尺寸不匹配

[英]Dimensions Not Matching In PyTorch Linear Layer

Following the training lesson in PyTorch on this page: https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py 遵循此页面上PyTorch的培训课程: https ://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py

It's basically their 'Hello World!' 基本上就是他们的“ Hello World!” version of an image classifier. 图片分类器的版本。

What I'm trying to do is manually code the training steps in the network to make sure I understand each one, but I am currently getting a dimension mismatch in one of my linear layers, which has me stumped. 我想做的是手动编码网络中的培训步骤,以确保我理解每个步骤,但是我目前在线性层之一中遇到尺寸不匹配的问题,这让我很头疼。 Especially since (AFAIK) I'm recreating the steps in the tutorial exactly. 特别是由于(AFAIK),我正在完全重新创建本教程中的步骤。

Anyways........ 无论如何........

MY NETWORK: 我的网络:

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.fc2   = 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()

I believe this is exactly as they have it on their own page. 相信这正是他们在自己的页面上拥有的。

I'm trying to calculate the following step without a loop: 我正在尝试计算以下步骤而没有循环:

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

What I'm doing is this: 我在做什么是这样的:

data = enumerate(trainloader)
inputs, labels = next(data)[1]
outputs = net(inputs)

And the last line gives me the following traceback: 最后一行为我提供了以下追溯:

RuntimeError                              Traceback (most recent call last)
<ipython-input-285-d4be5abf5bb1> in <module>
----> 1 outputs = net(inputs)

~\Anaconda\lib\site-packages\torch\nn\modules\module.py in __call__(self, 
*input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

<ipython-input-282-a6eca2e3e9db> in forward(self, x)
    14         x = x.view(-1, 16 * 5 * 5)
    15         x = F.relu(self.fc1(x))
---> 16         x = F.relu(self.fc2(x))
    17         x = self.fc3(x)

Which closes out with: 结束于:

RuntimeError: size mismatch, m1: [4 x 120], m2: [84 x 10] at 
c:\a\w\1\s\tmp_conda_3.7_110206\conda\conda- 
bld\pytorch_1550401474361\work\aten\src\th\generic/THTensorMath.cpp:940

I know this means my dimension values don't match, and I suspect it has to do with the line x = x.view(-1, 16 * 5 * 5) where I go from the convolutional to linear layer, but I have two confusions: 我知道这意味着我的尺寸值不匹配,我怀疑这与x = x.view(-1, 16 * 5 * 5) ,我从卷积层移到线性层,但是我有两个困惑:

  • As far as I can tell my network matches exactly what's on the PyTorch page 据我所知,我的网络与PyTorch页面上的内容完全匹配
  • My error occurs on the second linear layer, not the first, and columns of the preceeding layer match the rows of the current one, so I find it confusing why this error is happening. 我的错误发生在第二个线性层上,而不是第一个线性层上,并且先前层的列与当前层的行匹配,因此我感到困惑,为什么会发生此错误。

Actually, there is no self.fc3(x) in __init__() as you have mentioned in forward() function. 实际上,正如您在forward()函数中提到的那样, __init__() self.fc3(x) __init__()没有self.fc3(x) Try running you code by changing 尝试通过更改运行代码

self.fc2 = nn.Linear(84, 10) in __init__() function to self.fc3 = nn.Linear(84, 10) . self.fc2 = nn.Linear(84, 10)__init__()函数来self.fc3 = nn.Linear(84, 10)

Above mistake is the reason why you are getting the error. 错误以上是您得到错误的原因。 As you are initializing self.fc2 twice in the above code, see below lines: 在上述代码中两次初始化self.fc2 ,请参见以下self.fc2行:

self.fc2   = nn.Linear(120, 84)
self.fc2   = nn.Linear(84, 10)

Here, first value of self.fc2 is overriden by later value. 在此, self.fc2第一个值被后面的值覆盖。 So, finally it is initialized with a Linear layer with input channels 84 and output channels 10. Later on, in the forward function you are passing the output channels of x = F.relu(self.fc1(x)) , ie, 120 as an input channels to x = F.relu(self.fc2(x)) , which has been changed to 84 because of the above explained reasons, you are getting the error. 因此,最后使用带有输入通道84和输出通道10的线性层对其进行初始化。随后,在正向函数中,您传递x = F.relu(self.fc1(x))的输出通道,即120作为x = F.relu(self.fc2(x))的输入通道,由于上述原因,该值已更改为84,您会遇到错误。

Apart from this, I don't think if something wrong with your code. 除此之外,我不认为您的代码是否有问题。

I don't know what is the size of the images you're using, but it seems that it's affecting the size of the very last feature map, hence, the amount of data you send to the linear model. 我不知道您使用的图像的大小是多少,但是似乎它正在影响最后一个特征图的大小,因此会影响您发送到线性模型的数据量。

Try to check the size doing this: in your forward method: 尝试检查大小这样做:在你的forward方法:

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    print (x.shape)

You're gonna have something like: torch.Size([32, 16, 4, 4]) Then, you can use x = x.view(-1, 16 * 4 * 4) 您将具有以下内容: torch.Size([32, 16, 4, 4])然后,您可以使用x = x.view(-1, 16 * 4 * 4)

Alternatively, you can get these values directly from x.shape[1:] . 或者,您可以直接从x.shape[1:]获得这些值。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM