How to implement Softmax and NLL in pytorch?

I’m having as a last layer 2 channel

self.crosschannel = nn.Sequential(
            nn.Conv2d(512, 2, 1, stride=1, padding=0),
            nn.ELU(),
            nn.AvgPool2d(15, 20)
        ) 

then in my forward function I’m applying view to convert it into (batchsize,2)

def forward(self, x):
    .
    x = self.crosschannel(x)
    x = x.view(-1,2)
    return x

So in training procedure I have

criterion = nn.CrossEntropyLoss()
d_real_error = criterion(D_prediction, labels)

But it is not working
First with this error
RuntimeError: Expected object of type Variable[CUDALongType] but found type Variable[CUDAFloatType] for argument #1 'target’
why should i use long ?

anyway in this case I won’t have the value of the softmax to calculate the validation error

This will probably help resolve your issue: criterion(D_prediction, labels.type(torch.LongTensor).cuda())