Runtime error on program

username: kurianbenoy
cooked: <pre><code class="lang-auto">  File "/home/kurian/Projects/pytorch-ssd/.env/lib/python3.7/site-packages/torch/nn/functional.py", line 1871, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: invalid argument 2: dimension -1 out of range of 2D tensor at /pytorch/aten/src/TH/generic/THTensor.cpp:37
</code></pre>
<p>I am getting a very strange error, which on googling does not lead to any solution. There is also a deprecated warning which I feel has some relation to this error message:</p>
<pre><code class="lang-auto">/home/kurian/Projects/pytorch-ssd/.env/lib/python3.7/site-packages/torch/nn/_reduction.py:46: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.
  warnings.warn(warning.format(ret))
</code></pre>

username: ptrblck
cooked: <p>Could you print the shapes of <code>input</code> and <code>target</code> to your criterion?<br>
The code you’ve used to initialize the criterion might also be interesting to see.</p>
<p>The warning just states, that you are using a deprecated method (<code>size_average</code> or <code>reduce</code>) instead of the new <code>reduction</code> argument.</p>

username: kurianbenoy
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> the criterion is defined as:</p>
<pre><code class="lang-auto">    criterion = MultiboxLoss(config.priors, iou_threshold=0.5, neg_pos_ratio=3,
                             center_variance=0.1, size_variance=0.2, device=DEVICE)
</code></pre>
<p>WHereas Multibox is defined as a class as shown here:</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F
import torch


from ..utils import box_utils


class MultiboxLoss(nn.Module):
    def __init__(self, priors, iou_threshold, neg_pos_ratio,
                 center_variance, size_variance, device):
        """Implement SSD Multibox Loss.

        Basically, Multibox loss combines classification loss
         and Smooth L1 regression loss.
        """
        super(MultiboxLoss, self).__init__()
        self.iou_threshold = iou_threshold
        self.neg_pos_ratio = neg_pos_ratio
        self.center_variance = center_variance
        self.size_variance = size_variance
        self.priors = priors
        self.priors.to(device)

    def forward(self, confidence, predicted_locations, labels, gt_locations):
        """Compute classification loss and smooth l1 loss.

        Args:
            confidence (batch_size, num_priors, num_classes): class predictions.
            locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            boxes (batch_size, num_priors, 4): real boxes corresponding all the priors.
        """
        num_classes = confidence.size(2)
        with torch.no_grad():
            # derived from cross_entropy=sum(log(p))
            loss = -F.log_softmax(confidence, dim=2)[:, :, 0]
            mask = box_utils.hard_negative_mining(loss, labels, self.neg_pos_ratio)

        confidence = confidence[mask, :]
        classification_loss = F.cross_entropy(confidence.reshape(-1, num_classes), labels[mask], reduction='sum')
        pos_mask = labels &gt; 0
        predicted_locations = predicted_locations[pos_mask, :].reshape(-1, 4)
        gt_locations = gt_locations[pos_mask, :].reshape(-1, 4)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations, gt_locations, reduction=False)
        num_pos = gt_locations.size(0)
        return smooth_l1_loss/num_pos, classification_loss/num_pos
</code></pre>
<p>I will check out the input and target to the criterion</p>

username: kurianbenoy
cooked: <p>On checking the inputs passed by the function:</p>
<pre><code class="lang-auto">regression_loss, classification_loss = criterion(confidence, locations, labels, boxes) 
</code></pre>
<p>I realised locations is a zero tensor as shown here:<br>
tensor([[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
…,<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0]])</p>

username: ptrblck
cooked: <p>Does this explain the error?<br>
If not, could you also print the shapes of both tensors passed to <code>F.cross_entropy</code> ?</p>