Optimizing with BCE is not working, nothing will change
I have the following code:
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
import matplotlib.pyplot as plt
import os
import keras
from random import choice
import sys
devicet = 'cuda' if torch.cuda.is_available() else 'cpu'
device = torch.device(devicet)
if devicet == 'cpu':
  print ('Using CPU')
else:
  print ('Using GPU')
cuda0 = torch.device('cuda:0')
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.step1 = nn.Linear(5, 25)
        self.step2 = nn.Linear(25, 50)
        self.step3 = nn.Linear(50, 100)
        self.step4 = nn.Linear(100, 100)
        self.step5 = nn.Linear(100, 10)
        self.step6 = nn.Linear(10, 1)
    def forward(self, x):
      x = F.relu(x)
      x = self.step1(x)
      x = F.relu(x)
      x = self.step2(x)
      x = F.relu(x)
      x = self.step3(x)
      x = F.relu(x)
      x = self.step4(x)
      x = F.relu(x)
      x = self.step5(x)
      x = F.relu(x)
      x = self.step6(x)
      x = F.relu(x)
      return (x)
net = Net()
x = torch.rand(10,5)
num = choice(range(10))
zero_tensor = torch.zeros(num, 1)
one_tensor = torch.ones(10-num, 1)
y = torch.cat((zero_tensor,one_tensor),0)
x.to(devicet)
y.to(devicet)
learning_rate = 1e-3
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
loss_fn = torch.nn.BCELoss()
acc_list = []
for i in tqdm(range(1000),desc='Training'):
    y_pred = net(x)
    loss = loss_fn(y_pred, y)
    loss.backward()
    optimizer.step()
    acc_list.append(abs(net(x).detach().numpy()[0]-y.detach().numpy()[0]))
    with torch.no_grad():
        for param in net.parameters():
            param -= learning_rate * param.grad
    optimizer.zero_grad()
print ('\nFinished training in {} epochs.'.format(len(acc_list)))
plt.plot(range(len(acc_list)),acc_list)
plt.show()
for i in range(10):
  print (str(net(x).detach().numpy()[i][0])+', '+str(y.detach().numpy()[i][0]))

When I run this, it consistently just prints out the following:
Image
Why won't it do any training? It works if I use MSE loss (actually, it only works sometimes with MSE loss, sometimes it does the same thing as in the image) , it's only when I use BCE that it stops working entirely.

Final layer activation
You are outputting only positive values, those should be between 0 and 1 for starters, these line specifically:
x = F.relu(x)
return (x)

Use torch.sigmoid with BCELoss or even better, just output x and use torch.nn.BCEWithLogitsLoss which uses logits directly
Training
You are using Adam optimizer and doing SGD manually here:
with torch.no_grad():
    for param in net.parameters():
        param -= learning_rate * param.grad

Essentially you are applying optimization step twice which is probably too much and might destroy the weights.
optimizer.step() already does this, no need for both!
Accuracy
This part:
abs(net(x).detach().numpy()[0]-y.detach().numpy()[0])

I assume you want to calculate accuracy, it would go like this (also do not push data through the network twice via net(x), you already have y_pred!):
# Assuming sigmoid activation
def accuracy(y_pred, y_true):
    # For logits use
    # predicted_labels = y_pred &gt; 0.0
    predicted_labels = y_pred &gt; 0.5
    return torch.mean((y_true == predicted_labels).float())

