Skip to content
C.W.K.
Stream
Lesson 04 of 04 · published

Embeddings as Features

~26 min · embeddings, representation

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Embeddings summarize complex objects as vectors

An embedding maps a sentence, image, product, graph node, or session into a fixed-length numeric vector. Distances are intended to reflect relationships learned from the encoder's objective. Similarity is therefore task-dependent: a vector useful for semantic search may be poor for authorship, safety, or legal equivalence.

Familiar tools can operate on the vectors

Nearest-neighbor search retrieves related objects; clustering explores neighborhoods; logistic regression or boosting can use embeddings for classification or fuse them with structured features. Evaluate the complete downstream decision rather than assuming vectorization solved the task.

Start with a pretrained encoder

A pretrained text or image model provides a fast baseline without representation training from scratch. Build task-specific queries, relevant items, and hard negatives, then compare retrieval or classification quality with lexical search and simple engineered features.

Fine-tuning changes what distance means

Domain examples or contrastive pairs can make relevant objects closer and hard negatives farther apart. Hold out entities and time periods appropriately, version the resulting encoder separately from its base, and verify that gains are not limited to the fine-tuning sample.

Self-training carries the largest responsibility

Autoencoders or contrastive models trained on proprietary data can capture specialized structure, but need enough coverage, careful negative sampling, and a stronger evaluation story. Training loss alone does not prove a useful neighborhood.

Similarity depends on normalization and metric

Cosine similarity, dot product, and Euclidean distance are not universally interchangeable. Normalize only when the model contract requires it. Measure recall or precision at the number of results a user sees and test slices such as language, length, image style, and new categories.

An embedding is a versioned model artifact

Pin encoder identifier and revision, tokenizer or preprocessing, dimension, normalization rule, and distance metric. Store source-object version and embedding time. A silent encoder upgrade changes geometry while downstream code continues to run, so rebuild a separate index and compare before switching.

Budget storage and privacy

Vector dimension, index type, replication, and re-embedding frequency determine storage and compute cost. Embeddings can retain sensitive information or enable linkage even when raw text is absent. Apply access control, retention, deletion, and provenance rules to both vectors and indexes, and never mix incompatible generations in one collection.

Code

Sentence embeddings for tabular fusion·python
from sentence_transformers import SentenceTransformer
import numpy as np

encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embeds = encoder.encode(df["description"].tolist(), show_progress_bar=True)
embeds = np.asarray(embeds, dtype="float32")
X_full = np.hstack([X_tabular, embeds])
Vector search on stored embeddings·python
import numpy as np
from sklearn.preprocessing import normalize

matrix = normalize(embeds)  # cosine sim = dot product after L2-normalize
query = normalize(encoder.encode(["premium subscription churn"]))
sims = matrix @ query.T
top = np.argsort(-sims.ravel())[:10]

External links

Exercise

Pick one free-text column in your dataset. Encode it with sentence-transformers MiniLM. Concatenate to your tabular features. Compare CV score with and without embeddings. Quantify the lift before deciding to ship the encoder.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.