Pytorch CrossEntropyLoss from single dimensional Tensors
I am missing something here: why does CrossEntropyLoss not work with single dimensional Tensors?
from  torch  import Tensor                                                                                                                                     
X =Tensor([1.0,2.0,3.0])                                                                                                                                       
labs = Tensor([2,2,3])                                                                                                                                         
loss = nn.CrossEntropyLoss().forward(X,labs)

_stacklevel, dtype)
   1315         dim = _get_softmax_dim('log_softmax', input.dim(), _stacklevel)
   1316     if dtype is None:
-&gt; 1317         ret = input.log_softmax(dim)
   1318     else:
   1319         ret = input.log_softmax(dim, dtype=dtype)

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Why does that fail and what should be changed to get the desired result?

if you see the documentation here!
Input: (N,C) where C = number of classes
Target: (N) where each value is 0 &lt;= targets[i] &lt;= C-1
Output: scalar. If reduce is False, then (N) instead.

So it expects the input as 2D tensor
and target as 1D
import torch
from  torch  import Tensor                                                                                                                                 
X =Tensor([[1.0,2.0,3.0]])   #2D                                                                                                                                   
labs = torch.LongTensor([2]) # 0 &lt;= targets[i] &lt;= C-1                                                                                                                          
loss = nn.CrossEntropyLoss().forward(X,labs)

