预期一维目标张量,不支持多目标

数据挖掘 卷积神经网络 训练 火炬
2022-03-10 23:54:41

我正在尝试训练我的模型。我的模型输出一个 [4,2] 张量,其中 4 是批量大小,2 是因为二进制分类。收到输出后,我找到了每行的最大元素的索引。所以,现在形状是 [4,1],我的标签的形状也是 [4,1]。我不明白为什么我仍然收到此错误。有人可以帮我解决它。另外,我使用的优化器是 SGD,损失标准是交叉熵。

  for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(dataloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        #inputs, labels = data

        inputs, labels = \
            data['image'], data['Status']

        # zero the parameter gradients
        optimizer.zero_grad()
     
        outputs = net(inputs.float())
        

        a=torch.max(outputs,1).indices

        a=a.reshape(4,1)
        a=a.float()
        labels=labels.float()
        print(a.shape,labels.shape)
        loss = criterion(a, 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')

这是我得到的错误。

torch.Size([4, 1]) torch.Size([4, 1])
RuntimeError                              Traceback (most recent call last)
<ipython-input-83-72f63a4db63e> in <module>()
     22         labels=labels.float()
     23         print(a.shape,labels.shape)
---> 24         loss = criterion(a, labels)
     25         loss.backward()
     26         optimizer.step()

2 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2822     if size_average is not None or reduce is not None:
   2823         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2824     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2825 
   2826 

RuntimeError: 1D target tensor expected, multi-target not supported

另外,型号是:

import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 16 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 16, 5)
        self.conv2 = nn.Conv2d(16, 32, 7)
        self.dropout1 = nn.Dropout2d(0.25)
        self.dropout2 = nn.Dropout2d(0.5)
        self.fc1 = nn.Linear(4608,128)  
        self.fc2 = nn.Linear(128,16)
        self.fc3 = nn.Linear(16, 2)


    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square, you can specify with a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = self.dropout1(x)

        x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
        x = F.relu(self.fc1(x))
        
        x = F.relu(self.fc2(x))
        x = self.dropout2(x)
        x =self.fc3(x)
     
        return x


net = Net()
net = net.float()
print(net)
1个回答

如果这是一个二元分类问题,那么您的模型应该只需要预测一个输出 - 一个介于 0 和 1 之间的值。接近 0 的预测值表示输入可能属于您的第一类,接近 1 的预测值将表示输入可能属于第二类。

然后,您可以使用损失函数优化您的模型,例如nn.BCELoss(prediction, target)nn.BCEWithLogitsLoss(prediction, target)这应该避免您当前遇到的错误,因为您不会为预测处理多个输出值。