Handling negative values in vector components
Many embedding models output vectors with both positive and negative components. This is normal and does not require special “handling” for cosine similarity, dot product, or L2—all of these are defined for real-valued vectors and work with negative values.
Summary
- Cosine and dot product can be negative (opposite direction); L2 is non-negative. For quantization to INT8, use a scheme that supports negative values (symmetric range or offset).
- Binary or non-negative methods may assume non-negative input; signed embeddings need adaptation. Modern VDBs and indexes (e.g. HNSW, IVF) support float or quantized signed vectors.
- No need to shift or clip embeddings to non-negative for L2, cosine, or dot product; all are defined for real-valued (signed) vectors.
- Pipeline: use symmetric INT8 (or similar) for quantization when embeddings are signed; avoid [0,1]-only schemes unless you’ve shifted the data.
- Trade-off: no special handling needed for L2/cosine/dot product; quantization and binary schemes must support signed range.
Semantics and quantization
Cosine and dot product can produce negative similarity (meaning vectors point in opposite directions), which is semantically valid. L2 and other distances are non-negative by definition. Where negativity can matter is (1) quantization: mapping float embeddings to INT8 may assume a range; you need a scheme that supports negative values (e.g. symmetric range or offset). (2) Certain binary or non-negative matrix factorizations assume non-negative inputs; if your embeddings are signed, those methods need adaptation or a different metric.
Practical tip: when enabling scalar quantization, choose a symmetric range (e.g. INT8 [−128, 127]) or an offset scheme so that negative components are represented; otherwise accuracy can drop. See scalar quantization (SQ): float32 to INT8 for implementation options.
Practice
In practice, modern vector databases and indexes operate on float or quantized vectors without requiring non-negativity. Just ensure your distance or similarity choice matches how the model was trained (e.g. cosine vs. L2), and that any quantization or storage format you use supports the full value range of your embeddings.
Trade-off: binary quantization (sign bit per dimension) discards magnitude and works on signed values; full scalar quantization preserves range with symmetric or offset mapping. HNSW and IVF work on real-valued vectors; distance computations (L2, dot product) are defined for negative components, so no special handling is needed at search time.
Pipeline summary: use your embeddings as-is for L2, cosine, and dot product. When enabling quantization, pick a scheme that supports the full value range (symmetric INT8 or offset); avoid [0, 1] or unsigned-only mapping unless you explicitly shift. Trade-off between compression ratio and accuracy applies to quantized signed vectors as well; see how quantization reduces memory footprint.
Frequently Asked Questions
Do I need to shift embeddings to be non-negative?
No. L2, cosine, and dot product work with negative components. Only some specialized methods (e.g. non-negative factorization) assume non-negative input.
What if my quantization assumes [0, 1]?
Use a scheme that maps to a signed range (e.g. INT8 symmetric [−128, 127]) or offset; scalar quantization (SQ): float32 to INT8 describes options.
Can cosine similarity be negative?
Yes. Negative cosine means angle > 90° (vectors point in opposite directions). Common with signed embeddings.
Do HNSW and IVF support negative values?
Yes. They work on real-valued vectors; distance computations (L2, dot product) are defined for negative components.