I am attempting to make some python3 code that works with pytorch 1.5.0 also work correctly on newer versions (I am currently using pytorch 1.9.0). More specifically, I am attempting to update the code which does fast fourier transforms. I am trying to replace torch.rfft() in pytorch 1.5.0 with torch.fft.fftn() and torch.view_as_real() in pytorch 1.9.0. I noticed that I am getting a slightly different output when I run the following:
Using 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)
Using 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)
The outputs for the two Fast Fourier Transforms are the following:
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)
All the output values seem to vary by around +/- 1, which I am unable to explain or reconcile.