NLLLoss malfunction when target is torch.from_numpy()

Hey guys,

I’m having this weird error with NLLLoss:

The predicted and target outputs are the following:

Predicted

Variable containing:
-2.8742 -0.0581
-2.1152 -0.1285
-2.8361 -0.0604
-3.5815 -0.0282
-3.4316 -0.0329
-3.9429 -0.0196
-2.6402 -0.0740
-2.7773 -0.0642
-2.3009 -0.1056
-1.7565 -0.1895
[torch.FloatTensor of size 10x2]

Target

Variable containing:
    1
    0
    1
    1
    1
    1
    0
    0
    0
    1
[torch.LongTensor of size 10x1]

And I get the following error:

RuntimeError: multi-target not supported at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/THNN/generic/ClassNLLCriterion.c:20

I tried with toy examples before and the formats seem pretty much valid. I had a different version of the model running and it was working… Is this some kind of pytorch bug?

Best regards,
Miguel

EDIT

It only happens when I load the target output from numpy!

I was doing:
target = Variable(torch.from_numpy(np.random.randint(2, size=(10, 1))))

This is definitely something that should not happen…

the targets should be 1 dimension less than outputs for NLLLoss, as they are integer values from 0 to nClasses-1.
So in your case, target should be 1D tensor of size 10

1 Like

I guess that’s exactly what I was doing…

It may be related with the way numpy represents 1D vectors (either as column or line vector)

you can try:
target = Variable(torch.from_numpy(np.random.randint(2, size=(10))))
or
target = Variable(torch.squeeze(torch.from_numpy(np.random.randint(2, size=(10, 1)))))