karpathy / micrograd

A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API
MIT License
10.5k stars 1.52k forks source link

radd #76

Open ankannn10 opened 4 months ago

ankannn10 commented 4 months ago

In the micrograd engine.py line number 76, the code should be 'return other + self' not 'return self + other'

conscell commented 1 month 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