【发布时间】:2022-03-31 15:06:15
【问题描述】:
我正在尝试制作一些适用于 pytorch 1.5.0 的 python3 代码也可以在较新的版本上正常工作(我目前使用的是 pytorch 1.9.0)。更具体地说,我正在尝试更新执行快速傅立叶变换的代码。我正在尝试用 pytorch 1.9.0 中的 torch.fft.fftn() 和 torch.view_as_real() 替换 pytorch 1.5.0 中的 torch.rfft()。我注意到当我运行以下命令时得到的输出略有不同:
使用 PyTorch 1.5.0:
import torch
import numpy as np
arr = torch.from_numpy(np.array([[1.,2.,3.,4.,5.],
[6.,7.,8.,9.,10.],
[11.,12.,13.,14.,15.],
[16.,17.,18.,19.,20.]]))
ftt_arr = torch.rfft(arr,2,onesided=False)
print(fft_arr)
使用 PyTorch 1.9.0:
import torch
import numpy as np
arr = torch.from_numpy(np.array([[1.,2.,3.,4.,5.],
[6.,7.,8.,9.,10.],
[11.,12.,13.,14.,15.],
[16.,17.,18.,19.,20.]]))
fft_arr = torch.fft.fftn(arr,norm="backward")
fft_arr = torch.view_as_real(fft_arr)
print(fft_arr)
两个快速傅里叶变换的输出如下:
pytorch 1.5.0:
tensor([[[211.0000, 0.0000],
[-10.8090, 13.1760],
[ -9.6910, 4.2003],
[ -9.6910, -4.2003],
[-10.8090, -13.1760]],
[[-50.0000, 51.0000],
[ 0.5878, -0.8090],
[ -0.9511, 0.3090],
[ 0.9511, 0.3090],
[ -0.5878, -0.8090]],
[[-51.0000, 0.0000],
[ 0.8090, 0.5878],
[ -0.3090, -0.9511],
[ -0.3090, 0.9511],
[ 0.8090, -0.5878]],
[[-50.0000, -51.0000],
[ -0.5878, 0.8090],
[ 0.9511, -0.3090],
[ -0.9511, -0.3090],
[ 0.5878, 0.8090]]], dtype=torch.float64)
pytorch 1.9.0:
tensor([[[ 2.1000e+02, 0.0000e+00],
[-1.0000e+01, 1.3764e+01],
[-1.0000e+01, 3.2492e+00],
[-1.0000e+01, -3.2492e+00],
[-1.0000e+01, -1.3764e+01]],
[[-5.0000e+01, 5.0000e+01],
[ 2.2204e-15, 0.0000e+00],
[ 1.7764e-15, -4.4409e-16],
[ 1.7764e-15, -4.4409e-16],
[ 2.2204e-15, 0.0000e+00]],
[[-5.0000e+01, 0.0000e+00],
[-1.7764e-15, 0.0000e+00],
[-8.8818e-16, 0.0000e+00],
[-8.8818e-16, 0.0000e+00],
[-1.7764e-15, 0.0000e+00]],
[[-5.0000e+01, -5.0000e+01],
[ 2.2204e-15, 0.0000e+00],
[ 1.7764e-15, 4.4409e-16],
[ 1.7764e-15, 4.4409e-16],
[ 2.2204e-15, 0.0000e+00]]], dtype=torch.float64)
所有输出值似乎都在 +/- 1 左右变化,我无法解释或协调。
【问题讨论】:
-
为什么不在 pytorch 1.9 中使用 torch.fft.rfft? pytorch.org/docs/stable/generated/…
-
原始代码在 3D 矩阵上使用 pytorch 1.5.0 torch.rfft(),所以我会使用 torch.fft.rttfn() 来做 3 维 fft,但原始代码使用 torch 1.5.0 torch.rfft() 带有参数“oneside=False”(这意味着输出是完整的复杂结果,并且不会删除冗余结果)。在 Pytorch 1.9.0 中,torch.fft.rfftn() 没有“单面”参数来实现这一点,所以我改用 torch.fft.fftn()。
标签: python arrays numpy pytorch fft