mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-12 08:45:15 +00:00
fix(search): keep zero float16 cosine scores finite
Updated the handling of zero-norm rows to use a representable denominator of 1 instead of 1e-10.
This commit is contained in:
parent
c773fa7b77
commit
3e6a37318a
1 changed files with 2 additions and 2 deletions
|
|
@ -33,7 +33,7 @@ def batch_cosine_similarity(nd_array1: np.ndarray, nd_array2: np.ndarray) -> np.
|
|||
norms1 = np.linalg.norm(nd_array1, axis=1)
|
||||
norms2 = np.linalg.norm(nd_array2, axis=1)
|
||||
norm_products = np.outer(norms1, norms2)
|
||||
# Guard against zero-norm rows to keep division finite.
|
||||
norm_products = np.where(norm_products == 0, 1e-10, norm_products)
|
||||
# Use a representable denominator even in float16; zero rows have zero dot products.
|
||||
norm_products = np.where(norm_products == 0, 1, norm_products)
|
||||
|
||||
return dot_products / norm_products
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue