IndexError: Target 1 is out of bounds (NLLoss)

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F

class Net(nn.Module):
    
    def __init__(self):
        super(Net, self).__init__()
        self.lstm=nn.LSTM(10,120)
        self.output=nn.Linear(120,10)

    def forward(self, x):
        x,h = self.lstm(x)
        x = F.softmax(self.output(x))
        return x,h
    
net=Net()

inputs=[]
outputs=[]

for i in range(0,9):
    
    one=torch.zeros([10])
    one[i]=1
    inputs.append(one)
    
    one=torch.zeros(10)
    one[i+1]=1
    outputs.append(one)
    
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
    
for j in range(0,10):    

     optimizer.zero_grad()
     forward,hidden=net(torch.stack(inputs).view((9,1,10)))
     print(forward.shape)
     #print(outputs)
     loss = criterion(forward,torch.stack(outputs).long())
     print(loss)

Complete Error trace


  File "<ipython-input-70-ffecb9d328f7>", line 1, in <module>
    runfile('/Users/hrishikesh/Hrishikesh/Projects/Major Project/ASR/ann.py', wdir='/Users/hrishikesh/Hrishikesh/Projects/Major Project/ASR')

  File "/Users/hrishikesh/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "/Users/hrishikesh/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/hrishikesh/Hrishikesh/Projects/Major Project/ASR/ann.py", line 42, in <module>
    loss = criterion(forward,torch.stack(outputs).long())

  File "/Users/hrishikesh/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)

  File "/Users/hrishikesh/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)

  File "/Users/hrishikesh/opt/anaconda3/lib/python3.7/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 "/Users/hrishikesh/opt/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py", line 1863, in nll_loss
    input, target, weight, reduction_enum, ignore_index)

IndexError: Target 1 is out of bounds.

CrossEntropyLoss() requires a 1-D tensor of class indices of the target (outputs in your case) but not one-hot vectors of the target.
The shape of the target should be [mini_batch_size], but in your case it happens to be [mini_batch_size,num_classes].
That is why you are getting IndexError: Target 1 is out of bounds.
Go through the official documentation for more details(link)

1 Like