2

I'd like to perform a direct/inverse Fourier transform in TensorFlow. In particular, I want to write it as a function that I can easily integrate into a neural network, which must be differentiable.

In practice, I want to be able to write something like:

x = tf.layers.conv2d(input_tensor)
x = tf.nn.relu(x)

X = fourier_transform(x)
Y = X + something_else
y = inverse_fourier_transform(Y)

z = tf.layers.conv2d(y)
z = tf.nn.relu(z)

....

I've found implementations of fft2d for the FFT, and of ifft2d and irfft2d for the inverse FFT, but I'm not sure if they are differentiable. Moreover, I don't know the difference between ifft and irfft.

Thanks,

G.

gab
  • 792
  • 1
  • 10
  • 36

1 Answers1

2

An FFT is simply an efficient algorithm (using a factorization) for computing a DFT.

A DFT is simply a multiplication by a special complex-valued square matrix or a basis transform (thus linear and differentiable, since the DFT matrix basis vectors are orthogonal, thus non-degenerate).

An IFFT is the inverse transform of the full complex result of an FFT. An irfft, assumes the FFT vector is conjugate symmetric (thus the IRFFT only needs half the FFT data, since the other half is redundant) which produces a strictly real IFFT result (thus not needing any imaginary components in the complex IFFT result). So both the input and output of an irfft can be half the size of an IFFT.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Thank you :) Based on your answer, it seems that if I wanted to use the Fourier transform in a differentiable neural network I should go with DFT to have it differentiable. But how would I handle the imaginary parts? – gab May 16 '20 at 10:05
  • @hotpaw2 Can I interest you in a [bounty for a 2D FFT question](https://stackoverflow.com/q/61718126/176769)? – karlphillip May 17 '20 at 21:33