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

autograd return nan with to norm function #617

Open
giangbang opened this issue Mar 18, 2024 · 0 comments
Open

autograd return nan with to norm function #617

giangbang opened this issue Mar 18, 2024 · 0 comments

Comments

@giangbang
Copy link

Hi, I'm using autograd to calculate the gradient of an l2 norm operator, the code is as simple as

def f(x):
    return np.linalg.norm(x,axis=-1)**2

f_dx = grad(f)
f_dx(np.array([[0, 0.]]))

However, when I substitute vector 0 to f, it outputs nan

>> \autograd\numpy\linalg.py:100: RuntimeWarning: invalid value encountered in scalar divide                                                          
  return expand(g / ans) * x 

When I change the code to something that does not involve using linalg, it produces 0 as usual

def f(x):
    return np.sum(np.square(x))
f_dx = grad(f)
f_dx(np.array([[0, 0]], dtype=float))
array([[0., 0.]])

return expand(g / ans) * x

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

1 participant