RuntimeWarning: invalid value encountered in sqrt

倾然丶 夕夏残阳落幕 提交于 2021-01-25 07:32:05

问题


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

来源:https://stackoverflow.com/questions/63234390/runtimewarning-invalid-value-encountered-in-sqrt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!