问题
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