Repository navigation
Closed
Labels
bugSomething isn't workingSomething isn't workingpythonPull requests for the Python Semantic KernelPull requests for the Python Semantic Kernel
Description
| similarity_scores = ( | |
| embedding.dot(embedding_array.T) | |
| / (linalg.norm(embedding) * linalg.norm(embedding_array, axis=1)) | |
| )[0] |
Possible divide by zero if linalg.norm returns a 0.
Possible fix:
# Calculate the L2 norm (Euclidean norm) of the query embedding
query_norm = linalg.norm(embedding)
# Calculate the L2 norms of each embedding in the collection
collection_norms = linalg.norm(embedding_array, axis=1)
# Identify valid indices where both the query norm and collection norms are non-zero
# This step helps to avoid division by zero issues when calculating cosine similarity
valid_indices = (query_norm != 0) & (collection_norms != 0)
# Initialize an array to store similarity scores, setting them to 0.0 by default
similarity_scores = array([0.0] * len(embedding_collection))
# If there are any valid indices (i.e., both query and collection norms are non-zero),
# calculate the cosine similarity between the query embedding and the collection embeddings
if valid_indices.any():
similarity_scores[valid_indices] = (
# Calculate the dot product between the query embedding and valid collection embeddings
embedding.dot(embedding_array[valid_indices].T)
# Normalize the dot product by multiplying the query norm and valid collection norms
/ (query_norm * collection_norms[valid_indices])
)[0]
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingpythonPull requests for the Python Semantic KernelPull requests for the Python Semantic Kernel
Activity
jjhenkel commentedon Mar 27, 2023
Thanks for this Grant Hadlich (@ghadlich)!
We'll have a fix coming soon, I think we may just raise an error if any of the embeddings have a
norm == 0(that would be quite weird, given these should be embeddings of documents produced by an LLM/other embedding techniques --- I can't really imagine a scenario in which a zero vector sneaks in here that shouldn't be an error)evchaki commentedon Apr 4, 2023
This is now merged into the Python branch.
ghadlich commentedon Apr 4, 2023
Thanks folks!