CNN : "Target 5 is out of bounds." error

gaehikim: <p>Using CNN model, I keep getting this “target is out of bounds” error.<br>
What should I fix? my label has 5 classes.</p>
<p>[my model]<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/f/9/f98f1c0ba53d0081c25a74fc9bc0402fe7f59260.png" alt="스크린샷 2020-05-18 오후 5.14.05" data-base62-sha1="zBHv3D4DmMdGiJLAkA8nRx0HcRi" width="555" height="261"></p>
<p>[my code]</p>
<p>epochs = 30<br>
lr = 0.0003<br>
batch_size = 10000</p>
<p>train_idx = np.arange(x_train.size(0))<br>
test_idx = np.arange(x_test.size(0))<br>
optimizer = torch.optim.Adam(model.parameters(),lr)<br>
criterion = nn.CrossEntropyLoss(reduction=‘sum’)</p>
<p>loss_ls = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for epoch in range(epochs):<br>
model.train()</p>
<pre><code>random.shuffle(train_idx)
x_train = x_train[train_idx]
y_train = y_train[train_idx]
train_loss = 0

for start_idx, end_idx in zip(range(0, x_train.size(0), batch_size),
                              range(batch_size, x_train.size(0)+1, batch_size)):
    
    x_batch = x_train[start_idx : end_idx]
    y_batch = y_train[start_idx : end_idx].long() 
    
    scores = model(x_batch)
    predict = F.softmax(scores, dim=1).argmax(dim = 1)
    
    acc = (predict == y_batch).sum().item() / batch_size
    
    loss = criterion(scores, y_batch)
    train_loss += loss.item()
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
print('Train epoch : %s,  loss : %s,  accuracy :%.3f'%(epoch+1, train_loss / batch_size, acc))
print('=================================================================================================')

loss_ls.append(train_loss)

if (epoch+1) % 10 == 0:
    model.eval()
    scores = model(x_test)
    predict = F.softmax(scores, dim=1).argmax(dim = 1)
    
    acc = (predict == y_test.long()).sum().item() / y_test.size(0)
    loss = criterion(scores, y_test.long())
    
    print('*************************************************************************************************')
    print('*************************************************************************************************')
    print('Test Epoch : %s, Test Loss : %.03f , Test Accuracy : %.03f'%(epoch+1, loss.item()/y_test.size(0), acc))
    print('*************************************************************************************************')
    print('*************************************************************************************************')
</code></pre>
<h2><a name="error-msg-1" class="anchor" href="#error-msg-1"></a>[error msg]</h2>
<p>IndexError                                Traceback (most recent call last)<br>
 in ()<br>
30         acc = (predict == y_batch).sum().item() / batch_size<br>
31<br>
—&gt; 32         loss = criterion(scores, y_batch)<br>
33         train_loss += loss.item()<br>
34</p>
<p>3 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2113                          .format(input.size(0), target.size(0)))<br>
2114     if dim == 2:<br>
 → 2115         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
2116     elif dim == 4:<br>
2117         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)</p>
<p>IndexError: Target 5 is out of bounds.</p>

ptrblck: <p>I assume you are working on a multi-class classification use case with <code>nn.CrossEntropyLoss</code> as the criterion.<br>
If that’s the case, you would have to make sure that the model output has the shape <code>[batch_size, nb_classes]</code>, while the target should have the shape <code>[batch_size]</code> containing the class indices in the range <code>[0, nb_classes-1]</code>. For 5 classes, your target should contain the values <code>[0, 1, 2, 3, 4]</code>, while it seems you are passing a 5 to the criterion.</p>