Skip to content

Adding the derivative of fft #410

@dsweber2

Description

@dsweber2

I've talked with some folks on the gitter who suggested that I could do this via the @grad method, as in several of the examples in tracker/array.jl. Given that in 1D the partial derivative is just ∂fft(x)_j/∂x_k is just e^{2πik*j/N), if I'm reading how @grad works correctly, it should just be

@grad function fft(xs)
  fft(data(xs)), function (Δ)
    ns = size(xs,1)
    ω = [exp(2π*im*j*k/ns) for j=0:(ns-1), k=0:(ns-1)]
    ω*Δ
  end
end

Here's something I've written to test it:

W = randn(5,5)
f(x) = abs(sum(fft(W*x)))
Wp = param(W)
grads = Tracker.gradient(() -> f(W), Params([Wp]))
grads[Wp]

Which is currently returning zeros. However, the comment I've added on #217 suggests this isn't due to the way I implemented the grad here, but something about the way complex numbers are handled in the Tracker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions