You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用torch.fft模块时遭遇AttributeError: module 'torch.fft' has no attribute 'fftfreq'错误的技术求助

Fixing AttributeError: module 'torch.fft' has no attribute 'fftfreq' in PyTorch 1.7.x

Let me break down what's going on here and how to fix it quickly:

The Root Cause

The PyTorch documentation you're referencing is for the stable (current) version, which has moved fftfreq into the torch.fft submodule. But in PyTorch 1.7.x (the versions you're using), fftfreq hasn't been migrated there yet—it still lives directly under the main torch namespace.

Solution 1: Keep using PyTorch 1.7.x

Modify your code to call torch.fftfreq() instead of torch.fft.fftfreq():

import torch
import torch.fft

f = torch.fftfreq(4)  # Note: no .fft submodule here
a = torch.fft.fftshift(f)
print(a)

This should run without errors in PyTorch 1.7.x, since fftshift is part of the new torch.fft module in 1.7, even if fftfreq isn't.

Solution 2: Upgrade to PyTorch 1.8+

If you're able to upgrade your PyTorch version to 1.8 or newer, you can use the code exactly as shown in the official documentation:

import torch.fft

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

Upgrading will align your environment with the stable docs and avoid similar inconsistencies with other FFT-related functions going forward.

A Quick Note on Docs

Always double-check the documentation version that matches your installed PyTorch release. For PyTorch 1.7.x, the fftfreq function is documented under the main torch namespace, not torch.fft.

内容的提问来源于stack exchange,提问作者tag: python, pytorch, fft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:28:15