Skip to content

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

Merged
rsokl merged 4 commits into
masterfrom
nan-to-num
Jul 24, 2021
Merged

Update default behavior of derivative of linalg.norm and abs at x=0#379
rsokl merged 4 commits into
masterfrom
nan-to-num

Conversation

@rsokl

@rsokl rsokl commented Jul 23, 2021

Copy link
Copy Markdown
Owner

(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
Comment thread src/mygrad/linalg/funcs.py Outdated
@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