Skip to content

Fix NaN gradient in spectrogram#617

Merged
pxl-th merged 2 commits intomasterfrom
pxl-th/nanfix
Dec 22, 2024
Merged

Fix NaN gradient in spectrogram#617
pxl-th merged 2 commits intomasterfrom
pxl-th/nanfix

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
11 of 12 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