python - torch : "multi-target not supported"错误消息

标签 python pytorch

所以我想对一些(3, 50, 50)图片进行分类。首先,我在没有数据加载器或批处理的情况下从文件中加载了数据集,它起作用了。现在,在添加这两个东西之后我得到了那个错误:

RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15

我在互联网上找到了很多答案,主要是使用 target.squeeze(1) 但它对我不起作用。 我的目标批处理如下所示:

tensor([[1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0]], device='cuda:0')

这样应该没问题吧?

这里是完整的代码(请注意,我只是创建模型的结构,之后我将在其上应用完整和正确的数据集,因为我还没有完整的数据,只有 32 张图片,没有标签,这就是为什么我添加了 torch.tensor([1, 0]) 作为所有标签的占位符:

import torch
import torch.utils.data
import torch.nn as nn
import torch.nn.functional as F
import torch.optim
from torch.autograd import Variable

import numpy as np
from PIL import Image


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        # model structur:
        self.conv1 = nn.Conv2d(3, 10, kernel_size=(5,5),  stride=(1,1))
        self.conv2 = nn.Conv2d(10, 20, kernel_size=(5,5),  stride=(1,1))            # with mapool: output = 20 * (9,9) feature-maps -> flatten
        self.fc1 = nn.Linear(20*9*9, 250)
        self.fc2 = nn.Linear(250, 100)
        self.fc3 = nn.Linear(100, 2)

    def forward(self, x):
        # conv layers
        x = F.relu(self.conv1(x))   # shape: 1, 10, 46, 46
        x = F.max_pool2d(x, 2, 2)   # shape: 1, 10, 23, 23
        x = F.relu(self.conv2(x))   # shape: 1, 20, 19, 19
        x = F.max_pool2d(x, 2, 2)   # shape: 1, 20, 9, 9

        # flatten to dense layer:
        x = x.view(-1, 20*9*9)

        # dense layers
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        output = F.log_softmax(self.fc3(x), dim=1)
        return output


class Run:
    def __init__(self, epochs, learning_rate, dropout, momentum):
        # load model
        self.model = Model().cuda()

        # hyperparameters:
        self.epochs = epochs
        self.learning_rate = learning_rate
        self.dropout = dropout

    def preporcessing(self):
        dataset_folder = "/media/theodor/hdd/Programming/BWKI/dataset/bilder/"

        dataset = []
        for i in range(0, 35): 
            sample_image = Image.open(dataset_folder + str(i) + ".png")
            data = torch.from_numpy(np.array(sample_image)).type("torch.Tensor").reshape(3, 50, 50)
            target = torch.tensor([[1, 0]])
            sample = (data, target)
            dataset.append(sample)

        train_loader = torch.utils.data.DataLoader(dataset, batch_size=8)

        return train_loader

    def train(self):
        train_set = self.preporcessing()

        criterion = nn.CrossEntropyLoss()
        optimizer = torch.optim.SGD(self.model.parameters(), lr=self.learning_rate) 
        for epoch in range(self.epochs):
            epoch_loss = 0
            for i, data in enumerate(train_set, 0):

                sample, target = data
                # set data as cuda varibale
                sample = Variable(sample.float().cuda())
                target = Variable(target.cuda())
                # initialize optimizer
                optimizer.zero_grad()
                # predict
                output = self.model(sample)
                # backpropagation
                print(output, target.squeeze(1))
                loss = criterion(output, target.squeeze(1))    # ERROR MESSAGE: RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15
                loss.backward()
                optimizer.step()
            
                epoch_loss += loss.item()

            print("loss after epoch [", epoch, "|", self.epochs, "] :", epoch_loss)


run = Run(10, 0.001, 0.5, 0.9)
run.train()

所以我希望它开始训练(当然不会学习任何东西,因为标签是错误的)。

最佳答案

对于 nn.CrossEntropyLoss,目标必须是区间 [0, #classes] 中的单个数字,而不是单热编码目标向量。您的目标是 [1, 0],因此 PyTorch 认为您希望每个输入有多个标签,这是不支持的。

替换您的单热编码目标:

[1, 0] --> 0

[0, 1] --> 1

关于python - torch : "multi-target not supported"错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57325844/

相关文章:

python - 为什么我在 for 循环中收到无效语法错误?

python - 功能非常慢

python - 为了提交 HTML 表单数据,我应该使用变量 "id"或 "name"

machine-learning - 我们如何分析损失与历元图?

python - 为什么 MNIST 图像是 1x28x28 张量?

python - Pytorch 模型中所有梯度的列表

python - JoinableQueue.join() 会阻止什么?

python - 将Python应用程序部署到AWS

machine-learning - 如何在 pytorch 中为权重添加 L1 或 L2 正则化

python - 如何让one-hot数据与非one-hot数据兼容?