w = torch.tensor(2.0, requires_grad=True) b = torch.tensor(1.0, requires_grad=True) x = torch.tensor(3.0) y = w * x + b # 2·3 + 1 = 7 loss = (y - 5) ** 2 # (7 - 5)² = 4 loss.backward() print(w.grad) # tensor(12.) print(b.grad) # tensor(4.)