Assertion `cur_target >= 0 && cur_target < n_classes' failed

I got errors when i tried to run this net , my datasets is structured as follow :slight_smile:
inputs (500,1,14,14)
lable =( 500,1)


train_set = DataCustom(path=path, train=True)
test_set = DataCustom(path=path, train=False)


train_loader = torch.utils.data.DataLoader(dataset=train_set,
                                           batch_size=batch_size,
                                           shuffle=True
                                           )
test_loader = torch.utils.data.DataLoader(dataset=test_set,
                                          batch_size=batch_size,
                                          shuffle=False
                                          )

class CNN_test(nn.Module):
    def __init__(self):
        super(CNN_test, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(50*6*6, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        print(x.shape)
        x = x.view(-1, 50*6*6)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

    def name(self):
        return "CNN_test"


## training
model = CNN_test()

if use_cuda:
    model = model.cuda()

optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

criterion = nn.CrossEntropyLoss()

for epoch in range(EPOCH):
    # trainning
    ave_loss = 0
    for batch_idx, (x, target) in enumerate(train_loader):
        optimizer.zero_grad()
        if use_cuda:
            x, target = x.cuda(), target.cuda()
        x, target = Variable(x), Variable(target)
        x = x.float()
        out = model(x)
        loss = criterion(out, target.squeeze())
        ave_loss = ave_loss * 0.9 + loss.data[0] * 0.1
        loss.backward()
        optimizer.step()
        if (batch_idx + 1) % 100 == 0 or (batch_idx + 1) == len(train_loader):
            print ('==>>> epoch: {}, batch index: {}, train loss: {:.6f}'.format(epoch, batch_idx + 1, ave_loss))

    # testing
    correct_cnt, ave_loss = 0, 0
    total_cnt = 0
    for batch_idx, (x, target) in enumerate(test_loader):
        if use_cuda:
            x, target = x.cuda(), target.cuda()
        x, target = Variable(x, volatile=True), Variable(target, volatile=True)
        out = model(x)
        loss = criterion(out, target)
        _, pred_label = torch.max(out.data, 1)
        total_cnt += x.data.size()[0]
        correct_cnt += (pred_label == target.data).sum()
        # smooth average
        ave_loss = ave_loss * 0.9 + loss.data[0] * 0.1

        if (batch_idx + 1) % 100 == 0 or (batch_idx + 1) == len(test_loader):
            print ('==>>> epoch: {}, batch index: {}, test loss: {:.6f}, acc: {:.3f}'.format(epoch, batch_idx + 1, ave_loss, correct_cnt * 1.0 / total_cnt))

torch.save(model.state_dict(), model.name())

i got this error :

(100, 50, 6, 6)
Traceback (most recent call last):
  File "/home/daniel/anaconda2/envs/HYMenv2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2878, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-ccdef2ab36c2>", line 1, in <module>
    runfile('/media/daniel/System/Users/dany/Desktop/docypo/Codes/Tracking_HSI_pyt/hsicnn_v2611220181300/TEST_ONLY.py', wdir='/media/daniel/System/Users/HYM_T/Desktop/BUAA_PhD_018/Codes/Tracking_HSI_pyt/hsicnn_v2611220181300')
  File "/snap/pycharm-professional/103/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/media/daniel/System/Users/dany/Desktop/docypo/Codes/Tracking_HSI_pyt/hsicnn_v2611220181300/TEST_ONLY.py", line 84, in <module>
    loss = criterion(out, target.squeeze())
  File "/home/daniel/anaconda2/envs/HYMenv2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/daniel/anaconda2/envs/HYMenv2/lib/python2.7/site-packages/torch/nn/modules/loss.py", line 862, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/daniel/anaconda2/envs/HYMenv2/lib/python2.7/site-packages/torch/nn/functional.py", line 1550, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/daniel/anaconda2/envs/HYMenv2/lib/python2.7/site-packages/torch/nn/functional.py", line 1407, in nll_loss
    return torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at /opt/conda/conda-bld/pytorch_1535488076166/work/aten/src/THNN/generic/ClassNLLCriterion.c:93

Can anyone help please

The most common cause for this mistake is that you have labels starting at 1.

For example with 10 classes you would have 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 as labels.

However to use Crossentropy and NLL loss, your labels have to start at 0,so you would have to shift all labels by one to get labels from [0-9].
Is that your case?

4 Likes

Thanks for replying @justusschock, No not that case , mine it starts from 0-15

Check the dimensions of your output layer (looks like fc2). In this case, I think the dimensions need to match the number of classes.

Try making self.fc2 = nn.Linear(500, 16) for 16 classes and see if that fixes it.

3 Likes

Absolutely right FIXED IT, as you mentionned the output of the fc2 wasnโ€™t correct,Thank you so much wfondrie.for your help

@justusschock Good tip on the zero vs. one indexing

In an NLP setting where you pad sequences to matching lengths, the customary approach is to use 0 for the padding index. In that case, the non-padding classes should be labeled [1, 2, ... N]. Since NLLLoss canโ€™t handle a one-indexed encoding, is the correct approach to allow N + 1 total classes and set ignore_index=0 when initializing the NLLLoss criterion?