Skip to content

Possible Divide By Zero in volatile_memory_store.py #130

Closed

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]

Activity

  1. added
    bugSomething isn't working
    pythonPull requests for the Python Semantic Kernel
    on Mar 21, 2023
  2. jjhenkel commented on Mar 27, 2023

    @jjhenkel

    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)

  3. evchaki commented on Apr 4, 2023

    @evchaki
    Contributor

    This is now merged into the Python branch.

  4. ghadlich commented on Apr 4, 2023

    @ghadlich
    Author

    Thanks folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpythonPull requests for the Python Semantic Kernel

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions