pytorch中使用FFT进行卷积运算

问题描述 投票:0回答:1

我想用

convolution operation
来做
FFT

FFT
应用于两个输入张量并使用
element-wise multiplication
生成输出张量。然后,该输出张量经过
IFFT
产生最终输出。

下面的代码是我写的伪代码。

输入张量:

import torch
a = torch.tensor([0, 1, 2, 3])
b = torch.tensor([1, 2])

伪代码:

fft_a = torch.fft.fft(a)
fft_b = torch.fft.fft(b, n=4)
out = fft_a * fft_b
out = torch.fft.ifft(out)

预期输出:

print(out.real)
>>> tensor([2., 5., 8.])

实际输出:

print(out.real)
>>> tensor([6., 1., 4., 7.])

如何使用

convolution operation
来执行
FFT

python pytorch fft convolution
1个回答
0
投票

通过翻转一个输入张量,它可以正常工作。

下面的代码通过翻转 b 输入张量来应用

FFT

代码:

import torch
a = torch.tensor([0, 1, 2, 3])
b = torch.flip(torch.tensor([1, 2]), [0])

fft_a = torch.fft.fft(a)
fft_b = torch.fft.fft(b, n=4)
out = fft_a * fft_b
out = torch.fft.ifft(out)

print(out.real[1:])

输出:

tensor([2., 5., 8.])
© www.soinside.com 2019 - 2024. All rights reserved.