gpt4 book ai didi

python - skcuda.fft 与 numpy.fft.rfft 不一样?

转载 作者:行者123 更新时间:2023-12-01 00:47:45 33 4
gpt4 key购买 nike

我试图针对 numpy fft 测试 fft 的输出以进行单元测试,我很快就意识到它失败了,这并不是因为我做错了什么,而是 skcuda 实际上并没有产生相同的答案。我知道它们会略有不同,但至少其中一个数字比 numpy 生成的数字低几个数量级,并且 allclosealmost_equal 都返回大量错误(rtol=1e-6 为 33% 和 25%,atol=1e-6 为 16%)。我在这里做错了什么?我可以解决这个问题吗?

测试文件:

import pycuda.autoinit
from skcuda import fft
from pycuda import gpuarray
import numpy as np

def test_skcuda():
array_0 = np.array([[1, 2, 3, 4, 5, 4, 3, 2, 1, 0]], dtype=np.float32)
array_1 = array_0 * 10
time_domain_signal = np.array([array_0[0], array_1[0]], dtype=np.float32)
fft_point_count = 10
fft_plan = fft.Plan(fft_point_count, np.float32, np.complex64,
batch=2)
fft_reserved = gpuarray.empty((2, fft_point_count // 2 + 1), dtype=np.complex64)
fft.fft(gpuarray.to_gpu(time_domain_signal), fft_reserved, fft_plan)

np.testing.assert_array_almost_equal(
np.fft.rfft(time_domain_signal, fft_point_count), fft_reserved.get())

test_skcuda()

断言失败:

AssertionError: 
Arrays are not almost equal to 6 decimals

(mismatch 25.0%)
x: array([[ 2.500000e+01+0.000000e+00j, -8.472136e+00-6.155367e+00j,
-1.193490e-15+2.331468e-15j, 4.721360e-01-1.453085e+00j,
2.664535e-15+0.000000e+00j, 1.000000e+00+0.000000e+00j],...
y: array([[ 2.500000e+01+0.000000e+00j, -8.472136e+00-6.155367e+00j,
8.940697e-08+5.960464e-08j, 4.721359e-01-1.453085e+00j,
0.000000e+00+0.000000e+00j, 1.000000e+00+0.000000e+00j],...

打印输出:

#numpy
[[ 2.50000000e+01+0.00000000e+00j -8.47213595e+00-6.15536707e+00j
-1.19348975e-15+2.33146835e-15j 4.72135955e-01-1.45308506e+00j
2.66453526e-15+0.00000000e+00j 1.00000000e+00+0.00000000e+00j]
[ 2.50000000e+02+0.00000000e+00j -8.47213595e+01-6.15536707e+01j
-1.11022302e-14+2.39808173e-14j 4.72135955e+00-1.45308506e+01j
3.55271368e-14+7.10542736e-15j 1.00000000e+01+0.00000000e+00j]]

#skcuda
[[ 2.5000000e+01+0.0000000e+00j -8.4721355e+00-6.1553669e+00j
8.9406967e-08+5.9604645e-08j 4.7213593e-01-1.4530852e+00j
0.0000000e+00+0.0000000e+00j 1.0000000e+00+0.0000000e+00j]
[ 2.5000000e+02+0.0000000e+00j -8.4721359e+01-6.1553673e+01j
1.4305115e-06-4.7683716e-07j 4.7213597e+00-1.4530851e+01j
0.0000000e+00+1.9073486e-06j 1.0000000e+01+0.0000000e+00j]]

最佳答案

FFT 的输出存在与输入值的幅度相关的误差。每个输出元素都是通过组合所有输入元素来计算的,因此它们的大小决定了结果的精度。

您正在同一数组中计算两个一维 FFT。它们各自具有不同的幅度输入,因此应具有不同的幅度容差。

以下快速代码演示了如何实现这一点。我不知道如何调整 numpy.testing 中的任何函数来做到这一点。

import numpy as np

array_0 = np.array([[1, 2, 3, 4, 5, 4, 3, 2, 1, 0]], dtype=np.float32)
array_1 = array_0 * 10
time_domain_signal = np.array([array_0[0], array_1[0]], dtype=np.float32)

# numpy result
a=np.array([[ 2.50000000e+01+0.00000000e+00j, -8.47213595e+00-6.15536707e+00j,
-1.19348975e-15+2.33146835e-15j, 4.72135955e-01-1.45308506e+00j,
2.66453526e-15+0.00000000e+00j, 1.00000000e+00+0.00000000e+00j],
[ 2.50000000e+02+0.00000000e+00j, -8.47213595e+01-6.15536707e+01j,
-1.11022302e-14+2.39808173e-14j, 4.72135955e+00-1.45308506e+01j,
3.55271368e-14+7.10542736e-15j, 1.00000000e+01+0.00000000e+00j]])

# skcuda result
b=np.array([[ 2.5000000e+01+0.0000000e+00j, -8.4721355e+00-6.1553669e+00j,
8.9406967e-08+5.9604645e-08j, 4.7213593e-01-1.4530852e+00j,
0.0000000e+00+0.0000000e+00j, 1.0000000e+00+0.0000000e+00j],
[ 2.5000000e+02+0.0000000e+00j, -8.4721359e+01-6.1553673e+01j,
1.4305115e-06-4.7683716e-07j, 4.7213597e+00-1.4530851e+01j,
0.0000000e+00+1.9073486e-06j, 1.0000000e+01+0.0000000e+00j]])

# Tolerance for result array row relative to the mean absolute input values
# 1e-6 because we're using single-precision floats
tol = np.mean(np.abs(time_domain_signal), axis=1) * 1e-6

# Compute absolute difference and compare that to our tolearances
diff = np.abs(a-b)
if np.any(diff > tol[:,None]):
print('ERROR!!!')

关于python - skcuda.fft 与 numpy.fft.rfft 不一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56811521/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com