我在pytorch上遇到这个错误“RuntimeError:invalid argument 2:size'[ - 1 x 400]”

时间:2017-12-28 22:35:38

标签: python machine-learning torch pytorch image-size

任何人都可以帮我解决此错误吗?

  

运行时错误:无效参数2:对于在/ Users / soumith / miniconda2 / conda-bld / pytorch_1503975723910 / work / torch / lib / TH / THStorage中输入1597248元素,大小'[ - 1 x 400]'无效。 C:37

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
        inputs, labels = data

        # wrap them in Variable
        inputs, labels = Variable(inputs), Variable(labels)

        # 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.data[0]
        if i % 5 == 4:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 5))
            running_loss = 0.0

print('Finished Training')

3 个答案:

答案 0 :(得分:1)

当我缩放图像尺寸时,错误停止发生。

protected $fillable = 
    ['question','description','solution','image','tags','user_id'
   ,'subcategory_id'];

答案 1 :(得分:0)

您的问题是图像的大小不是32 * 32。 要解决此问题,您需要根据网络的输入大小计算QSqlQueryModel::data的输入大小。

答案 2 :(得分:0)

计算输入大小应该可以解决此问题,而无需缩放图像。 公式是 output_size =((image_size + 2 * padding -filter)/ stride +1)