Getting the proper prediction and comparing it to the true value

Freyj: <p>Hello,</p>
<p>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.<br>
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.<br>
My testing goes like this :</p>
<pre><code class="lang-auto">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 &gt;= 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
</code></pre>
<p>I would appreciate if anyone could give me some pointers to how I could proceed.</p>

ptrblck: <p>The code looks fine, besides some minor unnecessary indexing.<br>
What do you mean by “I don’t seem to be getting the proper values”? Could you explain a bit more about the problem?</p>
<p>Here is a small example, which basically computes, what your code does:</p>
<pre><code class="lang-auto">batch_size = 10
n_classes = 5
output = F.log_softmax(Variable(torch.randn(batch_size, n_classes)), dim=1)
target = Variable(torch.LongTensor(batch_size).random_(n_classes))

_, pred = torch.max(output, dim=1)

pred.eq(target)
</code></pre>

Freyj: <p>Thank you for your quick reply <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=5" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>What i mean is I am trying to get every prediction from this throughout the testing and have at the end a list of</p>
<pre><code class="lang-auto">prediction : target
</code></pre>
<p>to be able to make an F-measure table from the results to have more information on the network’s answer quality.</p>
<p>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.</p>
<p>Edit : I just thought that maybe having a testing batch of one would be a solution, wouldn’t it ?</p>

ptrblck: <p>It’s probably user’s preference, but I would remove the <code>keepdims=True</code> ans <code>.view_as</code>, like in my code. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=5" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Ah ok, I understand.<br>
You could just store them in a list.</p>
<pre><code class="lang-auto">preds = []
targets = []
for i in range(10):
    output = F.log_softmax(Variable(torch.randn(batch_size, n_classes)), dim=1)
    target = Variable(torch.LongTensor(batch_size).random_(n_classes))

    _, pred = torch.max(output, dim=1)
    preds.append(pred.data)
    targets.append(target.data)

preds = torch.cat(preds)
targets = torch.cat(targets)
</code></pre>
<p>Or you could of course cast them to <code>numpy</code> arrays, if you would like to calculate statistics using another framework.</p>

Freyj: <p>Thanks, I don’t know why I didn’t think of storing them in a list <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=5" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"> .</p>
<p>Have a great day !</p>

Angelina_Robert: <p>Thank you very much ptrblck, I will try to incorporate it in my work.</p>
<p>Heaps, heaps of thanks and stay blessed.</p>
<p>Cheers.</p>

P42: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="16468">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>_, pred = torch.max(output, dim=1)</code></p>
</blockquote>
</aside>
<p>Hi ,<br>
can someone help me in understanding this line of code.Why do we use _,pred.</p>
<p>Thanks in advance</p>

ptrblck: <p>You can use <code>pred</code> to get the predicted classes from the output (logits or probabilities) of the model in a multi-class classification use case.</p>

P42: <p>Thank you .<br>
why do we add  "_, "</p>

ptrblck: <p>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.<br>
Alternatively, you could also directly use <code>pred = torch.argmax(output, dim=1)</code>.</p>

P42: <p>Thank you so much for the help</p>