0

I get the error in the title for my code below, but I do not have any negative values or NaNs in my array.

Is there anything else I should check for?

I don't get the error for small arrays that I could copy here. I only get it in my arrays that are approx 100,000 x 1,000

def norm2(A,B):
    """numpy.ndarray A shape (m1,d), B shape (m2,d)
    Returns ndarray of shape (m1, m2) dists_{i,j} = ||A_i - B_j||
    """
    
    print("A.shape: ", A.shape)
    print("B.shape: ", B.shape)
    
    sums = np.sum(np.square(A[:,None,:] - B[None,:,:]),
                  axis = 2
                  )
    
    print("sums.shape: ", sums.shape)
    
    negs = np.sum(sums < 0)
    nans = np.sum(np.isnan(sums))
    
    warn = "There are " + str(negs) + " negative numbers and " + str(nans) + " NaNs."

    dists = np.sqrt(sums) # dists_{i,j} = || A_i - B_j ||

    return dists, warn

The print statements output:

A.shape:  (100000, 1000)
B.shape:  (100000, 1000)
sums.shape:  (100000, 100000)

The warn returned is: 'There are 0 negative numbers and 0 NaNs.'

What else can I check?

Other similar questions were due to negative numbers: Numpy, RuntimeWarning: invalid value encountered in sqrt

I checked the manual, but couldn't find anything about the runtimewarning https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html

I tried to check the source code, but couldn't figure out where the code for sqrt is: https://github.com/numpy/numpy/tree/master/numpy/lib

Joe
  • 662
  • 1
  • 7
  • 20
  • could you try with `np.isnan(sum).any()`? should give you the same result but just to check if there is something else off... – Dorian Aug 03 '20 at 17:57
  • ```np.isnan(sums).any() = False``` – Joe Aug 03 '20 at 18:05
  • 2
    and could you check if the warning persists if you change the `dtype` to `np.complex`? – Dorian Aug 03 '20 at 18:11
  • That made the warning go away!!! I don't understand. I even checked ```minsums = np.min(sums) print("minsums = ",minsums)``` which output 0. – Joe Aug 03 '20 at 18:17
  • 2
    I'm not sure which numpy version you have, but this is just a security warning. the compiler does't know where the numbers come form (not 100% sure how python works here), and since they are computed on the go, it throws you a warning that there might be bad values. – Dorian Aug 03 '20 at 18:22
  • 1
    Well, I didn't realize it until I just checked, but I'm running Python 2.7.5 with NumPy 1.14.3 – Joe Aug 03 '20 at 18:26
  • 2
    yes, that's probably the reason :) – Dorian Aug 03 '20 at 18:32
  • 1
    Yes, I did not get that warning with Python 3.7.5 and NumPy 1.18.1 – Joe Aug 03 '20 at 18:52

0 Answers0