Suppress divide by zero wranings for cosine similarity (#9006)

Suppress run time warnings for divide by zero as the downstream code
handles the scenario (handling inf and nan)
This commit is contained in:
Eugene Yurtsev 2023-08-09 18:56:51 -04:00 committed by GitHub
parent 5454591b0a
commit efa02ed768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,7 +20,9 @@ def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
X_norm = np.linalg.norm(X, axis=1)
Y_norm = np.linalg.norm(Y, axis=1)
similarity = np.dot(X, Y.T) / np.outer(X_norm, Y_norm)
# Ignore divide by zero errors run time warnings as those are handled below.
with np.errstate(divide="ignore", invalid="ignore"):
similarity = np.dot(X, Y.T) / np.outer(X_norm, Y_norm)
similarity[np.isnan(similarity) | np.isinf(similarity)] = 0.0
return similarity