I am making a neural network to make a binary classification and I would like to check the predictions made in the testing phase of my network, but I don’t seem to be getting the proper values.
What I want is not the loss over the whole batch but each prediction over every test sample to compare it to the true value.
My testing goes like this :
net.eval()
for i_batch, sample_batched in enumerate(dataloader):
data = Variable(sample_batched['tensor'].view(batch_size, -1, max_size * 20), requires_grad=False, volatile=True)
if gpu_used >= 0:
target = Variable(sample_batched['interaction'].cuda(gpu_used), requires_grad=False, volatile=True)
else:
target = Variable(sample_batched['interaction'], requires_grad=False, volatile=True)
output = net(data)
#test_loss += criterion(output, target).data[0]
test_loss += F.nll_loss(output, target, size_average=False).data[0] #fsize_average=False to sum, instead of average losses
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum() # to operate on variables they need to be on the CPU again
I would appreciate if anyone could give me some pointers to how I could proceed.
The code looks fine, besides some minor unnecessary indexing.
What do you mean by “I don’t seem to be getting the proper values”? Could you explain a bit more about the problem?
Here is a small example, which basically computes, what your code does:
What i mean is I am trying to get every prediction from this throughout the testing and have at the end a list of
prediction : target
to be able to make an F-measure table from the results to have more information on the network’s answer quality.
I am also curious as to what the unnecessary indexing is, I have been working on many iterations of my code and I probably have left things I no longer need, I’d be happy to remove them.
Edit : I just thought that maybe having a testing batch of one would be a solution, wouldn’t it ?
You can add it as a placeholder to indicate you don’t want to use this return value (the max. values) and only want to use the max. indices.
Alternatively, you could also directly use pred = torch.argmax(output, dim=1).