PyTorch: _thnn_nll_loss_forward is not implemented for type torch.LongTensor
When trying to create a model using PyTorch, when I am trying to implement the loss function nll_loss, it is throwing the following error

RuntimeError: _thnn_nll_loss_forward is not implemented for type torch.LongTensor 


The fit function I have created is:

for epoch in tqdm_notebook(range(1, epochs+1)):
    for batch_idx, (data, targets) in enumerate(train_loader):
        optimizer.zero_grad()
        net.float()
        output = net(data)
        output_x = output.argmax(dim=2) #to convert (64,50,43) -&gt; (64, 50)
        loss = F.nll_loss(output_x, targets)
        loss.backward()
        optimizer.step()
        if batch_idx % 100 == 0:
            print('Train epochs: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx*len(data), len(ds.data),
                100.*batch_idx / len(ds), loss.item()
            ))


Where the output and targets' shape is (64, 50) and the dtypes are torch.int64 for both.

Look at the description of F.nll_loss. It expects to get as input not the argmax of the prediction (type torch.long), but rather the full 64x50x43 prediction vectors (of type torch.float). Note that indeed the prediction you provide to F.nll_loss has an extra dimension more than the ground truth targets you provide.

In your case, simply remove the argmax:

loss = F.nll_loss(output, targets)

