Git Product home page Git Product logo

Comments (8)

bddap avatar bddap commented on June 16, 2024

if (std::is_floating_point<T>::value)

from diskann.

daxpryce avatar daxpryce commented on June 16, 2024

@harsha-simhadri : this is actually the behavior of the diskann C++ lib, and it does seem like a bug. Am I calling it wrong or something?

from diskann.

daxpryce avatar daxpryce commented on June 16, 2024

I don't seem to be calling anything incorrectly; with disk indices specifically, we just switch the cosine metric over to the L2 metric, spit something out to console about it, and move on.

At least with memory indices, we seem to be normalizing the vectors first, though I only validated up through to us setting the normalize_vecs flag when cosine is chosen (and we use L2 from that point on)

Edit: correctly -> incorrectly

from diskann.

daxpryce avatar daxpryce commented on June 16, 2024

#445 documents what currently works, and what does not. It also has python checks on metric : vector dtype prior to even reaching into the extension modules.

Leaving this open as a bug though because it's still not properly fixed.

from diskann.

rakri avatar rakri commented on June 16, 2024

Will investigate and get back soon.

from diskann.

IbraheemTaha avatar IbraheemTaha commented on June 16, 2024

I wonder if there is any update on this issue and if it has been solved yet?

Moreover, if the vectors are normalized in the same space, cosine and l2 become similar in terms of ranking since after normalization (every vector's length is 1) both consider measuring the angle between vectors. Can you confirm that the normalization can lead to same results using DiskANN?

from diskann.

IbraheemTaha avatar IbraheemTaha commented on June 16, 2024

Following my previous comment, normalizing the vectors would solve the problem as you can see in the following:

import glob
import os
from pathlib import Path

import diskannpy as dap
import numpy as np


def directory_is_empty(directory: str) -> bool:
    dir = Path(directory)
    fpath = dir.resolve()
    empty = not any(dir.iterdir())

    if not empty:
        print("Found {} . Removing contet".format(fpath))
        files = glob.glob('{}/*.*'.format(fpath))
        for f in files:
            os.remove(f)

    return empty


rel_dir = "bug_fix"
Path(rel_dir).mkdir(exist_ok=True)
directory_is_empty(rel_dir)

query = [1, -0.1]
search_space = [
    [10, 0],  # according to cosine distance, this is the closest
    [1, 0.1],  # according to l2, this is the closest
]


# normalizing all vectors in the search space 
normalized_search_space = []
for idx,col in enumerate(search_space):
    norm = np.linalg.norm(col)
    normalized_search_space.append(col / norm)

# to store back the normalized_search_space in search_space
search_space = normalized_search_space 

dap.build_disk_index(
        data = np.array(search_space, dtype=np.float32),
        distance_metric="l2",
        #distance_metric="cosine",
        index_directory=rel_dir,
        graph_degree=16,
        complexity=32,
        vector_dtype=np.float32,
        search_memory_maximum=0.00003,
        build_memory_maximum=1,
        num_threads=0,
        pq_disk_bytes=0
    )


index = dap.StaticDiskIndex(distance_metric="l2",
	    vector_dtype=np.float32,
            index_directory=Path(rel_dir).resolve(),
            num_threads=16,
            num_nodes_to_cache=10)

res = index.search(np.array(query, dtype=np.float32), 1, 2)
assert res[0].shape == (1,)

print("The results are: ",res)


assert res[0][0] == 0, "cosine distance is not being used"

@bddap could you confirm this?
Thanks in advance.

from diskann.

bddap avatar bddap commented on June 16, 2024

If you normalize the vectors before indexing them, I think l2 will behave similarly to cosine. This can serve as a workaround for some users.

from diskann.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.