Learning pathsPricingChecking sign-in…
← ML curriculum

MLF-014 · FOUNDATION

Embeddings and representation contracts

Embeddings are versioned representations trained for a particular relationship. Explain the objective, score geometry and serving compatibility rather than assuming vectors are interchangeable.

Author self-review; independent technical review pending. Deterministic synthetic vector/rank/contract fixtures; no trained model, ANN benchmark or production service.

Prerequisites and scoped previews

Task families and label semantics

Distinguish classification, regression, ranking, retrieval, forecasting and generation.

Scoped prerequisite preview. The complete chapter is planned; this preview does not count as authored coverage.

Feature engineering and transformations

Create numeric/categorical/text features, defaults and fitted transformations without leaking validation data.

Scoped prerequisite preview. The complete chapter is planned; this preview does not count as authored coverage.

Non-ML and simple learned baselines

Establish a deployable baseline before a complex model and keep comparisons controlled.

Scoped prerequisite preview. The complete chapter is planned; this preview does not count as authored coverage.

What, when, why and how

Representation and training signal

what
An embedding maps an entity or input into a fixed-length numeric vector. Its useful relationships depend on the objective and data used to train it.
when
Use embeddings when a learned compact representation can help retrieval, ranking or transfer across tasks under measured quality/cost constraints.
why
A co-watch representation may capture audience overlap rather than semantic equivalence. Identical vector dimensions alone do not mean two encoders share a compatible space.
how
Define input features and availability, output dimension, objective, negatives, normalization and representation version. Compare a popularity or sparse-feature baseline and verify the downstream task, not a pleasing vector visualization.

Similarity, distance and loss direction

what
Inner product is a similarity to maximize; squared Euclidean distance is a distance to minimize. Cosine is a normalized inner product for nonzero vectors.
when
Use this whenever training, indexing and query scoring must agree on which item should rank higher.
why
A distance/similarity sign error can train the opposite ordering, while unnormalized vector norms can dominate a supposedly semantic match.
how
For similarity s, the margin loss is max(0, m - s_positive + s_negative). State normalization and reject zero vectors for cosine. Check a positive and negative example by hand, then verify that the search index uses the matching metric.

Dual encoders and cold-start behavior

what
A dual encoder produces a query/user vector and an item vector whose joint objective supports a shared scoring function. ID lookup tables and feature-based encoders have different new-entity behavior.
when
Use separate item encoding when candidates can be precomputed and a fast query-time lookup is valuable.
why
A new item has no learned ID-only vector; using random or all-zero vectors hides a quality/failure policy. A jointly modeled query-item network may be more expressive but cannot generally precompute a query-independent item score.
how
Start with content/metadata features for new items, precompute item vectors, and version the query encoder with the item encoder and index. Later compare a reranker with richer interactions. Sparse or absent metadata still needs a bounded fallback.

Compute scores and the training direction

Let q=(1,0), item A=(1,0), B=(0.8,0.6) and C=(0,1). These are unit vectors. Inner products are 1, 0.8 and 0, so retrieval orders A, B, C. Squared distances are 0, 0.4 and 2. On these normalized inputs the order agrees when distance is minimized.

With margin m=1, positive similarity 3 and negative similarity 1, hinge loss max(0,1-3+1)=0. Swap positive and negative scores and loss becomes 3. This numerical case intentionally uses general similarity scores, not cosine-bounded values. It tests direction, not learned quality.

For unnormalized candidate X=(10,10), dot(q,X)=10 while cosine(q,X)=1/sqrt(2)≈0.7071. X outranks A by dot product but not cosine. Select the metric from the objective; do not describe one result as a bug unless the contract requires the other.

Specify a plausible training recipe

For a video retrieval exercise, a user encoder consumes pre-request permitted history and context, while an item encoder consumes item metadata available at indexing time. Both output d components. Score is their dot product under the declared normalization contract.

A contrastive objective can compare an observed positive with sampled candidates. Random, in-batch and mined negatives trade cost and difficulty; an unexposed item is not known to be disliked. Other positives in the batch can become false negatives, so exclude known duplicate/positive identities and document what remains uncertain. Split by time and fit transformations only on training.

The two-dimensional fixtures do not train either encoder. A production training recipe needs optimization, data quality, objective validation and resource experiments. Do not invent layer widths or GPU budgets without workload evidence.

Reject a mixed representation release

Bundle E1 declares encoder_family=video-v1, dimension=2, metric=dot and normalization=unit. An index built with E1 is compatible only with the matching query-encoder contract. An E2 encoder of the same dimension but a different trained space must be rejected, not accepted because the shape fits.

Rebuild candidate vectors and the index under E2, validate retrieval against its matching exact oracle, then promote a complete compatible bundle. Retain the E1 bundle for rollback. Content revocation and eligibility remain request-time constraints even if an old vector remains in an index.

CheckFailure exampleSafe result
Representation identitySame dimension; different encoder spaceReject incompatible request/index bundle.
Dimension / metric2-D query against 3-D index or dot vs cosineFail validation before scoring.
Finite valuesNaN or Infinity componentReject invalid vector and count the error.
Cosine normalizationZero vector has no directionUse explicit missing-vector fallback.

Choose an affordable representation

An ID-only matrix factorization baseline is useful with dense interaction history, but needs an explicit new-entity policy. A feature-based dual encoder can embed a new item when valid features exist; that does not guarantee relevance for an unseen distribution. A joint query-item reranker can model richer interactions at higher per-pair serving cost.

A research/pattern card should say what transfers: the separation between precomputed candidate representation and request-time ranking. The historical 2016 YouTube paper motivates two stages; it is not evidence for current YouTube architecture, a universal two-tower architecture or this exercise’s measured speed.

Diagrams and relationships

Embedding compatibility is a contract

The query and item paths must converge on the same representation contract before scoring.

Scroll horizontally on smaller screens, or enlarge the diagram.

Embedding compatibility is a contractThe query and item paths must converge on the same representation contract before scoring. User/context encoder E1 to Compatible score function: query vector. Item encoder + index E1 to Compatible score function: candidate vectors.query vectorcandidate vectorsUser/context encoder E1Item encoder + index E1Compatible score function
Embedding compatibility is a contractThe query and item paths must converge on the same representation contract before scoring. User/context encoder E1 to Compatible score function: query vector. Item encoder + index E1 to Compatible score function: candidate vectors.query vectorcandidate vectorsUser/context encoder E1Item encoder + index E1Compatible score function

Engineer, Senior and Staff follow-ups

These are teaching lenses. Each answer changes the scope of reasoning; they are not official employer leveling criteria.

Engineer: Why does X win by dot product but not cosine?

Its larger norm increases dot product to 10. Normalization removes that magnitude contribution, giving cosine about 0.7071 versus A’s 1. Neither metric is automatically correct; the learned objective and index contract decide.

Engineer: What does the swapped hinge example verify?

It checks that a correctly ordered positive incurs no loss at the chosen margin, while a reversed ordering incurs a positive loss. A formula that penalizes the first and rewards the second has the wrong sign for similarity.

Senior: Can equal dimensions prove that an index is compatible?

No. Independently trained spaces can rotate or encode different relationships while keeping the same dimension. Validate representation identity, transforms, metric and normalization alongside dimensions; promote query/index versions together.

Senior: How do you represent a brand-new video?

Use an eligible content/metadata encoder with the compatible model version if those features exist. Otherwise use a declared popularity/editorial fallback. Record new-item coverage and quality separately rather than treating absent behavior as negative feedback.

Staff: How do teams share embeddings without creating hidden coupling?

Publish a versioned contract and support window, task-scoped evaluation, refresh/retention ownership and a migration plan. Consumers validate suitability for their outcome; a retrieval embedding is not automatically a safe moderation or identity representation.

Staff: What does a rollback need besides old weights?

The matching query/item encoders, transform configuration, normalization, index snapshot, policy/calibration where relevant and retained permitted artifacts. A weights-only rollback into a newer index may silently change scores.

Practice and self-review

Your practice draft

Compare dot and cosine for A and X, compute the two hinge cases, then explain how a dimension-preserving encoder change should be released.

Saved locally on this device. The worked answer does not score your draft. Existing lesson notes and completion are separate.

Progressive hints

Hint 1

Write the norm as well as the dot product.

Hint 2

Use one score convention consistently in loss and retrieval.

Hint 3

A shape check is only one compatibility check.

Reveal worked answer guidance

Dot chooses X; cosine chooses A. Correctly ordered margin loss is zero and the swapped loss is three. A different representation identity needs a matching index rebuild and validation; reject mixed bundles and preserve the previous compatible bundle for rollback.

Evidence to check in your answer

  • Correct geometry and loss sign.
  • Invalid/zero-vector behavior explicit.
  • New-entity assumptions stated.
  • Compatible release and rollback named.

For each item, record Missing, Prompted, Independent or Adaptive evidence. Adaptive means you can revise the reasoning under a changed constraint. Reading completion and readiness are separate.

Sources and verification scope

Original synthetic examples and reference calculations. Sources support narrowly scoped background; they do not establish production performance or independent chapter review.

Deep Neural Networks for YouTube Recommendations ↗ · 2016 publication page/abstract inspected for candidate-generation versus ranking separation only. Full PDF could not be retrieved. No current YouTube architecture or full-paper review claim.

Faiss metric types and distances ↗ · Metric/normalization definitions only; the fixture executes exact scalar math and no Faiss/ANN benchmark.

Primary background links checked during authoring on 2026-10-19. Arithmetic, temporal eligibility, maturity and split reference functions are checked by the bounded local fixture suite. SQL is explanatory, not a migrated production query. No GPU, external dataset, model API or training job is required.