Embeddings and similarity
Similarity is a modeling choice: the same vectors tell different stories under cosine and dot product.
Novel equipment failures appear before reliable labels exist.
This chapter moves from define a positive pair to evaluate neighborhoods. We will make the idea concrete in a small code example before placing it back inside a production system with a declared latency, cost, and operator-capacity budget.
Measure recall at k and inspect nearest neighbors by slice, including fresh and rare items, under the exact encoder version used to build the index.
Verify the behavior illustrated in cosine_search.py, then measure decision utility, slice reliability, and system latency.
A stale ANN index never retrieves new inventory; a reranker cannot recover what retrieval omitted. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
An embedding turns an object—text, an image, or an incident—into a vector so that useful relationships become geometric. Retrieval works only when that geometry, the similarity rule, and the product notion of relevance agree.
Cosine similarity compares direction and ignores magnitude, while dot product rewards both alignment and vector norm. If vectors are L2-normalized, the two rankings coincide. Without normalization, frequently seen items may acquire larger norms and outrank a better semantic match.
The encoder is part of the index contract. Changing its weights changes the coordinate system, so query vectors from one version cannot safely search item vectors from another. Evaluate pairs, hard negatives, slices, and downstream retrieval—not just an attractive two-dimensional plot.
- anomaly triage
- cosine similarity
- HNSW
- recall-latency tradeoff
Follow the reasoning before touching the code.
- 01
Define a positive pair
Write concrete examples that should be close, plus difficult negatives that share surface details but should not satisfy the same need.
- 02
Choose the score intentionally
Normalize when direction expresses semantics, or retain norms only when magnitude has a validated meaning in the training objective.
- 03
Evaluate neighborhoods
Measure recall at k and inspect nearest neighbors by slice, including fresh and rare items, under the exact encoder version used to build the index.
import numpy as np
items = np.array([[3., 0.], [1., 1.], [0., 2.]])
query = np.array([1., 0.8])
items = items / np.linalg.norm(items, axis=1, keepdims=True)
query = query / np.linalg.norm(query)
scores = items @ query
for index in np.argsort(-scores):
print(index, round(scores[index], 3))Normalize vectors before ranking three candidate incidents.
What to notice
- After normalization, the dot product is cosine similarity and ranks the diagonal candidate above the larger horizontal vector.
- The score orders candidates but is not automatically a calibrated probability that a human will judge them relevant.
YOUR TURN Pause and predict before revealing the answer.
Remove normalization. Which item benefits most, and what hidden assumption would justify that result?
Need a hint?
Compare the candidate norms before looking at their directions.
Check your reasoning
The [3, 0] item gains from its large norm; that ranking is justified only if norm magnitude intentionally encodes useful strength or confidence.
Lessons 1, 2, 3 in this course.
Connect the chapter to the course’s quantitative spine.
Similarity is a modeling choice: the same vectors tell different stories under cosine and dot product. The cosine similarity below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
Cosine similarity
| Symbol | Meaning / shape / unit |
|---|---|
u, v | embedding vectors |
uᵀv | dot product |
∥·∥₂ | vector magnitude |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: u, v, uᵀv, ∥·∥₂.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c06(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
ANN recall–latency lab
Increase search effort and expose the recall/latency frontier.
Assumption: HNSW-like curve for a fixed one-million-vector index.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| query 1 | 82.7% | 4.7 ms |
| query 2 | 82.0% | 4.9 ms |
| query 3 | 81.2% | 5.1 ms |
| query 4 | 80.4% | 5.3 ms |
| query 5 | 79.5% | 5.5 ms |
Trace the complete operating path.
Carry the chapter’s central idea into the system: Similarity is a modeling choice: the same vectors tell different stories under cosine and dot product. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Define a positive pair” into a written invariant and a failing test.
- 02
Promote cosine_search.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Evaluate neighborhoods” as the release checkpoint: Measure recall at k and inspect nearest neighbors by slice, including fresh and rare items, under the exact encoder version used to build the index.
- 04
Serve or materialize behind explicit a declared latency, cost, and operator-capacity budget.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
A stale ANN index never retrieves new inventory; a reranker cannot recover what retrieval omitted. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Incident discovery and retrieval system
Combine PCA, clustering, anomaly triage, embeddings, and ANN search with human review.
- Representation analysis
- Cluster/anomaly validation
- Embedding evaluation
- ANN recall/latency audit
Read primary material with a purpose.
Return to the opening failure.
Version encoder and index together; monitor cluster stability, alert yield, exact-sample recall, latency, and freshness.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a pca, clustering, anomaly, and nearest-neighbor retrieval investigation.