PyTorch Linear MINST model training error
I am creating a binary classifier based on the MINST dataset using PyTorch. I want my classifier to classify between only 0s and 1s, however, when I train it, the error doesn't decrease and the loss becomes negative.
Here's the error and loss at the first few iterations:

I was obviously expecting better results.
Here is the code I am using:
# Loading the MNISR data reduced to  the 0/1 examples

from torchvision import datasets, transforms
from torch.utils.data import DataLoader

mnist_train = datasets.MNIST(&quot;./data&quot;, train=True, download=True, transform=transforms.ToTensor())
mnist_test = datasets.MNIST(&quot;./data&quot;, train=False, download=True, transform=transforms.ToTensor())

train_idx = mnist_train.train_labels &lt;= 1
try:
    mnist_train.train_data = mnist_train.train_data[train_idx]
except AttributeError:
    mnist_train._train_data = mnist_train.train_data[train_idx]
try:
    mnist_train.train_labels = mnist_train.train_labels[train_idx]
except AttributeError:
    mnist_train._train_labels = mnist_train.train_labels[train_idx]

test_idx = mnist_test.test_labels &lt;= 1
try:
    mnist_test.test_data = mnist_test.test_data[test_idx]
except AttributeError:
    mnist_test._test_data = mnist_test.test_data[test_idx]
try:
    mnist_test.test_labels = mnist_test.test_labels[test_idx]
except AttributeError:
    mnist_test._test_labels = mnist_test.test_labels[test_idx]
        
train_loader = DataLoader(mnist_train, batch_size = 100, shuffle=True)
test_loader = DataLoader(mnist_test, batch_size = 100, shuffle=False)


# Creating a simple linear classifier

import torch
import torch.nn as nn
import torch.optim as optim

# do a single pass over the data
def epoch(loader, model, opt=None):
    total_loss, total_err = 0.,0.
    for X,y in loader:
        yp = model(X.view(X.shape[0], -1))[:,0]
        loss = nn.BCEWithLogitsLoss()(yp, y.float())
        if opt:
            opt.zero_grad()
            loss.backward()
            opt.step()
        
        total_err += ((yp &gt; 0) * (y==0) + (yp &lt; 0) * (y==1)).sum().item()
        total_loss += loss.item() * X.shape[0]
    return total_err / len(loader.dataset), total_loss / len(loader.dataset)


model = nn.Linear(784, 1)
opt = optim.SGD(model.parameters(), lr=1)
print(&quot;Train Err&quot;, &quot;Train Loss&quot;, &quot;Test Err&quot;, &quot;Test Loss&quot;, sep=&quot;\t&quot;)
for i in range(10):
    train_err, train_loss = epoch(train_loader, model, opt)
    test_err, test_loss = epoch(test_loader, model)
    print(*(&quot;{:.6f}&quot;.format(i) for i in (train_err, train_loss, test_err, test_loss)), sep=&quot;\t&quot;)

I don't know why my error does not decrease nor why my loss keeps getting more negative. Does anyone spot the error?

I found the error. My initial code to select only 1s and 0s from the MNIST dataset didn't work. So obviously, applying BCELoss to a non-binary dataset was making the model fail.
