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

_backward as lambdas? #28

Open
ondras opened this issue Feb 1, 2023 · 3 comments
Open

_backward as lambdas? #28

ondras opened this issue Feb 1, 2023 · 3 comments

Comments

@ondras
Copy link

ondras commented Feb 1, 2023

Hi @karpathy,

congratulations on this repo/talk. The educational value is truly immense. Good job!

Can you please explain the main motivation for _backward methods implemented as lambdas, as opposed to (one) regular method that starts with a hypothetical switch (self._op) and contains implementation for all arithmetic cases?

@steve-z-seattle
Copy link

steve-z-seattle commented Feb 25, 2023

@ondras, That is a very interesting question. IMHO, it is less about lambda, and more about closure. It seems that the alternative approach will need some tedious unpacking, while the implementation here has the advantage to be more concise, as all the variables are conveniently available (typically self/x, other/y, and out) for gradient updates.

For the alternative implementation, please refer to my repo (WIP): https://github.com/steve-z-seattle/undergrad. The alternative implementation does not use lambdas/closure. Working in progress though.

At this point, I do think the closure implementation is more elegant.

@ondras
Copy link
Author

ondras commented Mar 2, 2023

Hi @steve-z-seattle,

thanks for your opinion. I will have a look at your repo.

@ElHadi73
Copy link

ElHadi73 commented Apr 8, 2024

def _backward():
self.grad = (1 - t**2) * out.grad
out._backward = _backward
return out

can anyone explain how this one work, it really confused me, i understand the math, but i didnt understand the access to it, and stuff, like how it is being accessed to and what variables the function will have access to later

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

3 participants