import torch
n_item, n_feature = 1000, 2
learning_rate = 0.01
epochs = 100
batch_size = 32
torch.manual_seed(123)
data_x = torch.randn(size=(n_item, n_feature)).float()
data_y = torch.where(data_x[:, 0]*0.5 - data_x[:, 1]*1.5 > 0, 1., 0.).float().view(-1, 1)
w = torch.randn(size=(n_feature, 1), requires_grad=True)
b = torch.zeros(size=(1, 1), requires_grad=True)
for epoch in range(epochs):
for i in range(0, n_item, batch_size):
x_batch = data_x[i:i+batch_size]
y_batch = data_y[i:i+batch_size]
z = x_batch @ w + b
y_hat = torch.sigmoid(z)
loss = -(y_batch*torch.log(y_hat) + (1-y_batch)*torch.log(1-y_hat)).mean()
loss.backward()
with torch.no_grad():
w -= learning_rate * w.grad
b -= learning_rate * b.grad
w.grad.zero_()
b.grad.zero_()
print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")