Skip to content

"cosine distance" inverted? #13

Closed
Closed

Description

@mmisiewicz

Running the python example from the docs:

import numpy as np
import usearch

index = usearch.Index(
    ndim=256, # Define the number of dimensions in input vectors
    metric='cos', # Choose the "metric" or "distance", default = 'ip', optional
    dtype='f32', # Quantize to 'f16' or 'i8q100' if needed, default = 'f32', optional
    connectivity=16, # How frequent should the connections in the graph be, optional
    expansion_add=128, # Control the recall of indexing, optional
    expansion_search=64, # Control the quality of search, optional
)

vector = np.random.uniform(0, 0.3, (index.ndim)).astype(np.float32)
index.add(42, vector)
matches, distances, count = index.search(vector, 10)

I get "distances" of array([0.99999994], dtype=float32).

Looks like this is inverted - the distance between vector and itself should be 0 (float eps notwithstanding).

This is a problem with big indexes, where the number of results is much smaller than the number of vectors in the index. I want the lowest distance vectors, which is the opposite . AFACIT there is no way to invert the order used in the search function.

Am I missing something here, or is there an option to invert the order of vectors returned by search?

Activity

  1. ashvardanian commented on May 11, 2023

    @ashvardanian
    Member

    Yes, you are right, the distance for some measures are inverted for efficiency in the low-level implementation. I will adjust that behavior to work in expected manner across all distances. Thank you!

  2. self-assigned this
    on May 11, 2023
  3. added
    invalid
    on May 11, 2023
  4. added a commit that references this issue on May 15, 2023
    3e8f18f
  5. added a commit that references this issue on May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

invalid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions