import torch import torch.nn.functional as F from torch.autograd import grad # Define our data and initial parameters y = torch.tensor([1.0]) # Ground truth label x1 = torch.tensor([1.1]) # Input feature w1 = torch.tensor([2.2], requires_grad=True) # Trainable weight b = torch.tensor([0.0], requires_grad=True) # Trainable bias # Forward pass: compute prediction z = x1 * w1 + b # Linear combination a = torch.sigmoid(z) # Sigmoid activation print(f"Prediction: {a.item():.4f}") # Compute loss loss = F.binary_cross_entropy(a, y) print(f"Loss: {loss.item():.4f}") # Method 1: Manual gradient computation grad_w1 = grad(loss, w1, retain_graph=True)[0] grad_b = grad(loss, b, retain_graph=True)[0] print(f"Weight gradient: {grad_w1.item():.4f}") print(f"Bias gradient: {grad_b.item():.4f}") __ __