PyTorch 3重塑错误

PyTorch 3 reshaping error

在 Python 中使用 PyTorch 训练 CNN 时,出现以下错误:

RuntimeError: invalid argument 2: size '[-3 x 3136]' is invalid for input with 160000 elements at /opt/conda/conda-bld/pytorch-cpu_1515613813020/work/torch/lib/TH/THStorage.c:41

这与下面模型中的 x.view 行有关:

class Net(nn.Module):

    def __init__(self):
        super(Net,self).__init__()
        self.conv1  = nn.Conv2d(3,32,5,padding=2) # 1 input, 32 out, filter size = 5x5, 2 block outer padding
        self.conv2  = nn.Conv2d(32,64,5,padding=2) # 32 input, 64 out,  filter size = 5x5, 2 block padding
        self.fc1    = nn.Linear(64*7*7,1024) # Fully connected layer 
        self.fc2    = nn.Linear(1024,2) #Fully connected layer 2 out.

    def forward(self,x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2) # Max pool over convolution with 2x2 pooling 
        x = F.max_pool2d(F.relu(self.conv2(x)), 2) # Max pool over convolution with 2x2 pooling 
        x = x.view(-1,64*7*7) # tensor.view() reshapes the tensor
        x = F.relu(self.fc1(x)) # Activation function after passing through fully connected layer
        x = F.dropout(x, training=True) #Dropout regularisation
        x = self.fc2(x) # Pass through final fully connected layer
        return F.log_softmax(x) # Give results using softmax

model = Net()
print(model) 

我不确定这是图像具有 3 个通道还是其他原因造成的。我知道这个命令应该将图像重塑为一维数组,为完全连接的层做好准备,所以当错误要求输入 160000 个元素时,我不确定如何解决这个问题。

我假设您的输入图像可能是 200x200px 大小(size 我的意思是 height x width,不考虑通道数)。

虽然您的 nn.Conv2d 层定义为输出相同大小的张量(conv1 有 32 个通道,con2 有 64 个通道),但 F.max_pool2d 是以这样的方式定义,它们将高度和宽度除以 2。

所以在 2 次最大池化操作之后,你的张量大小为 200 / (2 * 2) x 200 / (2 * 2) = 50x50px。使用来自 conv2 的 64 个通道,您将获得 64 * 50 * 50 = 160000 个元素。

现在,您需要调整 view(),以便将那些形状为 (batch_size, 64, 50, 50) 的输入转换为 (batch_size, 64 * 50 * 50)(以保留元素的数量)。您需要类似地调整您的第一个全连接层。

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

class Net(nn.Module):

    def __init__(self):
        super(Net,self).__init__()
        self.conv1  = nn.Conv2d(3,32,5,padding=2) # 1 input, 32 out, filter size = 5x5, 2 block outer padding
        self.conv2  = nn.Conv2d(32,64,5,padding=2) # 32 input, 64 out,  filter size = 5x5, 2 block padding
        self.fc1    = nn.Linear(64*50*50,1024) # Fully connected layer
        self.fc2    = nn.Linear(1024,2) #Fully connected layer 10 out.

    def forward(self,x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2) # Max pool over convolution with 2x2 pooling
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2) # Max pool over convolution with 2x2 pooling
        x = x.view(-1,64*50*50) # tensor.view() reshapes the tensor
        x = F.relu(self.fc1(x)) # Activation function after passing through fully connected layer
        x = F.dropout(x, training=True) #Dropout regularisation
        x = self.fc2(x) # Pass through final fully connected layer
        return F.log_softmax(x) # Give results using softmax

model = Net()
print(model)

x = np.ones((1, 3, 200, 200))
x = torch.tensor(x)
x = model.forward(x)
print(x)