Fourier transform in latest pytorch

Hi, I have a code that’s written for older versions of PyTorch, and I get the following error when I run it on the latest version of PyTorch. Is there any way to fix the issue for the latest version of PyTorch?

The code:

fft_src = torch.rfft(src_img.clone(), signal_ndim=2, onesided=False)

The error:

AttributeError: module 'torch' has no attribute 'rfft'

Hi Mohammad,

for some releases, PyTorch is trying to move closer to the NumPy API where it provides equivalent functions. The deprecation notice in PyTorch 1.7 (which I had to look up myself, so don’t feel bad about not seeing it), your use with onesided=False should be replaced by torch.fft.fft, but for 2d I think torch.fft.fft2 might be a better fit, though I did not try.

If you want to verify that it is working as expected and you have access to an old version of PyTorch, you could try to replicate the old PyTorch behaviour using NumPy’s numpy.fft.fft2 and once you have this install the new version and use the compatible NumPy command to replicate in new PyTorch (and the “compatible API” idea is that this is very easy). If you want to know at which version to split if you need to support both, I would use the new for 1.7+ and the old for up to 1.6. Though these versions seem pretty old, so if you don’t have to support them…

Best regards


1 Like

Hi @tom, thank you very much for your detailed reply. I actually ended up creating a new venv with an older version of PyTorch that supports torch.rfft. And, I realized there is no function in the newer version of PyTorch that can reproduce the results that I want. So, I’m just using an older version of PyTorch for this specific run. Thanks again!