Skip to content

Numpy mean fails/gives huge precision issues with large arrays and axis selection #11331

Closed

Description

@shachar-i

On Numpy 1.14.2 I get the following:

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

results in:
[127.50656009 127.49165182 127.51390158]
[64. 64. 64.]
127.50413

Even considering float32 precision, this type of failure seems odd, especially given that the entire array's mean can be calculated succesfully

Activity

  1. seberg commented on Jun 14, 2018

    @seberg
    Member

    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

  2. shachar-i commented on Jun 17, 2018

    @shachar-i
    Author

    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)

  3. seberg commented on Jun 17, 2018

    @seberg
    Member

    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....

  4. omasoud commented on Dec 8, 2018

    @omasoud

    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:

    [127.48983901 127.50801956 127.49455946]
    [127.48983901 127.50801956 127.49455946]
    127.4976

    Another minimalist example that illustrates this issue:

    a=np.random.rand(30*1000*1000,2).astype(np.float32)*.01+3.9 # 30 million pairs
    print('Expected:')
    print(' mean: ', np.mean(a,axis=0,dtype=np.float64), '  (analytical = 3.905)')
    print('  std: ', np.std(a,axis=0,dtype=np.float64), '  (analytical = .01/sqrt(12) = 0.00288...)')
    print('Instead, you get:')
    print(' mean: ', np.mean(a,axis=0))
    print('  std: ', np.std(a,axis=0))

    Outputs:

    Expected:
     mean:  [3.9050007  3.90499964]   (analytical = 3.905)
      std:  [0.00288722 0.00288692]   (analytical = .01/sqrt(12) = 0.00288...)
    Instead, you get:
     mean:  [2.236962 2.236962]
      std:  [1.4956477 1.4956477]
    

    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.

  5. charris commented on Dec 8, 2018

    @charris
    Member

    I suppose the warning could be made length dependent.

  6. added a commit that references this issue on Jun 7, 2019
    08ebd60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions