Open ankannn10 opened 4 months ago
No, because this would cause an error:
>>> from micrograd import engine
>>> v = engine.Value(3)
>>> v + 3
Value(data=6, grad=0)
>>> 3 + v
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ps/ugr/micrograd/micrograd/engine.py", line 76, in __radd__
return other + self
File "/home/ps/ugr/micrograd/micrograd/engine.py", line 76, in __radd__
return other + self
File "/home/ps/ugr/micrograd/micrograd/engine.py", line 76, in __radd__
return other + self
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
But modifying __rsub__
and __rtruediv__
can save extra unnecessary calls of __radd__
and __rmul__
:
def __rsub__(self, other): # other - self
return (-self) + other
...
def __rtruediv__(self, other): # other / self
return self**-1 * other
In the micrograd engine.py line number 76, the code should be 'return other + self' not 'return self + other'