Error: 0D or 1D target tensor expected, multi-target not supported

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import numpy as np
from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

class WineDataset(Dataset):
def init(self):
# data loading
xy = np.loadtxt(r’C:\Users\USER\Desktop\MyDatasets\wine_data.csv’, delimiter=’,’, dtype=np.float32, skiprows=1)
self.x = torch.from_numpy(xy[:, 1:])
self.y = torch.from_numpy(xy[:, [0]])
self.n_samples = xy.shape[0]
def getitem(self, index):
return self.x[index], self.y[index]
def len(self):
return self.n_samples

dataset = WineDataset()
dataloader = DataLoader(dataset=dataset, batch_size=4, shuffle=True)

samples = iter(dataloader)
features, targets = samples.next()

input_features = features.shape[1]
hidden_size = 40
num_classes = len(np.unique(targets))

class Model(nn.Module):
def init(self, input_features, hidden_size, num_classes):
super(Model, self).init()
self.l1 = nn.Linear(input_features, hidden_size)
self.relu = nn.ReLU()
self.l2 = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    out = self.l1(x)
    out = self.relu(out)
    out = self.l2(out)
    return out

Define some training parameters

model = Model(input_features, hidden_size, num_classes)
learning_rate = 0.01
epochs = 10
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

Training loop

n_total_steps = len(dataloader)
for epoch in range(epochs):
for i, (features, targets) in enumerate(dataloader):
# forward pass
prediction = model(features)
loss = criterion(prediction, targets)

    # empty gradients and backward pass
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (i+1) % 100 == 0:
        print(f'epoch: {epoch+1}/{epochs}, step: {i+1}/{n_total_steps}, loss: {loss.item():.4f}')

When I run this code it returns that error,
Perhaps someone can go through the snippet and help out. Thanks

I guess the target shape is wrong as nn.CrossEntropyLoss would expect a model output in the shape [batch_size, nb_classes] and a target in the shape [batch_size] containing class indices in the range [0, nb_classes-1].

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier :wink:

1 Like

So you mean instead of creating the dataset class. I could just load the data and use train_test_split, so that the the features and targets have the same sample size?

PS: thanks sir for the backticks :smiley:

No, as described in the previous post the data and target have different shapes and you should make sure this is the case in your use case.