From aade91ce8e424eee2570ea1fb2ce21f9759516ad 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:35:46 +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_custom_function.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/beginner_source/examples_autograd/polynomial_custom_function.py b/beginner_source/examples_autograd/polynomial_custom_function.py index 39057c8fd7..72df7c3f1f 100755 --- a/beginner_source/examples_autograd/polynomial_custom_function.py +++ b/beginner_source/examples_autograd/polynomial_custom_function.py @@ -74,7 +74,7 @@ def backward(ctx, grad_output): d = torch.full((), 0.3, device=device, dtype=dtype, requires_grad=True) learning_rate = 5e-6 -for t in range(2000): +for t in range(int(1/(learning_rate))): # To apply our Function, we use Function.apply method. We alias this as 'P3'. P3 = LegendrePolynomial3.apply @@ -98,9 +98,11 @@ def backward(ctx, grad_output): 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()} * P3({c.item()} + {d.item()} x)')