IndexError: Target 3 is out of bounds

milad66: <p>class SimpleCNN(nn.Module):</p>
<pre><code>def __init__(self):
    super(SimpleCNN, self).__init__()
    
    self.conv1 = nn.Sequential(
        nn.Conv2d(3, 16, kernel_size=5, padding=2),
        nn.BatchNorm2d(16),
        nn.ReLU(),
        nn.MaxPool2d(2)
    )
    
    self.conv2 = nn.Sequential(
        nn.Conv2d(16, 32, kernel_size=5, padding=2),
        nn.BatchNorm2d(32),
        nn.ReLU(),
        nn.MaxPool2d(2)
    )
    
    self.fc = nn.Linear(56 * 56 * 32, 2)
    
def forward(self, x):
    out = self.conv1(x)
    out = self.conv2(out)            # (bs, C, H,  W)
    out = out.view(out.size(0), -1)  # (bs, C * H, W)
    out = self.fc(out)
    return out
</code></pre>
<p>model = SimpleCNN()<br>
if use_gpu:<br>
model = model.cuda()</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(model.parameters(), lr=0.002, momentum=0.9)</p>
<p>num_epochs = 10<br>
losses = []<br>
for epoch in range(num_epochs):<br>
for i, (inputs, targets) in enumerate(train_dl):<br>
inputs = to_var(inputs)<br>
targets = to_var(targets)</p>
<pre><code>    # forwad pass
    optimizer.zero_grad()
    outputs = model(inputs)
    
    # loss
    loss = criterion(outputs, targets)
    losses += [loss.data[0]]
    # backward pass
    loss.backward()
    
    # update parameters
    optimizer.step()
    
    # report
    if (i + 1) % 50 == 0:
        print('Epoch [%2d/%2d], Step [%3d/%3d], Loss: %.4f'
              % (epoch + 1, num_epochs, i + 1, len(train_ds) // [0], loss.data[0]))
</code></pre>
<p>IndexError                                Traceback (most recent call last)<br>
 in <br>
11<br>
12         # loss<br>
—&gt; 13         loss = criterion(outputs, targets)<br>
14         losses += [loss.data[0]]<br>
15         # backward pass</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)<br>
930     def forward(self, input, target):<br>
931         return F.cross_entropy(input, target, weight=self.weight,<br>
–&gt; 932                                ignore_index=self.ignore_index, reduction=self.reduction)<br>
933<br>
934</p>
<p>~\anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2315     if size_average is not None or reduce is not None:<br>
2316         reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
-&gt; 2317     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)<br>
2318<br>
2319</p>
<p>~\anaconda3\lib\site-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>
-&gt; 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 3 is out of bounds.</p>
<p>thanks</p>

KFrank: <p>Hello Milad!</p>
<aside class="quote no-group quote-modified" data-username="milad66" data-post="1" data-topic="86075" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/91b2a8/48.png" class="avatar"> milad66:</div>
<blockquote>
<pre><code class="lang-auto">        self.fc = nn.Linear(56 * 56 * 32, 2)        
...
        out = self.fc(out)
        return out
...
criterion = nn.CrossEntropyLoss()
...
        loss = criterion(outputs, targets)
</code></pre>
<pre><code class="lang-auto">---&gt; 13         loss = criterion(outputs, targets)
...
IndexError: Target 3 is out of bounds.
</code></pre>
</blockquote>
</aside>
<p>Your last (fully-connected) layer has an output size of <code>2</code>.<br>
Therefore your model will output a tensor of shape<br>
<code>[batchSize, 2]</code>.  This is passed to <code>CrossEntropyLoss</code>,<br>
so your <code>targets</code> must be a tensor of shape <code>[batchSize]</code><br>
<em>consisting of integer class labels that range from</em> <code>0</code> <em>to</em> <code>1</code>.</p>
<p>One of your target labels has value <code>3</code>, hence the error.</p>
<p>It appears that you are working on a classification problem.<br>
How many classes does your problem actually have?</p>
<p>Best.</p>
<p>K. Frank</p>

milad66: <p>Thanks K.Frank Dear<br>
But 4 Classes(Dog , Cat , sheep , Fish)</p>

Dheepak_Jaganathan: <p>I encountered this issue when I was using the Hugging face <strong>ViTForImageClassification</strong> model.</p>
<pre><code class="lang-auto">  File "/opt/miniconda/envs/jupyter/lib/python3.7/site-packages/torch/nn/functional.py", line 2846, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
IndexError: Target 2 is out of bounds.
  0%|       
</code></pre>
<p>I solved the issue by providing the label2id and id2label parameters.</p>
<pre><code class="lang-auto">    labels = train_dataset.features["label"].names
    label2id, id2label = dict(), dict()
    for i, label in enumerate(labels):
        label2id[label] = i
        id2label[i] = label
model = ViTForImageClassification.from_pretrained(args.model, label2id=label2id, id2label=id2label)
</code></pre>