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

Problem: precision of faiss output #1086

Closed
2012fang opened this issue Jan 16, 2020 · 3 comments
Closed

Problem: precision of faiss output #1086

2012fang opened this issue Jan 16, 2020 · 3 comments

Comments

@2012fang
Copy link

2012fang commented Jan 16, 2020

vector1 = array([[-8.44551474e-02, 4.56927270e-02, -1.23174498e-02,
-3.28429081e-02, -7.21267983e-02, 1.78922843e-02,
5.72474487e-02, 1.16973273e-01, -2.67414916e-02,
-2.48716399e-02, 7.50321448e-02, -8.46290886e-02,
7.03940690e-02, 1.73218511e-02, -9.68408026e-03,
5.39224297e-02, -2.21001990e-02, -6.22086227e-02,
-5.71195409e-03, -4.89818901e-02, -2.62292381e-02,
-7.56375724e-04, -3.10133863e-03, 2.50932369e-02,
-2.45238114e-02, 2.69615464e-03, 1.92056168e-02,
-3.85982543e-02, 3.91353038e-04, 2.78586075e-02,
3.11407298e-02, 3.98177765e-02, 1.26895290e-02,
4.59183939e-02, -1.79051757e-02, -5.52790565e-03,
6.54223328e-03, 8.18098924e-05, -3.61517854e-02,
3.96922370e-03, 1.84885133e-02, 7.61720911e-02,
-1.71365440e-02, 2.89176721e-02, 1.41874431e-02,
2.79633328e-02, -9.05210152e-03, -5.40017057e-03,
1.52942166e-02, -4.57295403e-02, -1.27692312e-01,
2.91015562e-02, 3.44425137e-03, -1.32864729e-01,
1.61509942e-02, -4.71395580e-03, -3.31216329e-03,
-5.12617938e-02, -1.18143307e-02, 4.09921259e-03,
-1.69061478e-02, -6.64441613e-03, -1.79887109e-04,
3.39703285e-03, 2.31174359e-04, -6.80163456e-03,
-4.33840603e-02, 1.65534336e-02, -1.14529179e-02,
-5.13268374e-02, 8.57468881e-03, -4.03255783e-02,
1.27376884e-01, 2.95607466e-02, -7.07077840e-03,
6.40317500e-02, -4.19284543e-03, -1.87955312e-02,
3.14377132e-03, 3.27055436e-03, -2.84685977e-02,
3.05155981e-02, -1.38661405e-02, 2.08585709e-02,
-8.18277225e-02, 7.66183436e-03, -8.52554291e-03,
-4.12235968e-03, -2.16198359e-02, 8.11005197e-03,
5.65368915e-03, 7.68653117e-03, 1.53549192e-02,
-2.66802274e-02, 6.90017221e-03, -1.03461863e-02,
-3.84186208e-02, 7.67239253e-04, -7.69756362e-02,
-2.08837376e-03, 1.70545429e-01, -3.28036658e-02,
-1.23308171e-02, 2.23817527e-02, 2.46881787e-02,
-9.55710560e-03, -1.12988511e-02, 4.77782590e-03,
2.15060432e-02, -8.62445123e-03, -4.57427930e-03,
-9.77519713e-03, -8.32691789e-03, -1.17511367e-02,
8.34971201e-03, -8.32115579e-03, -1.06792047e-03,
4.26274911e-03, 2.91057304e-03, -2.71207988e-02,
1.73099246e-02, 5.73569834e-02, -1.06890816e-02,
5.14503904e-02, 5.71261998e-03, -6.38413336e-03,
2.71979533e-03, -2.98482575e-03, -2.09113173e-02,
-3.34538966e-02, 1.03973553e-01, 1.61792163e-03,
-2.10588938e-03, 1.16627095e-02, -4.08083200e-02,
-2.15507522e-02, -3.02650742e-02, -2.24047992e-02,
-5.13026938e-02, 3.36436518e-02, -1.44781824e-02,
1.46059990e-02, 1.78493012e-03, 1.40046664e-02,
1.03832912e-02, 5.89301228e-04, -8.61266702e-02,
2.70281229e-02, 1.32350363e-02, 2.37457342e-02,
6.16388721e-03, 1.34771280e-02, -3.71912145e-03,
5.50323725e-03, 1.83100328e-02, -1.15995295e-02,
4.94935736e-03, -3.10285240e-02, 2.88795643e-02,
2.23691966e-02, -5.46695804e-03, -1.25807934e-02,
1.06846122e-03, -9.27604176e-03, 2.31033235e-04,
-1.27426907e-02, -1.49435410e-02, -3.63811618e-04,
4.62528929e-04, 1.41673416e-01, -2.24193204e-02,
2.65473872e-03, 1.73668750e-02, -8.37226305e-03,
-7.68812199e-04, -5.59632443e-02, 1.76156759e-02,
-1.90522298e-02, 1.30176125e-02, -2.61259405e-03,
2.12291051e-02, -5.47764264e-03, 1.14389755e-01,
-1.40980510e-02, -4.03918093e-03, 1.17943855e-02,
2.88138464e-02, -8.01990330e-02, 5.73012047e-03,
1.15558198e-02, 8.47641262e-04, -1.48011073e-02,
-5.33317542e-03, 5.24302188e-04, 2.74261590e-02,
1.25564018e-03, 3.57892131e-04, 7.67934648e-03,
3.72179062e-03, -3.13465036e-02, -2.22872272e-02,
1.36735262e-02, -5.66293187e-02, -4.33784463e-02,
-1.10101821e-02, 1.83216650e-02, -1.62522821e-03,
5.90732263e-04, 1.58377308e-02, -3.08983680e-03,
-1.30449077e-02, 5.49028115e-03, 1.46150414e-03,
-3.28909699e-03, -2.34518554e-02, -2.49394365e-02,
3.35218087e-02, -3.72543884e-03, 8.32281914e-03,
1.74827911e-02, 1.36323106e-02, 1.96427316e-03,
-1.54801579e-02, 2.18579508e-02, 1.53032644e-02,
-1.24354541e-01, 4.61954763e-03, 6.42319210e-03,
-1.99424778e-03, 2.53712293e-02, -2.28092237e-03,
8.56701657e-03, -8.70297104e-03, 4.91529889e-03,
2.96094920e-02, -1.40514761e-01, -1.64863258e-03,
1.46121383e-02, -1.70896668e-02, -1.08292485e-02,
-1.93554610e-02, -9.06635891e-04, -5.14678983e-03,
8.32778960e-03, -3.03074601e-03, 5.09683415e-03,
2.29578353e-02, -2.38610152e-03, -3.95761512e-04,
-1.23001286e-03, -6.09071506e-03, -6.79963641e-03,
-1.66030917e-02, 5.24643157e-03, -2.21087039e-02,
-1.45360304e-03, 7.90787116e-03, -1.70765687e-02,
3.24698351e-03, 3.14700068e-03, 6.82703545e-03,
6.86773192e-03, -3.06519661e-02, -1.18442383e-02,
-9.98975523e-03, -1.33655027e-01, -3.16358381e-03,
1.30502349e-02, -3.98504548e-03, 8.38416163e-03,
-1.52457866e-03, -1.47919785e-02, -1.19871600e-02,
2.25242693e-02, -7.72700063e-04, 1.61835514e-02,
-3.06999348e-02, 4.59993165e-03, -1.12461997e-02,
-6.39913836e-03, -9.07016173e-03, 1.04761925e-02,
2.33298331e-03, 1.48088823e-03, 3.24343471e-03,
-7.87130930e-03, -1.27785711e-03, 2.93481238e-02,
1.79333263e-03, -4.18773573e-03, -2.99077109e-03,
-4.42271261e-03, -9.84390359e-03, -1.00352280e-02,
-4.04074416e-03, -9.34695639e-03, 1.35453782e-04,
1.59593262e-02, 6.48296159e-03, -1.59481931e-02,
1.58800000e+03]], dtype=float32)

array([-3.52100320e-02, 3.23681682e-02, 4.95685935e-02, 2.25345828e-02,
-2.19355747e-02, 3.47894281e-02, 1.88222472e-02, -2.67079379e-02,
2.93516293e-02, 2.51668319e-02, -2.38688551e-02, 4.35155258e-02,
1.52722495e-02, 1.03030205e-02, 6.67062914e-03, -1.76866446e-02,
-1.97095834e-02, -1.20737469e-02, 1.72977033e-03, 1.84225533e-02,
8.42243433e-03, -9.13981907e-03, -4.81031649e-02, 4.02168371e-03,
1.59920063e-02, -3.62270512e-02, -3.90865281e-02, 1.65622961e-02,
6.56311307e-03, -4.22318000e-03, 5.76240476e-03, -9.56683885e-03,
-1.00857817e-01, -2.68988730e-03, -1.93930697e-02, 7.16141844e-03,
2.22620480e-02, 3.36622037e-02, -1.15636700e-04, -1.84167828e-02,
-4.98541743e-02, -1.08579248e-02, 5.92702953e-03, 1.49204656e-02,
-2.58545415e-03, 4.85718600e-04, 1.40515016e-02, 1.60455033e-02,
-5.20697981e-03, -6.94154855e-03, -5.77796390e-03, 6.35442790e-03,
-3.31752072e-03, -9.27312002e-02, 4.30252589e-03, -5.70000196e-03,
-1.40949450e-02, -9.79238469e-03, -4.61103162e-03, 5.95919183e-03,
-1.21632749e-02, -2.81034783e-03, -1.30199082e-02, -1.80452187e-02,
8.07777699e-03, 3.62024433e-03, -9.82603058e-03, 2.10663397e-02,
-2.22302061e-02, 4.63656615e-03, -6.54944265e-03, -6.16694689e-02,
5.04274257e-02, -2.02144403e-03, 6.51451899e-03, -6.59902347e-04,
8.13715812e-03, 5.53054269e-04, 1.91264637e-02, 8.31208937e-03,
-5.69219403e-02, -1.88740622e-02, 1.34680541e-02, -1.88715551e-02,
-8.75640661e-02, -9.99674015e-03, -7.21022440e-03, -9.92303900e-03,
8.10161047e-03, 3.18781212e-02, -1.60904031e-03, 1.19031547e-02,
-1.93211790e-02, -5.63503662e-03, 1.11618862e-02, 1.78706311e-02,
-1.40397782e-02, -6.33524777e-03, 2.45600231e-02, -9.09450185e-03,
8.00114498e-02, -1.33760469e-02, 3.82367289e-03, 1.21191954e-02,
2.95079383e-03, 1.11511629e-02, -2.26596161e-03, -2.27466263e-02,
1.96284917e-03, 9.57483612e-03, -5.43116173e-03, -5.90029582e-02,
4.02573869e-03, 1.08914718e-03, -9.73853189e-03, -1.54579254e-02,
2.85442569e-03, -1.38227968e-02, 1.03292111e-02, -1.40649490e-02,
-1.08934157e-02, 7.44717792e-02, -1.53173171e-02, -1.34716146e-02,
6.72431313e-04, -1.31279239e-02, 1.95410103e-02, 8.77668557e-04,
-4.46675252e-03, -3.59900296e-02, -7.96748400e-02, -2.24557682e-03,
6.82948623e-03, 9.16492473e-03, 7.64212804e-04, 2.81372835e-04,
-2.21718214e-02, -7.32463878e-03, -3.13916639e-03, 1.40989078e-02,
8.69531592e-04, 6.18395628e-03, 3.09359375e-03, -1.62273063e-03,
-1.81020529e-04, -8.85605440e-03, -3.52023430e-02, 3.07588354e-02,
8.79613275e-04, 1.79869942e-02, 1.46495244e-02, -4.85088257e-03,
-3.63417645e-03, -1.43184152e-03, 3.72584537e-03, 1.49723524e-02,
1.18230851e-02, -5.99940633e-03, 1.20357645e-03, 4.32597334e-03,
-7.08900671e-03, -2.87136855e-03, -1.31057734e-02, 6.80287369e-03,
7.64887081e-04, -9.03168973e-03, -2.98980926e-03, -1.44707412e-02,
-8.48157724e-05, 4.62485813e-02, -8.42207205e-03, -1.21583730e-01,
-1.69677788e-03, -4.84203594e-03, -8.78408272e-03, -8.18974972e-02,
4.64980351e-03, -2.19167992e-02, -3.08722816e-03, -1.09999226e-02,
1.16145927e-02, -1.69398058e-02, 6.43228590e-02, 9.89711378e-03,
-1.94506701e-02, 1.65792853e-02, 4.67116339e-03, -5.69736473e-02,
-8.37437809e-03, -1.12876680e-03, 4.21729870e-03, 5.83686214e-03,
6.38038735e-04, 1.59960389e-02, 1.09745301e-02, 3.83243605e-04,
-8.73216465e-02, 3.84991872e-03, 2.24868301e-03, 8.21910799e-03,
1.75233446e-02, 3.22782376e-04, -1.21828035e-01, -5.73556870e-03,
-3.35951481e-04, 1.52382662e-03, -8.35970696e-03, 1.27844866e-02,
-4.76959301e-03, -2.53992574e-03, -1.60105270e-03, 2.01589130e-02,
-5.07437531e-03, 1.79226638e-03, -4.53328528e-03, 5.67664276e-04,
-2.51616351e-03, 1.22069418e-02, 8.47196672e-03, 1.02453716e-02,
5.38435159e-03, 2.99520907e-03, 2.78073899e-03, 1.76977459e-02,
1.48236239e-02, -1.45528376e-01, 3.87851335e-03, 1.42852003e-02,
2.61840597e-03, 1.18272370e-02, 4.32929164e-03, 1.07074659e-02,
-7.58202455e-04, 4.96439310e-03, 3.70181799e-02, -1.38960540e-01,
-7.00370781e-03, -1.27403131e-02, -5.37718879e-03, -1.02470759e-02,
-6.06298028e-03, -1.00048454e-02, -5.11866109e-03, 6.08263817e-03,
4.24736086e-03, -4.73625259e-03, 2.11990962e-04, 2.56018760e-03,
9.66703147e-03, -8.41557980e-03, -3.24191572e-03, -8.95409007e-03,
-1.19080013e-02, 9.59675666e-03, -4.40449733e-03, 6.89560198e-04,
-6.83376752e-03, -6.80298870e-03, 7.92070385e-03, -1.02428757e-02,
-3.82725964e-03, 1.28127681e-02, -2.40804814e-03, 4.09662817e-03,
-5.56241907e-03, -1.17346026e-01, -8.00196081e-03, 1.29663874e-03,
1.10061625e-02, -1.17006870e-02, -1.61563121e-02, -3.79574456e-04,
-9.67975520e-03, -2.44614016e-03, 2.91191484e-03, -1.25522225e-03,
5.71437413e-03, 3.36199673e-03, -1.04897069e-02, 6.90355338e-03,
-5.41149639e-03, -1.26767845e-03, 1.24809723e-02, 1.24099327e-03,
3.94557090e-03, -1.66563899e-03, -7.73626426e-03, 8.27431679e-03,
1.45018538e-02, -5.92773024e-04, 4.30521322e-03, -8.55698436e-03,
-9.26231686e-03, 1.06858984e-02, -2.15699635e-02, -9.08072386e-03,
-2.00625211e-02, -6.22118916e-03, -5.33714192e-03, 6.78832270e-03,
1.58700000e+03], dtype=float32)

python code:
vector2 = vector2.reshape((1,301))
vector1 = vector1.reshape((1,301))
index = faiss.IndexFlatL2(301)
index.add(vector2)
diss, inds = index.search(vector1,1)

when I run the faiss code, the result is as follows:
inds,diss = (array([[0]]), array([[1.]], dtype=float32))

When I run other Python code[np.sum(np.square(vector1-vector2))]
the result is :1.3264003

So, what I want to ask is why the output of the faiss code omits the decimal places

@2012fang 2012fang changed the title The accuracy of faiss output Problem: precision of faiss output Jan 16, 2020
@mdouze
Copy link
Contributor

mdouze commented Jan 16, 2020

This is because the distance computation is decomposed as described here: https://github.com/facebookresearch/faiss/wiki/Implementation-notes#matrix-multiplication-to-do-many-l2-distance-computations

So the inaccuracy is triggered by the fact that:

  • there is one component of the vector that is much larger than the others (last component is 1580, the others are around 1e-2)

  • Faiss uses the "matrix computation" trick because the dimension is not a multiple of 4 (this is an obsolete check that I will remove)

The fix to improve the numerical stability is to subtract the mean of the vectors. For example, subtracting 1500 from the last component fixes the discrepancy, see #297

@mdouze
Copy link
Contributor

mdouze commented Jan 20, 2020

The enhancement is to remove the obsolete check. Done now in FB internal version.

@mdouze
Copy link
Contributor

mdouze commented Feb 20, 2020

Will be shipped in 1.6.2.

@mdouze mdouze closed this as completed Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants