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

Update default behavior of derivative of linalg.norm and abs at x=0 #379

Merged
merged 4 commits into from
Jul 24, 2021

Conversation

rsokl
Copy link
Owner

@rsokl rsokl commented Jul 23, 2021

(I also smuggled in a fix for mygrad.arange that normalizes its interface with that of numpy.arange

Now both mygrad.linalg.norm and mygrad.absolute, by default, will return 0 wherever their derivatives would typically not defined (and would previously have returned nan).
Both of these functions can be passed nan_to_num=False to enable the previous, more-rigorous, behavior.

Before:

>>> import mygrad as mg
>>> x = mg.tensor([-2.0, 0.0, 2.0])

>>> mg.absolute(x).backward()
>>> x.grad
array([-1., nan,  1.])

After:

>>> import mygrad as mg
>>> x = mg.tensor([-2.0, 0.0, 2.0])

>>> mg.absolute(x).backward()
>>> x.grad
array([-1., 0.,  1.])

>>> mg.absolute(x, nan_to_num=False).backward()
>>> x.grad
array([-1., nan,  1.])

@rsokl rsokl changed the title Nan to num Update default behavior derivative of linalg.norm and abs at x=0 Jul 23, 2021
@rsokl rsokl requested a review from petarmhg July 23, 2021 17:54
@rsokl rsokl changed the title Update default behavior derivative of linalg.norm and abs at x=0 Update default behavior of derivative of linalg.norm and abs at x=0 Jul 23, 2021
src/mygrad/linalg/funcs.py Outdated Show resolved Hide resolved
@rsokl rsokl merged commit d4a80b2 into master Jul 24, 2021
@rsokl rsokl deleted the nan-to-num branch July 24, 2021 15:58
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

Successfully merging this pull request may close these issues.

1 participant