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

NaN in gradient of abs() on complex 0 #1472

Closed
koenvos opened this issue Nov 21, 2023 · 1 comment
Closed

NaN in gradient of abs() on complex 0 #1472

koenvos opened this issue Nov 21, 2023 · 1 comment

Comments

@koenvos
Copy link

koenvos commented Nov 21, 2023

using Zygote
x = [0f0 + 1im * 0f0]
f(x) = sum(abs.(x))
f'(x)

result:
1-element Vector{ComplexF32}:
NaN32 + NaN32*im

@koenvos
Copy link
Author

koenvos commented Nov 22, 2023

Seems like it's caused by the sqrt() (on the sum of squared real and imaginary parts).
Fixed by adding a small offset:
abs_(x::Complex) = sqrt(real(x) ^ 2 + imag(x) ^ 2 + 1f-12)

@koenvos koenvos closed this as completed Nov 22, 2023
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