`THIndexTensor_(size)(target, 0) == batch_size' failed. at d:\projects\pytorch\torch\lib\thnn\generic/ClassNLLCriterion.c:54

Viewed 491

I am trying to train my neural networks on dog breeds data set. After feed-forward, during the loss computation it throws this error :

RuntimeError: Assertion `THIndexTensor_(size)(target, 0) == batch_size' failed.  at d:\projects\pytorch\torch\lib\thnn\generic/ClassNLLCriterion.c:54 

Code:

criterion =nn.CrossEntropyLoss()
optimizer=optim.Adam(net.parameters(),lr=0.001)


for epoch in range(10):  # loop over the dataset multiple times
        running_loss = 0.0
        print(len(trainloader))
        for i, data in enumerate(trainloader, 0):
            # get the inputs
            inputs, labels  = data

            # wrap them in Variable
            inputs, labels = Variable(inputs).float(), Variable(labels).float().type(torch.LongTensor)


            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            outputs = net(inputs)

            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            # print statistics
            running_loss += loss.data[0]
            if i % 2000 == 1999:    # print every 2000 mini-batches
                print('[%d, %5d] loss: %.3f' %
                      (epoch + 1, i + 1, running_loss / 2000))
                running_loss = 0.0

print('Finished Training')

Error is generated in this line :

loss = criterion(outputs, labels)

What's the issue ??

1 Answers

I think problem is that you are missing the batch dimension on the tensor labels. The error says that the size of the 0th dimension is not equal to the batch size.

Try changing this:

loss = criterion(outputs, labels.unsqueeze(0))

Please note, the outputs tensor should have one more dimension than the labels tensor corresponding to a score for each label and the labels should just contain the index of the correct label.

Related