简体   繁体   English

运行时错误:预期一维目标张量,不支持多目标 Pytorch

[英]RuntimeError: 1D target tensor expected, multi-target not supported Pytorch

I recently shifted to pytorch from keras and I am still trying to understand how all this work.我最近从 keras 转向了 pytorch,我仍在努力了解这一切是如何工作的。 Below is the code I have implemented to classify mnist dataset using a simple MLP.下面是我使用简单的 MLP 对 mnist 数据集进行分类的代码。 Just like I used to do in keras I have flattend each of 28x28 image into a vector of 784 , and I have also created a one-hot representation for my labels.就像我以前在 keras 中所做的那样,我将每个 28x28 图像展平为一个向量 784 ,并且我还为我的标签创建了一个单热表示。 In the model I was hoping that given a vector of 784 the model would output a one-hot vector with probabilities,but as soon as my code reaches to compute the loss I get the following error :在模型中,我希望给定一个 784 的向量,该模型将输出一个具有概率的单热向量,但是一旦我的代码达到计算损失,我就会收到以下错误:

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

Below is my code :下面是我的代码:

    import numpy as np
import matplotlib.pyplot as plt
import torch
import time
from torch import nn, optim
from keras.datasets import mnist
from torch.utils.data import Dataset, DataLoader

RANDOM_SEED = 42
np.random.seed(RANDOM_SEED)
torch.manual_seed(RANDOM_SEED)


# ----------------------------------------------------

class MnistDataset(Dataset):

    def __init__(self, data_size=0):

        (x, y), (_, _) = mnist.load_data()

        x = [i.flatten() for i in x]
        x = np.array(x, dtype=np.float32)

        if data_size < 0 or data_size > len(y):
            assert ("Data size should be between 0 to number of files in the dataset")

        if data_size == 0:
            data_size = len(y)

        self.data_size = data_size

        # picking 'data_size' random samples
        self.x = x[:data_size]
        self.y = y[:data_size]

        # scaling between 0-1
        self.x = (self.x / 255)

        # Creating one-hot representation of target
        y_encoded = []
        for label in y:
            encoded = np.zeros(10)
            encoded[label] = 1
            y_encoded.append(encoded)

        self.y = np.array(y_encoded)

    def __len__(self):
        return self.data_size

    def __getitem__(self, index):

        x_sample = self.x[index]
        label = self.y[index]

        return x_sample, label


# ----------------------------------------------------

num_train_samples = 10000
num_test_samples = 2000

# Each generator returns a single
# sample & its label on each iteration.
mnist_train = MnistDataset(data_size=num_train_samples)
mnist_test = MnistDataset(data_size=num_test_samples)

# Each generator returns a batch of samples on each iteration.
train_loader = DataLoader(mnist_train, batch_size=128, shuffle=True)  # 79 batches
test_loader = DataLoader(mnist_test, batch_size=128, shuffle=True)  # 16 batches


# ----------------------------------------------------

# Defining the Model Architecture

class MLP(nn.Module):

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

        self.fc1 = nn.Linear(28 * 28, 100)
        self.act1 = nn.ReLU()
        self.fc2 = nn.Linear(100, 50)
        self.act2 = nn.ReLU()
        self.fc3 = nn.Linear(50, 10)
        self.act3 = nn.Sigmoid()

    def forward(self, x):
        x = self.act1(self.fc1(x))
        x = self.act2(self.fc2(x))
        output = self.act3(self.fc3(x))

        return output


# ----------------------------------------------------

model = MLP()

# Defining optimizer and loss function
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9)

# ----------------------------------------------------

# Training the model

epochs = 10

print("Training Started...")

for epoch in range(epochs):
    for batch_index, (inputs, targets) in enumerate(train_loader):

        optimizer.zero_grad()  # Zero the gradients
        outputs = model(inputs)  # Forward pass
        loss = criterion(outputs, targets)  # Compute the Loss
        loss.backward()  # Compute the Gradients
        optimizer.step()  # Update the parameters

        # Evaluating the model
        total = 0
        correct = 0
        with torch.no_grad():
            for batch_idx, (inputs, targets) in enumerate(test_loader):
                outputs = model(inputs)
                _, predicted = torch.max(outputs.data, 1)
                total += targets.size(0)
                correct += predicted.eq(targets.data).cpu().sum()
            print('Epoch : {} Test Acc : {}'.format(epoch, (100. * correct / total)))

print("Training Completed Sucessfully")

# ----------------------------------------------------

I also read some other posts related to the same problem & most of them said that the CrossEntropy loss the target has to be a single number ,which totally gets over my head.Can someone please explain a solution.Thank you.我还阅读了一些与同一问题相关的其他帖子,其中大多数人说目标的交叉熵损失必须是一个数字,这完全让我无法理解。有人可以解释一下解决方案吗?谢谢。

For nn.CrossEntropyLoss , you don't need one-hot representation of the label, you just need to pass the prediction's logit, which shape is (batch_size, n_class) , and a target vector (batch_size,)对于nn.CrossEntropyLoss ,您不需要标签的单热表示,您只需要传递预测的 logit,其形状是(batch_size, n_class)和一个目标向量(batch_size,)

So just pass in the label index vector y instead of one-hot vector.所以只需传入标签索引向量y而不是 one-hot 向量。

Fixed of your code:修复了您的代码:

class MnistDataset(Dataset):

    def __init__(self, data_size=0):

        (x, y), (_, _) = mnist.load_data()

        x = [i.flatten() for i in x]
        x = np.array(x, dtype=np.float32)

        if data_size < 0 or data_size > len(y):
            assert ("Data size should be between 0 to number of files in the dataset")

        if data_size == 0:
            data_size = len(y)

        self.data_size = data_size

        # picking 'data_size' random samples
        self.x = x[:data_size]
        self.y = y[:data_size]

        # scaling between 0-1
        self.x = (self.x / 255)

        self.y = y # <--

    def __len__(self):
        return self.data_size

    def __getitem__(self, index):

        x_sample = self.x[index]
        label = self.y[index]

        return x_sample, label

Take a look at Pytorch example for more detail: https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html查看 Pytorch 示例以获取更多详细信息: https ://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch:需要一维目标张量,不支持多目标 - Pytorch: 1D target tensor expected, multi-target not supported 如何解决这个问题(Pytorch RuntimeError:预期一维目标张量,不支持多目标) - how to solve this (Pytorch RuntimeError: 1D target tensor expected, multi-target not supported) Pytorch 错误:RuntimeError:预期一维目标张量,不支持多目标 - Pytorch error: RuntimeError: 1D target tensor expected, multi-target not supported 如何解决多类分类中的“RuntimeError: 1D target tensor expected, multi-target not supported”? - How to solve “RuntimeError: 1D target tensor expected, multi-target not supported” in multi-class classification? 对于分类 class RuntimeError:预期为 0D 或 1D 目标张量,不支持多目标 - for categorical class RuntimeError: 0D or 1D target tensor expected, multi-target not supported RuntimeError: 1D 目标张量,不支持多目标 - RuntimeError: 1D target tensor expected, multi-target not supported Python: NumPy pytorch错误:CrossEntropyLoss()中不支持多目标 - pytorch error: multi-target not supported in CrossEntropyLoss() pytorch:“不支持多目标”错误消息 - pytorch: “multi-target not supported” error message Pytorch:CrossEntropyLoss 的多目标错误 - Pytorch: multi-target error with CrossEntropyLoss 使用 pytorch tensorDataset class 时出现“预期一维目标张量”错误 - '1D target tensor expected' error when using pytorch tensorDataset class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM