Simple image classification model, why this error?

I try to output internal layers’ output of a cnn, but why the “preds” is not a Variable? How to solve this?

class CNN(nn.Module):
    def __init__(self, channel):
        super(CNN, self).__init__()
        self.pretrained = models.resnet18(pretrained=True)
        self.modules = list(self.pretrained.children())[:-3]
        self.resnet = nn.Sequential(*self.modules)
        self.layer1 =nn.AvgPool2d(14)
        self.layer2 = nn.Linear(channel,4)
    def forward(self,input):
        o0 = self.resnet(input)
        o1 = self.layer1(o0)
        o2 = o1.squeeze(2)
        o2 = o2.squeeze(2)
        o3 = self.layer2(o2)
        return o0,o2,o3
input=Variable(torch.FloatTensor(3,3,224,224)).cuda()
cnn=CNN(256).cuda()

att_feats,fc_feats,outputs = cnn(input)
print(outputs)
_ ,preds = torch.max(outputs.data,1)
print(preds)
target = Variable(torch.LongTensor(3,4)).cuda()
criterion=nn.CrossEntropyLoss().cuda()
loss= criterion(outputs,target)

the error looks like:

Variable containing:
-0.1038 0.0071 0.0303 0.0366
-0.0857 -0.0365 0.0138 0.0473
-0.0920 -0.0510 0.0131 0.0482
[torch.cuda.FloatTensor of size 3x4 (GPU 0)]

3
3
3
[torch.cuda.LongTensor of size 3x1 (GPU 0)]
RuntimeError: multi-target not supported at /home/jcc/pytorch/torch/lib/THCUNN/generic/ClassNLLCriterion.cu:17

The docs say that the second return value of torch.max is indeed a LongTensor. Does wrapping it in a Variable work for you? ie
loss = criterion(preds, Variable(target))

loss= criterion(Variable(preds),target)

cause this error:

KeyError: <class ‘torch.cuda.LongTensor’>

sorry, it should be loss= criterion(outputs,target), but still error

Sorry, I didn’t read the error message in your above post. The error message suggests that target should be a vector, not a 3x1 tensor – you can fix that with target.squeeze(1)

Hello, am receiving this error with some fastai custom classes.
Have not discovered where to try to squeeze the target.
In case you are familiar with the package it involves ColumnModelData and
a StructuredLearner with CrossEntropyLoss.

Not sure this is the error type but it is my top suspect.

What is the error message?

So sorry, seems I forgot it:

-> 1052 return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
1053 elif dim == 4:
1054 return torch._C._nn.nll_loss2d(input, target, weight, size_average, ignore_index, reduce)

RuntimeError: multi-target not supported at /opt/conda/conda-bld/pytorch_1518244421288/work/torch/lib/THNN/generic/ClassNLLCriterion.c:22