Skip to content

Commit

Permalink
fix the bug of the cosine similarity
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Dec 27, 2022
1 parent a9ba218 commit b54d97b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.7
2.0.8
2 changes: 1 addition & 1 deletion lib/NGT/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ using namespace std;
void
NGT::Command::repair(Args &args)
{
const string usage = "Usage: ng[ [-m c|r|R] repair index \n"
const string usage = "Usage: ngt [-m c|r|R] repair index \n"
"\t-m mode\n"
"\t\tc: Check. (default)\n"
"\t\tr: Repair and save it as [index].repair.\n"
Expand Down
7 changes: 6 additions & 1 deletion lib/NGT/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,12 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds,
}
if (insertionA != insertionB) {
stringstream msg;
msg << "Graph::removeEdgeReliably:Warning. Lost connectivity! Isn't this ANNG? ID=" << id << ".";
msg << "Graph::removeEdgeReliably:Warning. Lost connectivity! Isn't this ANNG? ID=" << id
#if defined(NGT_SHARED_MEMORY_ALLOCATOR)
<< ". (" << node.at(i, repository.allocator).id << ":" << node.at(minj, repository.allocator).id << ")";
#else
<< ". (" << node[i].id << ":" << node[minj].id << ")";
#endif
#ifdef NGT_FORCED_REMOVE
msg << " Anyway continue...";
cerr << msg.str() << endl;
Expand Down
8 changes: 7 additions & 1 deletion lib/NGT/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,13 @@ namespace NGT {
bool addEdge(ObjectID target, ObjectID addID, Distance addDistance, bool identityCheck = true) {
size_t minsize = 0;
GraphNode &node = property.truncationThreshold == 0 ? *getNode(target) : *getNode(target, minsize);
addEdge(node, addID, addDistance, identityCheck);
try {
addEdge(node, addID, addDistance, identityCheck);
} catch(NGT::Exception &err) {
std::stringstream msg;
msg << " Cannot add the edge. " << target << "->" << addID << ". " << err.what();
NGTThrowException(msg);
}
if ((size_t)property.truncationThreshold != 0 && node.size() - minsize >
(size_t)property.truncationThreshold) {
return true;
Expand Down
21 changes: 17 additions & 4 deletions lib/NGT/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,9 @@ CreateIndexThread::run() {
graphIndex.searchForNNGInsertion(obj, *rs);
}
} catch(NGT::Exception &err) {
cerr << "CreateIndex::search:Fatal error! ID=" << job.id << " " << err.what() << endl;
abort();
stringstream msg;
msg << "CreateIndex::search:Fatal error! ID=" << job.id << " " << err.what();
NGTThrowException(msg);
}
job.results = rs;
poolThread.getOutputJobQueue().pushBack(job);
Expand Down Expand Up @@ -730,7 +731,13 @@ insertMultipleSearchResults(GraphIndex &neighborhoodGraph,
cerr << " The number of edges for the node=" << gr.results->size() << endl;
cerr << " The pruned parameter (edgeSizeForSearch [-S])=" << neighborhoodGraph.NeighborhoodGraph::property.edgeSizeForSearch << endl;
}
neighborhoodGraph.insertNode(gr.id, *gr.results);
try {
neighborhoodGraph.insertNode(gr.id, *gr.results);
} catch(NGT::Exception &err) {
std::stringstream msg;
msg << " Cannot insert the node. " << gr.id << ". " << err.what();
NGTThrowException(msg);
}
}
}

Expand Down Expand Up @@ -1403,7 +1410,13 @@ GraphAndTreeIndex::createIndex(const vector<pair<NGT::Object*, size_t> > &object
if (((*job.results).size() == 0) && (job.id != 1)) {
cerr << "insert warning!! No searched nodes!. If the first time, no problem. " << job.id << endl;
}
GraphIndex::insertNode(job.id, *job.results);
try {
GraphIndex::insertNode(job.id, *job.results);
} catch(NGT::Exception &err) {
std::stringstream msg;
msg << " Cannot insert the node. " << job.id << ". " << err.what();
NGTThrowException(msg);
}
}
if (job.results != 0) {
delete job.results;
Expand Down
13 changes: 13 additions & 0 deletions lib/NGT/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ LeafNode::removeObject(size_t id, size_t replaceId) {

size_t fsize = getObjectSize();
size_t idx;
if (replaceId != 0) {
for (idx = 0; idx < fsize; idx++) {
#if defined(NGT_SHARED_MEMORY_ALLOCATOR)
if (getObjectIDs(allocator)[idx].id == replaceId) {
#else
if (getObjectIDs()[idx].id == replaceId) {
#endif
std::cerr << " Warning. found the same ID as the replaced ID." << std::endl;
replaceId = 0;
break;
}
}
}
for (idx = 0; idx < fsize; idx++) {
#if defined(NGT_SHARED_MEMORY_ALLOCATOR)
if (getObjectIDs(allocator)[idx].id == id) {
Expand Down
7 changes: 4 additions & 3 deletions lib/NGT/PrimitiveComparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,13 +837,14 @@ namespace NGT {

template <typename OBJECT_TYPE>
inline static double compareCosineSimilarity(const OBJECT_TYPE *a, const OBJECT_TYPE *b, size_t size) {
return 1.0 - compareCosine(a, b, size);
auto v = 1.0 - compareCosine(a, b, size);
return v < 0.0 ? -v : v;
}

template <typename OBJECT_TYPE>
inline static double compareNormalizedCosineSimilarity(const OBJECT_TYPE *a, const OBJECT_TYPE *b, size_t size) {
double v = 1.0 - compareDotProduct(a, b, size);
return v < 0.0 ? 0.0 : v;
auto v = 1.0 - compareDotProduct(a, b, size);
return v < 0.0 ? -v : v;
}

class L1Uint8 {
Expand Down
3 changes: 2 additions & 1 deletion samples/qg-l2-float/qg-l2-float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
int
main(int argc, char **argv)
{
#ifdef NGTQ_QBG
string indexPath = "index";
string objectFile = "./data/sift-dataset-5k.tsv";
string queryFile = "./data/sift-query-3.tsv";
Expand Down Expand Up @@ -115,7 +116,7 @@ main(int argc, char **argv)
cerr << "Error" << endl;
return 1;
}

#endif
return 0;
}

Expand Down

0 comments on commit b54d97b

Please sign in to comment.