在新旧版的torch中的傅里叶变换函数在定义和用法上存在不同,记录一下。

1、旧版

fft = torch.rfft(input, 2, normalized=True, onesided=False)
#  input 为输入的图片或者向量,dtype=torch.float32,size比如为[1,3,64,64]
#  signal_ndim(int):The number of dimensions in each signal,can only be 1、2、3
#  normalized(bool,optional):controls wheather to return normallized results. Default:False
#  onesided(bool,optional):controls whether to return half of results to avoid redundancy.Default:True 

上面例子中图像中 singal_ndim = 2 ,是因为输入图像是2维的。

1.7之后的版本中,如果要用 oneside output,则改用torch.fft.rfft();如果要用two-side output,则改用torch.fft.fft()

input= torch.arange(4)
fft = torch.rfft(input, 2, normalized=True, onesided=False)

2、新版

一维离散傅里叶变换

torch.fft.rfft(input,n=None,dim=-1,norm=None) --> Tensor
# input:Tensor
# n(int,optional):Output signal length. This determines the length of the
        output signal. 
# dim(int, optional): The dimension along which to take the one dimensional real IFFT.
# norm (str, optional): Normalization mode.

二维离散傅里叶变换 

torch.fft.rfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
input (Tensor): the input tensor
s (Tuple[int], optional): Signal size in the transformed dimensions.
dim (Tuple[int], optional): Dimensions to be transformed.
norm (str, optional): Normalization mode.

高维离散傅里叶变换 

rfftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
input (Tensor): the input tensor
s (Tuple[int], optional): Signal size in the transformed dimensions.
dim (Tuple[int], optional): Dimensions to be transformed.
norm (str, optional): Normalization mode. For the forward transform

3、新旧版对比

import torch
input = torch.rand(1,3,32,32)

# 旧版pytorch.rfft()函数
fft = torch.rfft(input, 2, normalized=True, onesided=False)

# 新版 pytorch.fft.rfft2()函数
output = torch.fft.fft2(input, dim=(-2, -1))
output = torch.stack((output.real, output_new.imag), -1)

ffted = torch.rfft(input, 1, onesided=False) to ffted = torch.view_as_real(torch.fft.fft(input, dim=1))
and
iffted = torch.irfft(time_step_as_inner, 1, onesided=False) to
iffted = torch.fft.irfft(torch.view_as_complex(time_step_as_inner), n=time_step_as_inner.shape[1], dim=1)

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐