Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

radd #76

Open
ankannn10 opened this issue Jul 12, 2024 · 1 comment
Open

radd #76

ankannn10 opened this issue Jul 12, 2024 · 1 comment

Comments

@ankannn10
Copy link

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

@conscell
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants