Embeddings and Feature Extraction

LESSON

Deep Learning and Neural Networks

027 30 min intermediate

Embeddings and Feature Extraction

By the end of this lesson, you will be able to...

  • explain an embedding as a learned vector whose useful relationships depend on how it was trained and used;

  • trace how feature extraction turns an input into a vector that a classifier, comparison, or audit can consume;

  • evaluate whether a distance or nearest-neighbor result supports a downstream decision rather than treating it as semantic truth.

Idea in one sentence: An embedding is useful geometry made by an encoder, but “near” is evidence only for the relationship that the encoder, preprocessing, and metric have earned.

Core Insight

Suppose the warehouse team keeps the best frozen backbone from the fine-tuning lesson. An operator now asks a practical question: “This new photo was classified as damaged. Which earlier photos does the model consider similar?”

The team sends the new photo through the backbone and stores the resulting vector. It does the same for a small set of reviewed photographs, then returns the nearest vectors. At first this looks like a simple promise: close vectors must mean the same defect.

That promise is too strong. The backbone may place two images close because both share a lighting pattern, camera angle, or package shape that helped its original training objective. The two images can be close and still require different operational decisions. Conversely, two photos of the same defect can be separated if the encoder did not learn to ignore a target-specific nuisance.

The stronger idea is that an embedding is a learned representation in vector form. It gives a model a space in which some relationships may be easier to express. Its geometry is not a universal map of meaning. To use it responsibly, inspect the encoder, the chosen output layer, preprocessing, metric, neighborhood examples, and the decision the neighborhood is supposed to support.

The Small Situation: From Image to Feature Vector

The team has a fixed encoder and a labeled reference set. The following is a teaching model of the pipeline, not an execution trace from a real warehouse system.

package photograph
  -> fixed preprocessing
  -> pretrained backbone
  -> selected intermediate output
  -> vector e = [e1, e2, ..., ed]
  -> normalize and compare with reference vectors
  -> inspect neighbors before making a downstream decision

The encoder is the part that maps an input into the vector. Feature extraction means choosing an intermediate or final internal output of a trained model and returning it as a feature vector. When that vector is used with a comparison rule—such as nearest-neighbor search, clustering, or a lightweight classifier—it often serves as an embedding of the input.

There is a related but smaller case. For a categorical token or product id, an embedding layer is a learned lookup table: the integer id selects one row of trainable vector values. PyTorch describes nn.Embedding this way: indices go in, corresponding vectors come out. That is a direct mechanism for discrete inputs. An image encoder produces a vector by applying many learned transformations rather than one table lookup, but both outputs can be called embeddings when their vector relationships are useful to the task.

The Initial Model: Every Coordinate Has a Human Meaning

An embedding is a list of numbers, so it is tempting to read each coordinate as a named property: perhaps dimension 12 means “corner damage” and dimension 47 means “cardboard texture.” This sometimes happens in specially designed representations, but it is not a safe default for learned neural features.

The initial model works for one modest purpose: a vector has coordinates, so arithmetic can be applied to it. It breaks when we turn that arithmetic into an explanation. Learned information is usually distributed across coordinates. Rotating a representation can preserve many pairwise distances while changing every individual coordinate, which shows why one coordinate need not carry a stable human concept.

Another tempting model says that the nearest image is automatically the most relevant example. That works only when the encoder and distance function have been evaluated for the relation the user cares about. A neighbor search is a measurement from a particular representation; it is not an independent label, causal explanation, or proof of similarity in every respect.

The Better Model: Learned Geometry Under a Declared Metric

In plain English, an embedding makes an input into a point. The directions and distances between points can make a learned distinction easier for another procedure to use.

In the warehouse scenario, each photo becomes a point. A target image can be compared with reviewed reference images; a nearby cluster might help an auditor find analogous cases or a classifier find a useful boundary.

The technical name for the vector and its relationships is an embedding space. Its geometry is defined jointly by four choices:

Choice Question to record Why it changes the result
Encoder and checkpoint Which model and selected layer produced the vector? Different layers preserve different information.
Preprocessing How were crop, resize, color conversion, and normalization applied? A changed input contract can move a point for reasons unrelated to the defect.
Representation preparation Were vectors normalized, pooled, or reduced? These operations change which magnitude or direction differences matter.
Metric Is “near” cosine similarity, Euclidean distance, or another declared comparison? The same vectors can receive a different ordering under a different metric.

For normalized vectors, cosine similarity is the dot product:

cosine_similarity(a, b) = a · b

It compares direction rather than raw vector length. That can be useful when the chosen representation's direction encodes the relation of interest. It is not inherently the right metric. Euclidean distance measures straight-line separation and can react differently to vector magnitude. The metric belongs in the experiment and the evaluation, not in a slogan such as “cosine means semantic similarity.”

A Worked Neighborhood Audit

Assume, for a small two-dimensional teaching example, that preprocessing and the encoder have already produced unit-length vectors. Real embeddings are normally much higher-dimensional; these values are deliberately simplified so the comparison is visible.

Image Reviewed label Unit embedding Cosine similarity with query
Query: Q unknown [0.80, 0.60] 1.00
A crushed corner [0.88, 0.48] 0.99
B intact, same camera glare [0.76, 0.65] 0.998
C torn film [-0.20, 0.98] 0.43

For the query and B, the illustrative calculation is:

Q · B = (0.80 × 0.76) + (0.60 × 0.65)
      = 0.608 + 0.390
      = 0.998

The naive conclusion is “B is nearly identical, so the query is intact.” But the review label says B is intact while the query is unknown. The high score supports a narrower statement: under this encoder, preprocessing, and cosine metric, B is the closest of these reference vectors. It does not settle the query's defect label.

This mismatch is useful evidence. The encoder may be paying too much attention to camera glare, the reference set may lack relevant damaged examples, or the selected layer may be wrong for the decision. To investigate, keep the comparison contract fixed and inspect more neighbors across defect, camera, material, and lighting slices. Then compare the neighborhood behavior against human-reviewed labels. If close points consistently mix intact and damaged packages under the desired conditions, the geometry is not adequate for that decision even if it looked attractive in a two-point demo.

So far, we have seen that a nearest neighbor is a result of a specific representation and metric. This matters because the team can use neighborhood inspection to find a representation failure instead of mistaking a high similarity score for an answer.

What Feature Extraction Changes

Feature extraction separates two jobs that an end-to-end model normally hides:

encoder job: turn input into a vector
downstream job: classify, compare, retrieve, cluster, or audit that vector

This separation can make a small target experiment cheaper and easier to inspect. A frozen backbone can generate features once; a new lightweight classifier can test whether those features separate a new target label. An auditor can inspect a nearest-neighbor set without claiming that the neighbors are the classifier's whole reasoning process.

It also creates a contract. A stored vector cannot be safely compared with a newly generated vector unless the encoder version, selected layer, preprocessing, pooling, normalization, and metric are compatible. A change to any of these can create a new embedding space even when the vectors have the same dimension. The same shape—say 512 numbers—is not evidence that two spaces share a meaningful geometry.

TorchVision exposes this idea directly: its feature-extraction utility can return chosen intermediate nodes from a model. The returned value is not automatically an embedding for every use; the downstream task and evaluation determine whether that output is useful geometry. TorchVision's feature-extraction documentation shows the mechanics of returning such intermediate outputs.

Consequences, Trade-offs, and Limits

Embeddings can make comparison and reuse efficient. A vector can be stored, indexed, or passed to a simpler downstream model instead of repeatedly exposing every raw input to a large task-specific pipeline. They also make audits possible: a reviewer can see which examples are near under a declared geometry.

This buys efficiency and a useful inspection surface, but it costs validation work. A distance metric, neighbor count, reference set, and preprocessing contract all introduce choices that can fail quietly. An embedding does not preserve all information in the original input, and a compact vector can discard the very distinction a downstream decision needs.

The boundary appears when neighborhood quality fails on the relationship that matters: relevant examples are far apart, irrelevant examples dominate the nearest results, or performance changes sharply by domain slice. In that case, change only one hypothesis at a time: inspect the input contract, layer, representation, metric, reference data, or pretraining/adaptation choice. Do not repair a weak geometry by merely renaming its distances “semantic.”

Common Confusions

Confusion: An embedding is a compressed explanation of an input.

Why it is tempting: the vector is smaller than the original image or a large one-hot vector. Better model: it is a learned representation optimized by its training objective. Compression can preserve useful structure while losing other information, and its coordinates need not be human-readable explanations.

Confusion: A high cosine similarity proves two inputs have the same meaning.

Why it is tempting: the score is precise and ranks examples. Better model: it proves only that the chosen vectors point in a similar direction. Whether that supports a semantic, label, or operational relation must be tested on representative pairs.

Confusion: Feature extraction is manual feature engineering.

Why it is tempting: both produce features for a downstream model. Better model: manual engineering defines features explicitly; learned feature extraction returns activations learned by a model. They can be combined, but they are different sources of structure.

Check Your Understanding

Check: Two image vectors have the same dimension, but one came from a new encoder version with different image normalization. Can they be safely mixed in one nearest-neighbor index because their shapes match?

Think first, then reveal.

Answer: No. The dimensions only show the same vector length. A different encoder or preprocessing contract can change the geometry, so distances between old and new vectors may not represent the intended relation. Recompute or isolate the index under a compatible versioned contract.

Check: In the worked audit, B is closer to the query than A, but B is intact and A has a crushed corner. What should the team investigate first?

Think first, then reveal.

Answer: Inspect a representative neighborhood and its slices before drawing a label conclusion. The result suggests that the current geometry may overvalue glare or camera conditions, but it does not identify the cause by itself. Check input preprocessing, reference coverage, selected layer, and metric against reviewed labels.

Practice: Design a Neighborhood Test

A team wants to use image embeddings to route new product photos to a reviewer by showing five similar historical cases. Write a compact test plan that names:

Model answer: Version the selected encoder layer, resize/crop and color-normalization policy, vector pooling and L2 normalization, and cosine metric. Include both matching defects under varied cameras and visually similar intact packages, because both test whether the neighbors support the routing decision. A useful signal is that the top-five set contains reviewed examples of the same defect across camera variation at an agreed rate. Stop rollout if intact glare-heavy images repeatedly occupy the nearest set for damaged queries, or if results differ sharply by camera without a documented reason. The test evaluates the reviewer aid, not a claim that vectors contain ground-truth semantics.

Resources

Key Takeaways

PREVIOUS Fine-Tuning Pre-trained Models NEXT Domain Adaptation and Few-Shot Learning