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

Fix NaN gradient in spectrogram #617

Merged
merged 2 commits into from
Dec 22, 2024
Merged

Fix NaN gradient in spectrogram #617

merged 2 commits into from
Dec 22, 2024

Conversation

pxl-th
Copy link
Member

@pxl-th pxl-th commented Dec 21, 2024

With power > 0 gradient will become NaN where spectrogram is ~0 (because of singularity at abs(0)).
Adding small epsilon fixes this issue (and the results match pytorch).

x = gpu(ones(Float32, 1024, 1, 1))
window = gpu(hann_window(1024)))
l, g = Zygote.withgradient(x) do x
    sum(spectrogram(x; n_fft=1024, hop_length=256, power=2.0, window)
end

PR Checklist

  • Tests are added
  • Documentation, if applicable

@pxl-th pxl-th changed the title Fix NaNs Fix NaN gradient in spectrogram Dec 21, 2024
Copy link
Member

@mcabbott mcabbott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fine, a test would be great.

@pxl-th pxl-th merged commit be9c1c8 into master Dec 22, 2024
12 of 13 checks passed
@pxl-th pxl-th deleted the pxl-th/nanfix branch December 22, 2024 13:04
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.

2 participants