Repository: pytorch/pytorch

Issue:
dimension out of range (expected to be in range of [-1, 0], but got 1)
```python
criterion = nn.CrossEntropyLoss()

print(outputs.data)
print(label.data)
loss = criterion(outputs, label)      # getting error at this point
```

The output that I'm getting is => 
```
 0.0174
 0.1866
[torch.FloatTensor of size 2]


 0
 1
[torch.FloatTensor of size 2]
```

This is correct as far as the documentation is concern, but still getting following error
```dimension out of range (expected to be in range of [-1, 0], but got 1)```


Discussion:
From the docs:
![image](https://user-images.githubusercontent.com/5652049/36983425-587c89be-2060-11e8-9991-4929b33e58fb.png)
I'm assuming your C = 2 and N = 2. Then target should be a tensor of size (2,) while output should be a tensor of size (2, 2).
@zou3519 is right, but let’s keep this issue open until we fix the error message
Thanks @zou3519,
That means, I need to have the dimension of the `outputs` tensor (1,2) and label tensor (1) as I am evaluating single example at a time, right ??
@pskanade you have two labels, yes? Then your output should be of size `(2, 2)`, where `outputs[0]` contains two elements that are the probability that `outputs[0]` is in either class. Same for `outputs[1]`.

Resolving patch:
diff --git a/torch/nn/functional.py b/torch/nn/functional.py
--- a/torch/nn/functional.py
+++ b/torch/nn/functional.py
@@ -1365,7 +1365,7 @@ def nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, r
             in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K > 1`
             in the case of K-dimensional loss.
         target: :math:`(N)` where each value is :math:`0 \leq \text{targets}[i] \leq C-1`,
-            or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K \geq 1` for
+            or :math:`(N, d_1, d_2, ..., d_K)` where :math:`K \geq 1` for
             K-dimensional loss.
         weight (Tensor, optional): a manual rescaling weight given to each
             class. If given, has to be a Tensor of size `C`
@@ -1388,6 +1388,12 @@ def nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, r
     dim = input.dim()
     if torch.is_tensor(weight):
         weight = weight
+    if dim < 2:
+        raise ValueError('Expected 2 or more dimensions (got {})'.format(dim))
+
+    if input.size(0) != target.size(0):
+        raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
+                         .format(input.size(0), target.size(0)))
     if dim == 2:
         return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
     elif dim == 4:
@@ -1405,8 +1411,6 @@ def nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, r
             return torch._C._nn.nll_loss2d(input, target, weight, size_average, ignore_index, reduce)
         out = torch._C._nn.nll_loss2d(input, target, weight, size_average, ignore_index, reduce)
         return out.view(out_size)
-    else:
-        raise ValueError('Expected 2 or more dimensions (got {})'.format(dim))
 
 
 def poisson_nll_loss(input, target, log_input=True, full=False, size_average=True, eps=1e-8, reduce=True):


Test patch:
diff --git a/test/test_nn.py b/test/test_nn.py
--- a/test/test_nn.py
+++ b/test/test_nn.py
@@ -2935,6 +2935,13 @@ def _test_loss_equal_input_target_shape(self, cast):
     def test_loss_equal_input_target_shape(self):
         self._test_loss_equal_input_target_shape(lambda x: x)
 
+    def test_NLLLoss_mismatched_batch(self):
+        x = torch.randn((10, 3), requires_grad=True)
+        # t should have size (10,)
+        t = torch.zeros((3,), dtype=torch.int64)
+        with self.assertRaisesRegex(ValueError, 'Expected.*batch_size'):
+            F.nll_loss(x, t)
+
     def test_RNN_cell_no_broadcasting(self):
         def test(cell_module, input, hx, input_size, hidden_size):
             cell = cell_module(input_size, hidden_size)