I am referring this previously asked question in stack-overflow which remains unsolved till now.

I am facing same problem with pytorch when I am solving multiclass classification.

Here is my exception

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at c:\programdata\miniconda3\conda-bld\pytorch_1532501501514\work\aten\src\thnn\generic/ClassNLLCriterion.c:93

I have added whole code of mine with the dataset I am trying with but still get stuck on that

# -*- coding: utf-8 -*-
Created on Thu Jul 11 22:41:07 2019

@author: Ananda Mohon Ghosh

import pandas as pd
import numpy as np
import os
import torch
from torch import nn
from torch.autograd import Variable
import torch.utils.data as utils
from sklearn import preprocessing
from sklearn.metrics import mean_squared_error, mean_absolute_error
import matplotlib.pyplot as plt
import torch.nn.functional as F
from torchvision import datasets, transforms, models
import torch.optim as optim

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.classificaion =nn.Sequential(
            nn.Linear(16, 12),

    def forward(self, x):
        clsf = self.classificaion(x)
        return clsf

def oneHotCode(y):
    y = y-min(y)
    nOfClasses = len(set(y)) 
    labels = y.reshape(len(y), 1)
    return (labels == torch.arange(nOfClasses).reshape(1, nOfClasses)).long()

def batchData(xBatch, yBatch):
    return np.concat((xBatch, yBatch),axis=1)

def findMinMax(df):
    #find max-min
    minimum = 999999
    maximum =  -999999

    for di in  df:
        tMin = min(di)
        tMax = max(di)
            minimum = tMin
            maximum = tMax
    return maximum, minimum

dataFrame = pd.read_csv('data/features_test_encoded16_ds1_batch_sig.csv')
dataFrameValues = dataFrame.values

availabelCuda = torch.device("cuda:2" if torch.cuda.is_available() else "cpu")
print('Availabel CUDA ', availabelCuda)

num_epochs = 10
batch_size = 128
learning_rate = 1e-3
weight_decay = 1e-5
momentum = 0.9

clsf = Net()
model = clsf.to(availabelCuda)

lossFunc= nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate,  weight_decay=weight_decay)

observations =  dataFrameValues.shape[0]
featureSetLength = dataFrameValues.shape[1]
featureSet = dataFrameValues[0:len(dataFrameValues), 0:featureSetLength-1 ]
maximum, minimum =findMinMax(featureSet)

featureSet = (featureSet-minimum)/(maximum-minimum)

labelSet = dataFrameValues[0:len(dataFrameValues), featureSetLength-1 ]

Xtrain = featureSet[0:int(observations*0.7), :]
Xtest = featureSet[int(observations*0.7):observations, :]
Ytrain = labelSet[0:int(observations*0.7)]
Ytest = labelSet[int(observations*0.7):observations]

X = torch.Tensor(Xtrain)
Y = torch.Tensor(Ytrain.reshape(-1,1))
trainDataset = utils.TensorDataset(X, Y)

dataloaderTrain = utils.DataLoader(trainDataset, batch_size=batch_size, shuffle=False)

print('Model running on ', availabelCuda)

for epoch in range(num_epochs):
    for xX, yY in dataloaderTrain:
        dataX = Variable(xX).to(availabelCuda)
        dataY = Variable(yY.long()).to(availabelCuda)

        outclass = clsf(dataX)
        print(outclass.type(), dataY.view(-1).type())

        loss = lossFunc(outclass, dataY.view(-1))
        optimizer.zero_grad()               # clear gradients for this training step
        loss.backward()                     # backpropagation, compute gradients
    print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy()) #CPU

and here is the link of my dataset. Actually, I was hoping that I have problem with data normalization but eventually I couldn't solve it and still have this issue. Any kind of help is appreciated. Thanks.


1 Answer 1


Okay after 4 days I got my solution eventually. I figured it out that my classes are started from 1 to 12, so, in total I had 12 classes.

The problem was appearing when I was about to calculate the predicted class vs actual class loss with the loss function. Instead of having start from 1 that function always from class 0 instead of 1 and continuously look for 0 to 12 which indicated it do have 13 classes in total which generates an exception.

So how do I solved that? I just docked 1 form each labels and shifted it from 0 to 11 and it went fine. That's how I solved the issue. Mention, I got couple of solution at PyTorch forum regarding the same issue and those wasn't helpful at all, literally!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.