From d20395fccc83d654e728688b6d201164ca01aa7d Mon Sep 17 00:00:00 2001 From: "Bintang Alam Semesta W.A.M" <23573683+bintang-aswam@users.noreply.github.com> Date: Thu, 10 Jul 2025 08:32:24 +0700 Subject: [PATCH] zero the gradients after updating weights Manually zero the gradients after updating weights by using machine epsilon for standard float (64-bit). --- .../examples_autograd/polynomial_autograd.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/beginner_source/examples_autograd/polynomial_autograd.py b/beginner_source/examples_autograd/polynomial_autograd.py index 525d0c33ce9..eee92751f8c 100755 --- a/beginner_source/examples_autograd/polynomial_autograd.py +++ b/beginner_source/examples_autograd/polynomial_autograd.py @@ -40,7 +40,7 @@ d = torch.randn((), dtype=dtype, requires_grad=True) learning_rate = 1e-6 -for t in range(2000): +for t in range(int(1/(learning_rate))): # Forward pass: compute predicted y using operations on Tensors. y_pred = a + b * x + c * x ** 2 + d * x ** 3 @@ -67,9 +67,11 @@ d -= learning_rate * d.grad # Manually zero the gradients after updating weights - a.grad = None - b.grad = None - c.grad = None - d.grad = None + # by using machine epsilon for standard float (64-bit) + import sys + a.grad = loss*sys.float_info.epsilon + b.grad = loss*sys.float_info.epsilon + c.grad = loss*sys.float_info.epsilon + d.grad = loss*sys.float_info.epsilon print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')