pytoch交叉熵函数 IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

在使用pytoch交叉熵函数时遇到这样的问题

loss = loss_func(output, b_y)

报错信息

Traceback (most recent call last):
  File "e:\testcode\cnn.py", line 122, in <module>
    loss = loss_func(output, b_y)   # cross entropy loss
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\loss.py", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 2021, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 1317, in log_softmax
    ret = input.log_softmax(dim)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

print(output)的值

tensor([ 1.1870,  0.5772, -0.1288, -0.5445], grad_fn=<SelectBackward>)

print(b_y)

tensor([2])

求教各位能帮忙看看吗

问题咋解决的呀

同样的问题

同问

https://blog.csdn.net/liming_2464/article/details/103876431