面试题库
共 328 题,当前筛选 1 题。
课程全部30 天从前端工程师到 Agent 工程师5 天提示词工程零基础Claude 高效使用:从对话到 Claude CodeCodex 与 OpenAI Agents SDK 高效使用7 天 MCP:把工具接进任何 Agent7 天 Agent Skills:把经验做成可复用能力5 天上下文工程14 天 RAG:从检索到可信回答14 天用 Agent 搭一条 AI 短剧生产线
标签
全部#normalisation1#evaluation7#embeddings5#chunking4#ingestion4#architecture3#cost3#data-quality3#citation-verification2#hybrid-search2#long-context2#recall2
还有 42 个标签收起标签
#refusal2#access-control1#api-design1#bm251#citations1#context-assembly1#debugging1#dimensions1#failure-analysis1#failure-modes1#filtering1#fine-tuning1#grounding1#hallucination1#hnsw1#information-retrieval1#iterative-scan1#ivfflat1#metadata1#model-selection1#modularity1#ocr1#ordering1#overlap1#parent-child1#pdf-parsing1#production-readiness1#prompt-engineering1#quantization1#rag-basics1#ranking1#retrieval1#retrieval-failure1#retrospective1#risk-assessment1#similarity1#streaming1#system-design1#thresholds1#trade-offs1#vector-database1#vector-index1
14 天 RAG:从检索到可信回答
D2 embedding 与向量检索:相似度、维度与模型选型,把文本存进 pgvector
余弦相似度和内积什么时候等价?如果向量没有归一化,用内积排序会出什么问题?When are cosine similarity and inner product equivalent? What goes wrong if you rank by inner product on vectors that are not normalised?
国内高频海外高频基础#embeddings#similarity#normalisation分析过程 · 先想清楚再作答
- 这题是送分题,但区分度藏在后半句。只答「归一化之后两者等价」的人很多,面试官真正想听的是「没归一化会怎么坏」,因为那是线上真的会发生的事。
- 先把定义摆出来:余弦相似度等于内积除以两个向量模长的乘积。模长都是 1 时除数就是 1,所以余弦相似度就是内积——这一句话就是等价的全部理由,不需要额外的假设。
- 再说没归一化的后果:内积里混着「方向有多一致」和「向量有多长」两层信息。文本越长,模型输出的向量模长往往越大,于是排序会系统性地偏向长文档——这跟 BM25 里 b 参数要压的是同一个毛病,只是换了个地方冒出来。
- 点出这类 bug 的性质:它不报错。程序照常跑、结果照常出,只是名次悄悄偏了,你要跑一轮离线评估才可能发现。所以工程上的做法是在 embedding 的出口统一归一化一次,而不是靠每个调用点自觉。
- 补一句欧氏距离:向量都归一化之后,欧氏距离的平方等于 2 减去 2 倍内积,也就是余弦距离的单调函数,三种距离排出来的名次完全一致。这一句能说明你理解的是关系而不是三条并列的规则。
- 可预期的追问:那 pgvector 里该用哪个运算符?答案是既然已经归一化,`<=>`(余弦距离)和 `<#>`(负内积)名次一样,选 `<=>` 的理由是可读性和「就算哪天有人漏了归一化也不至于错」。
How to reason about it · think before answering
- This starts as a giveaway, but the second half is where candidates separate. Many can say 'they are equivalent after normalisation'; few can describe what breaks without it.
- State the definition: cosine similarity is the inner product divided by the product of the two magnitudes. When both magnitudes are 1, the divisor is 1 and cosine reduces to the inner product. That is the whole argument.
- Then the failure mode: an un-normalised inner product mixes 'how aligned' with 'how long'. Longer texts tend to produce larger-magnitude vectors, so ranking drifts systematically toward long documents, the same bias BM25's b parameter exists to counter.
- Stress that this bug is silent. Nothing throws, results still look plausible, and only an offline evaluation reveals the drift. Hence the engineering rule: normalise once at the embedding boundary, never at each call site.
- Add Euclidean distance for completeness: on normalised vectors, squared L2 equals 2 minus twice the inner product, a monotone function of cosine distance, so all three metrics produce the same ranking.
- Expected follow-up: which pgvector operator should you use? Since the vectors are normalised, `<=>` and `<#>` rank identically; prefer `<=>` for readability and because it stays correct if someone later forgets to normalise.
答题要点
- 余弦相似度 = 内积 / 两个模长之积,模长为 1 时除数为 1,两者等价。
- 没归一化时内积混入模长信息,长文档的向量模长普遍更大,排序会系统性偏向长文档。
- 这类错误不报错,只能靠离线评估发现,所以要在 embed 出口统一归一化。
- 归一化之后欧氏距离与余弦距离互为单调函数,三种运算符名次一致。
- pgvector 里对应 `<->`(L2)、`<#>`(负内积)、`<=>`(余弦距离)三个运算符。
Key points
- Cosine equals inner product divided by both magnitudes; with unit magnitudes the divisor is 1, so they coincide.
- Without normalisation the inner product carries magnitude, and longer documents usually have larger magnitudes, biasing the ranking.
- The failure is silent, so normalise once at the embedding boundary and verify with offline evaluation.
- On normalised vectors L2 and cosine are monotonically related, so all operators rank the same.
- In pgvector the operators are `<->` for L2, `<#>` for negative inner product and `<=>` for cosine distance.