gpt4 book ai didi

python - 使用 Numba 的 @jit 导致数学与 Python 中使用的 Numpy 的 float32 不一致

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:36 24 4
gpt4 key购买 nike

当使用 Numba 的 @jit 和 Numpy 的 float32 数据类型时,我得到了?截断?问题。这主要是噪音,因为它远远超出了我关心的小数位数 - 大约第 7 或第 8 位 - 但知道发生了什么以及我是否可以修复它仍然是件好事。

顺便说一句,我必须使用 float32 数据类型来节省内存!

这是我用作测试的代码:

import numpy as np
from test_numba import test_numba

np.random.seed(seed=1774);
number = 150;
inArray = np.round(np.float32((np.random.rand(number)-.5)*2),4); #set up a float32 with 4 decimal places
numbaGet = test_numba(inArray); #run it through
print("Get:\t"+str(numbaGet)+" Type: "+str(type(numbaGet)));
print("Want:\t"+str(np.mean(inArray))+" Type: "+str(type(np.mean(inArray)))); #compare to expected

结合以下功能

import numpy as np
from numba import jit #, float32

@jit(nopython=True) #nopython=True, nogil=True, parallel=True, cache=True , nogil=True, parallel=True #float32(float32),
def test_numba(inArray):

#outArray = np.float32(np.mean(inArray)); #forcing float32 did not change it
outArray = np.mean(inArray);

return outArray;

输出结果是:

Get:    0.0982406809926033 Type: <class 'float'>
Want: 0.09824067 Type: <class 'numpy.float32'>

这似乎表明 Numba 正在使它成为 Python float 类(据我所知是 float64)并进行数学运算,然后不知何故失去了精度。

如果我切换到 float64,差异会大大减小。

Get:    0.09824066666666667 Type: <class 'float'>
Want: 0.09824066666666668 Type: <class 'numpy.float64'>

不确定我做错了什么。同样,在我的情况下,这是一个可以忽略的问题(从小数点后 4 位开始),但仍然想知道为什么!

最佳答案

原因是,numba 不使用 np.mean 而是用/推出 its own version 代替它:

def array_mean_impl(arr):
# Can't use the naive `arr.sum() / arr.size`, as it would return
# a wrong result on integer sum overflow.
c = zero
for v in np.nditer(arr):
c += v.item()
return c / arr.size

前段时间我给了an answer to a very similar question关于 numpy.meanpandas.mean 之间的区别(使用 bottleneck)。所以那里所说的一切也适用于这里,请查看它以获取更多详细信息,简而言之:

  • numba 使用的朴素求和误差为 O(n),其中 n 是求和数。
  • Numpy 使用类似于 pairwise-summation 的方法,错误 O(log(n)) 更精确。
  • float32 的区别很明显,float64 的区别不太明显,尽管同样的问题仍然存在。

关于python - 使用 Numba 的 @jit 导致数学与 Python 中使用的 Numpy 的 float32 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262281/

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