Unable to figure issue in binary classifier

ulhaqi12: <p>Hi,<br>
I am trying to build a binary classifier to classify pneumonia in radiology images. here is my model architecture<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/8/38e478ef2fa51fb3454056f454be05bcc5958a85.png" data-download-href="https://discuss.pytorch.org/uploads/default/38e478ef2fa51fb3454056f454be05bcc5958a85" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/8/38e478ef2fa51fb3454056f454be05bcc5958a85.png" alt="image" data-base62-sha1="87ieLEFndVoMaq1rzwdq9rexW7P" width="690" height="332" data-dominant-color="F1F1F1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">901×434 48 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>here is code to train the model</p>
<pre><code class="lang-auto">NUM_EPOCHS = 5
BEST_MODEL_PATH = 'best_model.pth'
best_accuracy = 0.0

optimizer = optim.SGD(model.parameters(), lr=0.001)

for epoch in range(NUM_EPOCHS):
    train_error_count=0.0
    for images, labels in iter(train_loader):
        labels = labels.type(torch.FloatTensor)
        images = images.to(device)
        labels = labels.to(device)
        optimizer.zero_grad()
        outputs = model(images)
        train_error_count += float(torch.sum(torch.abs(labels - outputs.argmax(1))))
        outputs = torch.squeeze(outputs)
        loss = nn.BCELoss()(outputs, labels)
        loss.backward()
        optimizer.step()
        
    train_accuracy = 1.0 - float(train_error_count) / float(len(train_dataset))
    
    test_error_count = 0.0
    for images, labels in iter(test_loader):
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        test_error_count += float(torch.sum(torch.abs(labels - outputs.argmax(1))))
    
    test_accuracy = 1.0 - float(test_error_count) / float(len(test_dataset))
    print('Epoch %d: train_acc =%f  test_acc =%f' % (epoch, train_accuracy, test_accuracy))
    if test_accuracy &gt; best_accuracy:
        torch.save(model.state_dict(), BEST_MODEL_PATH)
        best_accuracy = test_accuracy
        print('Best Model Saved!!!')
</code></pre>
<p>I am unable to train it successfully because it is showing the following accuracy.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/5/256c8b514562afb86a630795543f4a92d7ad763d.png" alt="image" data-base62-sha1="5l4dIGlTVh2AtuF3wzkPLLOZi5D" width="627" height="133"></p>
<p>It would be great if anyone can identify a bug. I am a beginner in PyTorch so it would be helpful.<br>
Thank you,</p>

ptrblck: <p><code>outputs.argmax(1)</code> will return an all zero tensor, since your output has the shape <code>[batch_size, 1]</code>.<br>
To get the prediction after a sigmoid layer you could use a threshold such as:</p>
<pre><code class="lang-python">preds = outputs &gt; 0.5
</code></pre>
<p>Afterwards, you could calculate the accuracy or error by comparing the target tensor to this prediction tensor.</p>