Neural Net Pytorch - Error: multi-target not supported


I am new to pytorch and I am trying to predict diabetes classification.

import torch.optim as optim
import torch
import numpy as np
from torch.autograd import Variable
from import Dataset, DataLoader
model = DeepNet(8)
#8 input features, and 3 outputs

optimizer = optim.SGD(model.parameters(), lr=0.0001)
loss_fn = nn.CrossEntropyLoss()
#batch size of 200 is indicated in the parameters of the dataloader.
loss_list = []
for epoch in range(100):  # loop over the dataset for 100 epochs
    avg_loss = 0
    for index, data in enumerate(train_loader, 0):  
        inputs, labels = data
        inputs, labels = Variable(inputs), Variable(labels)

        outputs = model(inputs)
        print(inputs.size(), labels.size(), outputs.size())
        loss = loss_fn(outputs, labels.long())
        avg_loss +=[0]
print("The loss value at 100th epoch:", loss_list[99])

However, I am having an error: multi-target not supported at c:\a\w\1\s\tmp_conda_3.6_091443\conda\conda-bld\pytorch_1544087948354\work\aten\src\thnn\generic/ClassNLLCriterion.c:21."

Here are the sizes of the inputs, labels, and outputs, respectively:
torch.Size([200, 8]) torch.Size([200, 1]) torch.Size([200, 3])

Any help is greatly appreciated! Thank you very much!

There is a convention that the labels should be a 1-dimensional array, not a 2-dimensional array. I.e., right now, you have 200x1 as the label array, but it should be simply 200. Try

loss = loss_fn(outputs, labels.view(-1).long())

instead of

loss = loss_fn(outputs, labels.long())

This be able to solve your issue. Btw I see that you are using Variable etc, which is old PyTorch code (it has been deprecated and removed in newer versions) – just wanted to mention that in case you encounter errors related to that later on when you update your PyTorch installation to the current ones.

Thank you very very much!!!