Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved from L2 to either L2 or IP #3

Merged
merged 3 commits into from
Mar 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ann_benchmarks/algorithms/milvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, metric, dim, conn_params, index_type, method_params):
self._port = conn_params['port'] # 19530
self._index_type = index_type
self._method_params = method_params
self._metric = metric
self.metric = {'angular': 'IP', 'euclidean': 'L2'}[metric]
DvirDukhan marked this conversation as resolved.
Show resolved Hide resolved
self._query_params = dict()
connections.connect(host=conn_params['host'], port=conn_params['port'])
try:
Expand All @@ -30,14 +30,14 @@ def __init__(self, metric, dim, conn_params, index_type, method_params):
]
schema = CollectionSchema(fields)
self._milvus = Collection('milvus', schema)
self._milvus.create_index('vector', {'index_type': self._index_type, 'metric_type':'L2', 'params':self._method_params})
self._milvus.create_index('vector', {'index_type': self._index_type, 'metric_type':self._metric, 'params':self._method_params})
except:
self._milvus = Collection('milvus')

def fit(self, X, offset=0, limit=None):
limit = limit if limit else len(X)
X = X[offset:limit]
if self._metric == 'angular':
if self._metric == 'IP':
X = sklearn.preprocessing.normalize(X)

self._milvus.insert([[id for id in range(offset, limit)], X.tolist()])
Expand All @@ -57,10 +57,10 @@ def set_query_arguments(self, param):
self._query_params['ef'] = param

def query(self, v, n):
if self._metric == 'angular':
if self._metric == 'IP':
v /= numpy.linalg.norm(v)
v = v.tolist()
results = self._milvus.search([v], 'vector', {'metric_type':'L2', 'params':self._query_params}, limit=n)
results = self._milvus.search([v], 'vector', {'metric_type':self._metric, 'params':self._query_params}, limit=n)
if not results:
return [] # Seems to happen occasionally, not sure why
result_ids = [result.id for result in results[0]]
Expand Down