Target 1 out of bonds error

username: PARUL_JOSHI
cooked: <p>I’m getting Target 1 is out of bounds error and not able to debug the error. Can anyone please help me out with this error. I have also added a snapshot of my model summary.</p>
<p><strong>My code :</strong></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/8/8818f18a8dd66215b519faf8cd3fbdb3f0b1f374.png" data-download-href="https://discuss.pytorch.org/uploads/default/8818f18a8dd66215b519faf8cd3fbdb3f0b1f374" title="Screenshot 2023-08-04 101105"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/8/8818f18a8dd66215b519faf8cd3fbdb3f0b1f374.png" alt="Screenshot 2023-08-04 101105" data-base62-sha1="jpYno3Mk5SXgi3z3uoxGrZe1Agc" width="494" height="500" data-dominant-color="F3F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2023-08-04 101105</span><span class="informations">522×528 12.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto"># Define the loss function (negative log-likelihood)
criterion = nn.NLLLoss()

# Define the optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=base_learning_rate, momentum=momentum, weight_decay=weight_decay)

# Initialize lists to store accuracy values
train_accuracy_history = []

# Training loop
train_loss_history = []
verbose = True

l2_lambda = 0.0001 

for epoch in range(num_epochs):
    model.train()
    train_loss = 0.0
    correct = 0
    total = 0

    for batch_data, batch_labels in train_loader:
        optimizer.zero_grad()

        # Forward pass
        output = model(batch_data)
        
        batch_labels = batch_labels.view(-1)

        # Compute loss
        ce_loss = criterion(output, batch_labels.long())
        
        l2_reg = 0.0
        for param in model.parameters():
            l2_reg += torch.norm(param, p=2)

        # Combine the cross-entropy loss and L2 regularization term
        loss = ce_loss + l2_lambda * l2_reg

        # Backward pass and optimization
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = torch.max(output.data, 1)
        total += batch_labels.size(0)
        correct += (predicted == batch_labels).sum().item()

    train_loss /= len(train_loader)
    accuracy = correct / total
    train_accuracy_history.append(accuracy)
    train_loss_history.append(train_loss)
    
    if verbose: 
        print(f"\nEpoch [{epoch+1}/{num_epochs}] | Train Loss: {train_loss:.4f} | Train Accuracy: {accuracy:.2f}")
        
</code></pre>
<p><strong>My error:</strong></p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [29], in &lt;cell line: 14&gt;()
     26 batch_labels = batch_labels.view(-1)
     28 # Compute loss
---&gt; 29 ce_loss = criterion(output, batch_labels.long())
     30 #ce_loss = criterion(output, batch_labels)
     32 l2_reg = 0.0

File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1130, in Module._call_impl(self, *input, **kwargs)
   1126 # If we don't have any hooks, we want to skip the rest of the logic in
   1127 # this function, and just call forward.
   1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129         or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1130     return forward_call(*input, **kwargs)
   1131 # Do not call functions when jit is used
   1132 full_backward_hooks, non_full_backward_hooks = [], []

File ~\anaconda3\lib\site-packages\torch\nn\modules\loss.py:211, in NLLLoss.forward(self, input, target)
    210 def forward(self, input: Tensor, target: Tensor) -&gt; Tensor:
--&gt; 211     return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)

File ~\anaconda3\lib\site-packages\torch\nn\functional.py:2689, in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   2687 if size_average is not None or reduce is not None:
   2688     reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2689 return torch._C._nn.nll_loss_nd(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target 1 is out of bounds.
</code></pre>

username: ptrblck
cooked: <p>Your last linear layer outputs a single value, which is wrong in combination with <code>nn.NLLLoss</code>, since this loss function is used for a multi-class classification expecting an output in the shape <code>[batch_size, nb_classes]</code> containing log probabilities.<br>
If you are working on a binary classification, initialize the last linear layer with <code>out_features=2</code> or use <code>nn.BCEWithLogitsLoss</code> as an alternative approach.</p>

username: PARUL_JOSHI
cooked: <p>Okay, thank you for explaining the error. I worked with my model and changed the output feature to 2 and it worked.</p>
<p>But right now I’m getting accuracies as below mentioned and not able to understand the issue behind these. Can you please tell the possible reasons for the same?</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/5/e5033a136df952263dd5a3adfd27be0a153b98a6.png" alt="image" data-base62-sha1="wFWfLk0GVlGRrcivGh9hIFuZIj4" width="478" height="387"></p>

username: ptrblck
cooked: <p>I guess you are missing the <code>F.log_softmax</code> or <code>nn.LogSoftmax</code> call in your model since <code>nn.NLLLoss</code> expexcts log probabilities, not logits. If you want to keep using logits you would need to use <code>nn.CrossEntropyLoss</code> as the criterion.</p>