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

Daniel_Joseph: <p>I got errors when i tried to  run this net , my datasets is structured as follow <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
inputs (500,1,14,14)<br>
lable =( 500,1)</p>
<pre><code class="lang-auto">
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 ('==&gt;&gt;&gt; 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 ('==&gt;&gt;&gt; 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())

</code></pre>
<p>i got this error :</p>
<pre><code class="lang-auto">(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 "&lt;ipython-input-2-ccdef2ab36c2&gt;", line 1, in &lt;module&gt;
    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 &lt;module&gt;
    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 &gt;= 0 &amp;&amp; cur_target &lt; n_classes' failed.  at /opt/conda/conda-bld/pytorch_1535488076166/work/aten/src/THNN/generic/ClassNLLCriterion.c:93

</code></pre>
<p>Can anyone help please</p>

justusschock: <p>The most common cause for this mistake is that you have labels starting at 1.</p>
<p>For example with 10 classes you would have 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 as labels.</p>
<p>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].<br>
Is that your case?</p>

Daniel_Joseph: <p>Thanks for replying <a class="mention" href="/u/justusschock">@justusschock</a>, No not that case , mine it starts from 0-15</p>

wfondrie: <p>Check the dimensions of your output layer (looks like <code>fc2</code>). In this case, I think the dimensions need to match the number of classes.</p>
<p>Try making <code>self.fc2 = nn.Linear(500, 16)</code> for 16 classes and see if that fixes it.</p>

Daniel_Joseph: <p>Absolutely right <strong><em>FIXED IT</em></strong>, as you mentionned the output of the fc2 wasn’t correct,Thank you so much <strong>wfondrie</strong>.for your help</p>

addisonklinke: <p><a class="mention" href="/u/justusschock">@justusschock</a> Good tip on the zero vs. one indexing</p>
<p>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 <code>[1, 2, ... N]</code>. Since <code>NLLLoss</code> can’t handle a one-indexed encoding, is the correct approach to allow <code>N + 1</code> total classes and set <code>ignore_index=0</code>  when initializing the <code>NLLLoss</code> criterion?</p>