简体   繁体   English

Numpy浮点舍入错误

[英]Numpy floating point rounding errors

While searching for some numpy stuff, I came across a question discussing the rounding accuracy of numpy.dot(): 在搜索一些numpy的东西时,我遇到了一个讨论numpy.dot()的舍入精度的问题:

Numpy: Difference between dot(a,b) and (a*b).sum() Numpy:点(a,b)和(a * b)之间的差异.sum()

Since I happen to have two (different) Computers with Haswell-CPUs sitting on my desk, that should provide FMAand everything, I thought I'd test the example given by Ophion in the first answer, and I got a result that somewhat surprised me: 因为我碰巧有两台(不同的)计算机和Haswell-CPU坐在我的桌子上,那应该提供FMA和一切,我想我会在第一个答案中测试Ophion给出的例子,我得到的结果让我感到有些惊讶:

After updating/installing/fixing lapack/blas/atlas/numpy, I get the following on both machines: 更新/安装/修复lapack / blas / atlas / numpy后,我在两台机器上都得到以下内容:

>>> a = np.ones(1000, dtype=np.float128)+1e-14
>>> (a*a).sum()
1000.0000000000199999
>>> np.dot(a,a)
1000.0000000000199948

>>> a = np.ones(1000, dtype=np.float64)+1e-14
>>> (a*a).sum()
1000.0000000000198
>>> np.dot(a,a)
1000.0000000000176

So the standard multiplication + sum() is more precise than np.dot(). 所以标准乘法+ sum()比np.dot()更精确。 timeit however confirmed that the .dot() version is faster (but not much) for both float64 and float128. 但是timeit确认.dot()版本对于float64和float128来说都更快(但并不多)。

Can anyone provide an explanation for this? 任何人都可以为此提供解释吗?

edit: I accidentally deleted the info on numpy versions: same results for 1.9.0 and 1.9.3 with python 3.4.0 and 3.4.1. 编辑:我不小心删除了numpy版本的信息:1.9.0和1.9.3与python 3.4.0和3.4.1相同的结果。

It looks like they recently added a special Pairwise Summation to ndarray.sum for improved numerical stability. 看起来他们最近为ndarray.sum添加了一个特殊的成对求和 ,以提高数值稳定性。

From PR 3685 , this affects: PR 3685 ,这会影响:

all add.reduce calls that go over float_add with IS_BINARY_REDUCE true
so this also improves mean/std/var and anything else that uses sum.

See here for code changes. 请参阅此处了解代码更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM