Siamese Networks
The problem: classification doesn't scale to new classes
A standard classifier learns a fixed set of output classes. Add a new face to a face-recognition system, a new product to a catalog, or a new category you've never seen before, and a normal classifier is stuck — it needs retraining with a new output layer.
Siamese Networks sidestep this by never learning to classify at all. Instead, they learn a similarity function: given two inputs, output how similar they are. Once trained, comparing a new, never-before-seen input to a reference is just a forward pass and a distance calculation — no retraining required.
Twin networks, shared weights
A Siamese Network consists of two (or more) identical sub-networks with tied, shared weights. Each copy processes a different input and produces an embedding vector. Because the weights are shared, both inputs are mapped into the same embedding space using the exact same function — this is what makes the resulting distances meaningful and comparable.
The training objective pulls embeddings of similar pairs (two photos of the same person) closer together and pushes embeddings of dissimilar pairs (photos of different people) further apart. The network never explicitly predicts "who is this" — it only ever learns "how far apart should these two embeddings be."
Note
Face verification: the canonical application
Face verification asks "are these two photos the same person," which is fundamentally a one-shot similarity problem, not a classification problem — you cannot retrain a classifier every time a new employee badge photo is added. A Siamese (or triplet) network trained once on a large face dataset produces an embedding space where same-person photos cluster tightly and different-person photos sit far apart, regardless of whether that specific person's face appeared during training.
Unlocking a phone by face, verifying an ID photo against a selfie, and deduplicating a photo library all use this exact pattern: embed, compare distance, threshold.
Few-shot learning with Siamese networks
The same architecture underlies few-shot classification: given just one or a handful of labeled examples per class, classify a new query by comparing its embedding against the embeddings of the labeled examples and picking the nearest one (nearest-neighbor in embedding space). This is literally how the original Siamese network paper framed one-shot image recognition, and it's conceptually the ancestor of later few-shot approaches like prototypical networks and matching networks.
Siamese networks vs. standard classifiers
| Property | Standard classifier | Siamese network |
|---|---|---|
| Output | Fixed set of class probabilities | A similarity score / distance between two inputs |
| New classes | Requires retraining the output layer | Works immediately — no retraining |
| Training data shape | (input, label) | (input A, input B, same/different) or triplets |
| Typical use case | Fixed-category classification | Verification, deduplication, few-shot recognition |
Note
What's next
Siamese networks solve similarity with a fixed pair of examples at inference time. Meta-learning approaches like MAML go further: they learn how to adapt an entire model quickly from just a handful of examples, rather than only comparing embeddings. The next lesson, One-Shot Learning & Meta-Learning, covers that broader family.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.