Skip to content

Commit eb49369

Browse files
Manually zero the gradients after updating weights by using machine epsilon for standard float (64-bit)
Manually zero the gradients after updating weights by using machine epsilon for standard float (64-bit).
1 parent ab2aafd commit eb49369

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

beginner_source/examples_autograd/polynomial_autograd.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@
6767
d -= learning_rate * d.grad
6868

6969
# Manually zero the gradients after updating weights
70-
a.grad = None
71-
b.grad = None
72-
c.grad = None
73-
d.grad = None
70+
# by using machine epsilon for standard float (64-bit)
71+
import sys
72+
73+
a.grad = loss*sys.float_info.epsilon
74+
b.grad = loss*sys.float_info.epsilon
75+
c.grad = loss*sys.float_info.epsilon
76+
d.grad = loss*sys.float_info.epsilon
7477

7578
print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')

0 commit comments

Comments
 (0)