You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Numpy mean fails/gives huge precision issues with large arrays and axis selection #11331
A = np.random.rand(1024,256,256,3)*255 # similar to a 1024 256x256 images tensor
print(np.mean(A,axis=(0,1,2))) # 64 bit works fine
print(np.mean(A.astype(np.float32),axis=(0,1,2))) # 32 bit works fails
print(np.mean(A.astype(np.float32))) # 32 bit works fine if without axis selection
Well, numpy is not overly fancy about how to calculate means, there is a thing that you get better then naive precision in some cases (which kicks in for the full array here), see also for example gh-8116
Ok. So I get why a naive summation with float32 would that, but then it was quite confusing to see it succeed without axis selection.
As I understand from your comment this is due to pairwise summation in some of the cases (?)
Is this strange behavior what one should expect?
Why not implement pairwise summation for all situations?
And (maybe this naive) - is there a way to warn about such drastic round errors without a huge performance hit?
(these type of bugs are usually hard to pin down)
The reason is memory layout and thus speed. Doing the (mostly) pairwise summation in numpy with the typical machinery only works reasonably along a single axis (where it comes with no performance loss at all). But only when summing the fast axis it is feasable to do this, because otherwise others would be complaining about massive performance drops.
I agree that there should be more documentation on this, heck I was even hesitant when we first put this in.... It would also be nice to have more stable summations in general....
I'm glad to see #9393 that will add something in the documentation. But this can be a serious issue that can go unnoticed. A warning would be even more welcome. A fix is of course the idea situation.
My recommended workaround for people running into this issue is to add ,dtype=np.float64 to the np.mean() and np.std() calls. Doing that in the above code yields the following result:
I expect this situation to be encountered a lot and without noticing in neural network data normalization code where statistics over large training data gets computed. People tend to go for single (or half) rather than double precision because of performance and memory savings, and the fact that successful neural network training rarely requires double precision.
Activity
seberg commentedon Jun 14, 2018
Well, numpy is not overly fancy about how to calculate means, there is a thing that you get better then naive precision in some cases (which kicks in for the full array here), see also for example gh-8116
shachar-i commentedon Jun 17, 2018
Ok. So I get why a naive summation with float32 would that, but then it was quite confusing to see it succeed without axis selection.
As I understand from your comment this is due to pairwise summation in some of the cases (?)
Is this strange behavior what one should expect?
Why not implement pairwise summation for all situations?
And (maybe this naive) - is there a way to warn about such drastic round errors without a huge performance hit?
(these type of bugs are usually hard to pin down)
seberg commentedon Jun 17, 2018
The reason is memory layout and thus speed. Doing the (mostly) pairwise summation in numpy with the typical machinery only works reasonably along a single axis (where it comes with no performance loss at all). But only when summing the fast axis it is feasable to do this, because otherwise others would be complaining about massive performance drops.
I agree that there should be more documentation on this, heck I was even hesitant when we first put this in.... It would also be nice to have more stable summations in general....
omasoud commentedon Dec 8, 2018
I'm glad to see #9393 that will add something in the documentation. But this can be a serious issue that can go unnoticed. A warning would be even more welcome. A fix is of course the idea situation.
My recommended workaround for people running into this issue is to add
,dtype=np.float64to thenp.mean()andnp.std()calls. Doing that in the above code yields the following result:[127.48983901 127.50801956 127.49455946][127.48983901 127.50801956 127.49455946]127.4976Another minimalist example that illustrates this issue:
Outputs:
I expect this situation to be encountered a lot and without noticing in neural network data normalization code where statistics over large training data gets computed. People tend to go for single (or half) rather than double precision because of performance and memory savings, and the fact that successful neural network training rarely requires double precision.
charris commentedon Dec 8, 2018
I suppose the warning could be made length dependent.
DOC: Mention and try to explain pairwise summation in sum