PyTorch和Numpy之间的计算DFT差异

时间:2019-04-19 21:56:57

标签: numpy math fft pytorch

我正在尝试使用numpy.fft.fft2将numpy的DFT实现转换为所有PyTorch。当对torch.fft进行配对时,结果是不同的。为什么会这样?

脾气暴躁

>>> np.fft.fft2(test_cp, axes=(1,2)).shape
(100, 32, 32, 16)
>>> np.fft.fft2(test_cp, axes=(1,2))[0][0][0]
array([ 121.36078 +0.j,  862.5427  +0.j, -478.19095 +0.j,   -3.937337+0.j,
        124.33441 +0.j,   42.13216 +0.j,  401.703   +0.j,   43.894173+0.j,
       -155.7095  +0.j,  513.4581  +0.j, -119.220665+0.j, -166.82913 +0.j,
       -954.76184 +0.j, -401.38803 +0.j, -189.80426 +0.j, -231.5777  +0.j],
      dtype=complex64)
>>> abs(np.fft.fft2(test_cp, axes=(1,2))[0][0][0])  # to compare directly to real numbers in torch
array([121.36078 , 862.5427  , 478.19095 ,   3.937337, 124.33441 ,
        42.13216 , 401.703   ,  43.894173, 155.7095  , 513.4581  ,
       119.220665, 166.82913 , 954.76184 , 401.38803 , 189.80426 ,
       231.5777  ], dtype=float32)

Pytorch 从(*,2)的输出中查看实数

>>> torch.fft(layer_acts, signal_ndim=2).shape
torch.Size([100, 32, 32, 16, 2])
>>> torch.fft(layer_acts, signal_ndim=2)[:, :,:,:,0][0][0][0]
tensor([   6.9696,  -41.5982, -144.1784,  -54.1054,   -2.1185,  -78.1295,
          72.2305,  107.3909,  172.7782,  107.3909,   72.2305,  -78.1295,
          -2.1185,  -54.1054, -144.1784,  -41.5982], grad_fn=<SelectBackward>)

与火炬相比,是什么使numpy的计算方式有所不同?

谢谢!

0 个答案:

没有答案