AttributeError: module ‘torch.fft’ has no attribute ‘fftfreq’

I followed the example from https://pytorch.org/docs/stable/generated/torch.fft.fftshift.html#torch.fft.fftshift

import torch.fft
f = torch.fft.fftfreq(4)
a = torch.fft.fftshift(f)
print(a)

and got the error

AttributeError: module ‘torch.fft’ has no attribute ‘fftfreq’

I tried pip torch==1.7.0+cu110 and pip torch==1.7.1+cu110 and also conda pytorch==1.7.1 with cudatoolkit=11.0. Others have the same problem https://discuss.pytorch.org/t/unable-to-use-correctly-the-new-torch-fft-module/104560/6 But changing to torch1.7.0 didn’t solve the problem.

How to use torch.fft correctly?

Answer

Function torch.fft.fftfreq was introduced in PyTorch version 1.8.0. You need to upgrade to this version or higher in order to use it.