From 02e5a75dc17eec70591fbfc71b6e53b390083c3a Mon Sep 17 00:00:00 2001 From: "Bintang Alam Semesta W.A.M" <23573683+bintang-aswam@users.noreply.github.com> Date: Tue, 17 Jun 2025 07:25:15 +0700 Subject: [PATCH] (manually) update zero gradients after updating the weights Previous version: a.grad, b.grad, c.grad, d.grad = None, None, None, None instead it's appropriate to just set 0. instead of "None" as follows: a.grad, b.grad, c.grad, d.grad = 0. , 0. , 0. , 0. --- beginner_source/examples_autograd/polynomial_autograd.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/beginner_source/examples_autograd/polynomial_autograd.py b/beginner_source/examples_autograd/polynomial_autograd.py index 525d0c33ce9..a3715de4d94 100755 --- a/beginner_source/examples_autograd/polynomial_autograd.py +++ b/beginner_source/examples_autograd/polynomial_autograd.py @@ -67,9 +67,10 @@ d -= learning_rate * d.grad # Manually zero the gradients after updating weights - a.grad = None - b.grad = None - c.grad = None - d.grad = None + #a.grad = None + #b.grad = None + #c.grad = None + #d.grad = None + a.grad, b.grad, c.grad, d.grad = 0. , 0. , 0. , 0. print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')