assert_array_almost_equal erroring out incorrectly?

Viewed 72

I'm using assert_array_almost_equal here https://numpy.org/doc/stable/reference/generated/numpy.testing.assert_array_almost_equal.html and I'm perplexed over the results I am getting.

Here is the output. In order for the assertion error to be raised, the max absolute difference between any given element in the 2 tensors must be >=1.5 since decimal=0 here. But in this output, you can see it states that the max absolute difference is 1. So how is it possible that the assertion error is being raised?

I also manually verified that the 2 arrays differ by no more than an absolute difference of 1.

AssertionError: 
Arrays are not almost equal to 0 decimals
Mismatched elements: 1 / 288 (0.347%)
Max absolute difference: 1
Max relative difference: 0.5
 x: array([[[[ -62,  -28],
         [ -17,  -23],
         [ -37,  -34],...
 y: array([[[[ -62,  -28],
         [ -17,  -23],
         [ -37,  -34],...

----------------------------------------------------------------------
Ran 1 test in 179.310s

FAILED (failures=1)

Here are two of the arrays that failed this test:

[[[[-104]
   [-103]
   [-108]
   [-110]
   [-106]
   [ -91]
   [ -79]]

  [[ -77]
   [ -70]
   [ -71]
   [ -61]
   [ -59]
   [ -51]
   [ -57]]

  [[-111]
   [-111]
   [-107]
   [-123]
   [-127]
   [-106]
   [ -95]]

  [[ -95]
   [ -91]
   [-106]
   [-112]
   [ -78]
   [ -90]
   [ -99]]

  [[ -70]
   [ -75]
   [ -87]
   [ -85]
   [ -70]
   [ -65]
   [ -92]]

  [[-107]
   [ -91]
   [ -87]
   [ -65]
   [-101]
   [ -53]
   [ -85]]

  [[ -75]
   [ -79]
   [ -71]
   [ -87]
   [ -90]
   [-117]
   [ -81]]

  [[ -86]
   [ -88]
   [-122]
   [ -79]
   [ -83]
   [ -91]
   [ -87]]

  [[ -57]
   [ -96]
   [ -63]
   [ -56]
   [ -69]
   [ -56]
   [ -66]]

  [[ -91]
   [ -83]
   [ -73]
   [ -94]
   [ -96]
   [ -79]
   [ -87]]

  [[ -56]
   [ -86]
   [ -57]
   [ -85]
   [ -48]
   [ -54]
   [ -67]]

  [[ -55]
   [ -60]
   [ -91]
   [ -85]
   [ -89]
   [ -81]
   [ -73]]

  [[ -67]
   [ -69]
   [ -70]
   [ -61]
   [ -79]
   [ -87]
   [ -56]]

  [[ -79]
   [ -47]
   [ -83]
   [ -73]
   [ -61]
   [ -69]
   [ -75]]

  [[ -92]
   [ -78]
   [-110]
   [ -97]
   [-101]
   [ -78]
   [ -72]]

  [[-115]
   [-104]
   [-128]
   [ -93]
   [ -95]
   [-113]
   [ -89]]]]
[[[[-104]
   [-103]
   [-108]
   [-110]
   [-105]
   [ -91]
   [ -78]]

  [[ -77]
   [ -70]
   [ -71]
   [ -61]
   [ -58]
   [ -50]
   [ -57]]

  [[-110]
   [-111]
   [-106]
   [-123]
   [-126]
   [-106]
   [ -95]]

  [[ -94]
   [ -91]
   [-105]
   [-112]
   [ -78]
   [ -89]
   [ -98]]

  [[ -70]
   [ -75]
   [ -87]
   [ -85]
   [ -69]
   [ -64]
   [ -91]]

  [[-107]
   [ -90]
   [ -86]
   [ -64]
   [-100]
   [ -53]
   [ -85]]

  [[ -75]
   [ -78]
   [ -71]
   [ -86]
   [ -89]
   [-117]
   [ -80]]

  [[ -85]
   [ -87]
   [-121]
   [ -79]
   [ -83]
   [ -91]
   [ -87]]

  [[ -57]
   [ -95]
   [ -63]
   [ -56]
   [ -68]
   [ -55]
   [ -66]]

  [[ -90]
   [ -83]
   [ -73]
   [ -94]
   [ -96]
   [ -79]
   [ -87]]

  [[ -56]
   [ -86]
   [ -57]
   [ -84]
   [ -48]
   [ -54]
   [ -66]]

  [[ -55]
   [ -59]
   [ -91]
   [ -84]
   [ -89]
   [ -80]
   [ -73]]

  [[ -67]
   [ -68]
   [ -69]
   [ -61]
   [ -79]
   [ -87]
   [ -56]]

  [[ -78]
   [ -47]
   [ -82]
   [ -72]
   [ -61]
   [ -68]
   [ -75]]

  [[ -91]
   [ -78]
   [-109]
   [ -96]
   [-101]
   [ -78]
   [ -72]]

  [[-115]
   [-104]
   [ 127]
   [ -92]
   [ -95]
   [-113]
   [ -88]]]]

Actually, in this case, I can see there is a -128 & 127 element, but I don't understand why the program is saying the absolute difference here is 1 and not 255.

1 Answers

As pointed out in the comments, your arrays are of type np.int8, hence

-128 - 127 = -255

will overflow to 1.

Hence abs(np.int8(-255)) == 1

Related