Chromadb查询相同文档为何返回非0余弦距离?
为什么ChromaDB查询相同语句返回的余弦距离不是0而是-2.220446049250313e-16?
问题描述
我用ChromaDB创建数据集并添加了10条新闻标题,查询第一条标题时,预期余弦距离为0.0,但实际返回了-2.220446049250313e-16,想了解这一现象的原因。
复现代码
import chromadb from chromadb.utils import embedding_functions embedding_function = embedding_functions.DefaultEmbeddingFunction() client = chromadb.PersistentClient(path="./db") sentences = [ "COVID-19 Vaccination Rates Reach 70% Milestone in the United States", "Stock Market Surges to Record Highs as Economy Recovers", "NASA's Perseverance Rover Discovers Ancient Signs of Life on Mars", "World Leaders Gather for Climate Summit to Address Global Warming", "New Study Shows Link Between Exercise and Mental Health Improvement", "Record-Breaking Heatwave Hits Europe, Sparks Concerns About Climate Change", "Major Cybersecurity Breach Exposes Sensitive Data of Millions of Users", "Scientists Make Breakthrough in Fusion Energy, Promising Clean Power", "UN Report Highlights Alarming Decline in Biodiversity Worldwide", "SpaceX Successfully Launches Crewed Mission to the International Space Station" ] ids = list(map(str, list(range(1, len(sentences) + 1)))) metadatas = [{'type': 'news', 'source': 'nytimes'} for i in range(len(sentences))] collection = client.get_or_create_collection(name="headlines", embedding_function=embedding_function, metadata={"hnsw:space": "cosine"}) collection.add(ids=ids, documents=sentences, metadatas=metadatas) q_client = chromadb.PersistentClient(path="./db") query = sentences[0] q_collection = q_client.get_collection("headlines") results = q_collection.query(query_texts=[query], include=["documents", "distances", "metadatas"], n_results=1) print(results)
输出结果
{'ids': [['1']], 'distances': [[-2.220446049250313e-16]], 'metadatas': [[{'source': 'nytimes', 'type': 'news'}]], 'embeddings': None, 'documents': [['COVID-19 Vaccination Rates Reach 70% Milestone in the United States']]}
原因解释
这个现象是浮点数精度误差导致的,属于数值计算中的常见情况:
- 你设置的是余弦距离空间,两个完全相同的向量理论上余弦相似度为1,对应的余弦距离应为0。但浮点数在存储和计算过程中无法精确表示所有实数,会产生极小的精度偏差。
- 你看到的
-2.220446049250313e-16是机器epsilon量级的数值,它是浮点数系统中能表示的最小非零值之一,本质是计算时的精度损失,并非逻辑错误。 - 这个值和0的差距小到可以完全忽略,而且查询返回的文档确实是目标语句,说明匹配逻辑是准确的。如果业务场景需要严格的0值,你可以对结果做阈值判断,比如将小于
1e-10的距离统一视为0。
内容的提问来源于stack exchange,提问作者CSP Nanda
相关产品推荐
相关产品推荐

