title: How to correctly use Numpy's FFT function in PyTorch?
body: <p>I was recently introduced to PyTorch and began running through the library's documentation and tutorials.
  In the <a href="http://pytorch.org/tutorials/advanced/numpy_extensions_tutorial.html" rel="nofollow noreferrer">&quot;Creating extensions using numpy and scipy&quot;</a> tutorial, under &quot;Parameter-less example&quot;, a sample function is created using numpy called <code>BadFFTFunction</code>.</p>
  <p>The description for the function states:</p>
  <blockquote>
  <p>&quot;This layer doesn’t particularly do anything useful or mathematically
  correct.</p>
  <p>It is aptly named BadFFTFunction&quot;</p>
  </blockquote>
  <p>The function and its usage are given as:</p>
  <pre><code>from numpy.fft import rfft2, irfft2
  
  class BadFFTFunction(Function):
  
      def forward(self, input):
          numpy_input = input.numpy()
          result = abs(rfft2(numpy_input))
          return torch.FloatTensor(result)
  
      def backward(self, grad_output):
          numpy_go = grad_output.numpy()
          result = irfft2(numpy_go)
          return torch.FloatTensor(result)
  
  def incorrect_fft(input):
      return BadFFTFunction()(input)
  
  input = Variable(torch.randn(8, 8), requires_grad=True)
  result = incorrect_fft(input)
  print(result.data)
  result.backward(torch.randn(result.size()))
  print(input.grad)
  </code></pre>
  <p>Unfortunately, I was only recently introduced to signal processing as well, and am unsure of where the (likely obvious) error is in this function.</p>
  <p>I am wondering, how might one go about fixing this function so that its forward and backward outputs are correct?</p>
  <p>How can <code>BadFFTFunction</code> be fixed so that a differentiable FFT function can be used in PyTorch?</p>