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

Bug when raising zero to powers? #581

Open
yairchu opened this issue Aug 31, 2022 · 0 comments
Open

Bug when raising zero to powers? #581

yairchu opened this issue Aug 31, 2022 · 0 comments

Comments

@yairchu
Copy link

yairchu commented Aug 31, 2022

Consider these two seemingly equivalent functions:

def fa(x):
    return x ** 1.5

def fb(x):
    return x * x ** 0.5

We can see that one of them is differentiated ok, while the other produces warnings and nans at zero:

>>> [ga, gb] = map(autograd.grad, [fa, fb])
>>> print(ga(2.), ga(0.))
2.121320343559643 0.0
>>> print(gb(2.), gb(0.))
.../autograd/numpy/numpy_vjps.py:59: RuntimeWarning: divide by zero encountered in power
  lambda ans, x, y : unbroadcast_f(x, lambda g: g * y * x ** anp.where(y, y - 1, 1.)),
.../autograd/numpy/numpy_vjps.py:59: RuntimeWarning: invalid value encountered in double_scalars
  lambda ans, x, y : unbroadcast_f(x, lambda g: g * y * x ** anp.where(y, y - 1, 1.)),
2.121320343559643 nan

I'm not sure whether this is a proper fix, but if in the defvjp call of anp.power we change anp.where(y, y - 1, 1.) to anp.where(x, anp.where(y, y - 1, 1.), 1.) then gb(0.) does produce the same result as ga(0.)

I concede that 0 ** -0.5 and ZeroDivisionError in general is a delicate topic. But my fix still seems consistent to me. 🤷‍♂️

yairchu added a commit to soundradix/autograd that referenced this issue Aug 31, 2022
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
@yairchu and others