python-3.x - 运行时错误 : The size of tensor a (133) must match the size of tensor b (10) at non-singleton dimension 1

标签 python-3.x conv-neural-network pytorch

我正在训练一个 CNN 模型。我在为我的模型进行训练迭代时遇到了问题。代码如下:

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()

        #convo layers
        self.conv1 = nn.Conv2d(3,32,3)
        self.conv2 = nn.Conv2d(32,64,3)
        self.conv3 = nn.Conv2d(64,128,3)
        self.conv4 = nn.Conv2d(128,256,3)
        self.conv5 = nn.Conv2d(256,512,3)

        #pooling layer
        self.pool = nn.MaxPool2d(2,2)

        #linear layers
        self.fc1 = nn.Linear(512*5*5,2048)
        self.fc2 = nn.Linear(2048,1024)
        self.fc3 = nn.Linear(1024,133)

        #dropout layer
        self.dropout = nn.Dropout(0.3)
        def forward(self, x):
        #first layer
        x = self.conv1(x)
        x = F.relu(x)
        x = self.pool(x)
        #x = self.dropout(x)
        #second layer
        x = self.conv2(x)
        x = F.relu(x)
        x = self.pool(x)
        #x = self.dropout(x)
        #third layer
        x = self.conv3(x)
        x = F.relu(x)
        x = self.pool(x)
        #x = self.dropout(x)
        #fourth layer
        x = self.conv4(x)
        x = F.relu(x)
        x = self.pool(x)
        #fifth layer
        x = self.conv5(x)
        x = F.relu(x)
        x = self.pool(x)
        #x = self.dropout(x)

        #reshape tensor
        x = x.view(-1,512*5*5)
        #last layer
        x = self.dropout(x)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout(x)
        x = self.fc2(x)
        x = F.relu(x)
        x = self.fc3(x)

        return x

        #loss func
        criterion = nn.MSELoss()
        optimizer = optim.Adam(net.parameters(), lr = 0.0001)
        #criterion = nn.CrossEntropyLoss()
        #optimizer = optim.SGD(net.parameters(), lr = 0.05)

        def train(n_epochs,model,loader,optimizer,criterion,save_path):    
           for epoch in range(n_epochs):
              train_loss = 0
              valid_loss = 0
              #training 
              net.train()
              for batch, (data,target) in enumerate(loaders['train']):
                   optimizer.zero_grad()
                   outputs = net(data)
                   #print(outputs.shape)
                   loss = criterion(outputs,target)
                   loss.backward()
                   optimizer.step()

当我使用 CrossEntropy Loss 函数和 SGD 优化器时,我能够毫无错误地训练模型。
当我使用 MSE 损失函数和 Adam 优化器时,我面临以下错误:
RuntimeError Traceback (most recent call last) <ipython-input-20-2223dd9058dd> in <module>
      1 #train the model
      2 n_epochs = 2
----> 3 train(n_epochs,net,loaders,optimizer,criterion,'saved_model/dog_model.pt')

<ipython-input-19-a93d145ef9f7> in train(n_epochs, model, loader, optimizer, criterion, save_path)
     22 
     23             #calculate loss
---> 24             loss = criterion(outputs,target)
     25 
     26             #backward prop

RuntimeError: The size of tensor a (133) must match the size of tensor b (10) at non-singleton dimension 1.

选择的损失函数和优化器是否影响模型的训练?任何人都可以帮忙吗?

最佳答案

好吧,错误是因为 nn.MSELoss()nn.CrossEntropyLoss()期待不同 input/target组合。您不能在不适当更改输入和目标的情况下简单地更改标准函数。从文档:

nn.CrossEntropyLoss :

  • Input:
    • (N, C) where C = number of classes, or
    • (N, C, d_1, d_2, ..., d_K) with K >= 1 in the case of K-dimensional loss.
  • Target:
    • (N) where each value is in range [0, C-1] or
    • (N, d_1, d_2, ..., d_K) with K >= 1 in the case of K-dimensional loss.


nn.MSELoss :

  • Input:
    • (N,∗) where ∗ means, any number of additional dimensions.
  • Target:
    • (N,∗), same shape as the input


如您所见,在 MSELoss 中,Target 期望与输入具有相同的形状,而在 CrossEntropyLoss 中,C维度下降。您不能使用 MSELoss 作为 CrossEntropyLoss 的替代品。

关于python-3.x - 运行时错误 : The size of tensor a (133) must match the size of tensor b (10) at non-singleton dimension 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783182/

相关文章:

python-3.x - 错误 : How to fix "SystemError: <built-in function imshow> returned NULL without setting an error" in Opencv python

python - Django 规则重用谓词

tensorflow - Keras fit 在第一个纪元结束时卡住

neural-network - 任何监控神经网络训练的pytorch工具?

python - 人脸识别keras维数问题

machine-learning - 卷积神经网络中的批量归一化

neural-network - 如何在caffe中使用convert_imageset来处理未放在一个文件夹中的图像?

python - Huggingface的Bert的第二个输出是什么意思?

python - 将多分辨率图像输入 Pytorch 神经网络

python - 在 - OpenPyXL 中应用样式时遇到问题