Siamese Networks
A siamese network runs two inputs through twin networks with shared weights, learning similarity itself instead of memorizing categories.
Why Does This Exist?
Classifiers answer which of my known classes this is, which is useless for signature checks, duplicate detection, and one-shot learning, where the classes at test time never appeared in training. The needed question is comparative: are these two the same. Siamese networks exist to learn that comparison directly. Twin encoders with tied weights map both inputs into one space where distance decides, trained by contrastive loss or triplet loss. Trackers reuse the design to compare a template against each new frame.
This page covers the twin-encoder mechanism and its worked distances. The embedding features it produces are feature extraction, and the low-label regime it serves is few-shot vision.
Think of It Like This
Twin judges with one rubric
Two judges score divers separately but trained from the same rubric book, so a 7.5 from either means exactly the same thing. The score gap, not the absolute numbers, decides whether two dives match. Siamese twins are the judges, shared weights are the rubric book, and the distance between embeddings is the score gap.
The analogy stops at the symmetry requirement. Real judges drift apart over a season, while tied weights force the twins identical forever, which is what makes the comparison fair.
How It Actually Works
Both inputs pass through the same encoder with one shared weight set, producing two embeddings and . Here is the neural network, and are the two inputs, and and are their short vector summaries. A distance layer converts the pair into one score: Euclidean distance or cosine distance . Contrastive training pulls same-class pairs together and pushes different pairs apart beyond a margin , with loss , where marks same and marks different. Gradients flow back through both branches and accumulate into the shared weights. At test time the encoder runs once per input and distances against stored gallery embeddings decide, with the operating threshold picked on held-out validation pairs.
Worked comparison
Twin encoders emit and for two signatures, both already unit length. Gaps are , squared distance is , so Euclidean distance is about 0.064. Against a 0.5 operating threshold this pair is confidently same-writer, and its contrastive loss at is . A forgery at gives gaps , squared distance , distance about 0.535, which lands just over the line as different. With margin its loss is , a real push-apart signal.
Code
import math
def euclidean(a, b): return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))
a, b, c = (0.6, 0.8), (0.65, 0.76), (0.1, 0.99)print(round(euclidean(a, b), 4), round(euclidean(a, c), 4))# -> (0.064, 0.5349)The genuine pair sits far below the 0.5 threshold while the forgery lands just above it.
Watch Out For
Untied weights that silently fork
Copying one branch to start the other and then training both independently forks the twins, so identical inputs start scoring differently. The symptom is similarity scores that drift with input order. Fix it by literally sharing one module instance between both paths, never two copies.
Thresholds tuned on training pairs
Picking the operating threshold on the same pairs the network trained on underestimates live error, because training pairs are the ones the model already fits best. The symptom is strong offline numbers with false accepts in production. Calibrate the threshold on held-out validation pairs drawn from unseen classes.
The Quick Version
- Siamese networks learn comparison, not classification, via twin shared-weight encoders.
- Distance between embeddings is the decision, and thresholds come from validation pairs.
- Contrastive and triplet losses supply the push-apart training signal.
- One encoder serves both inputs at test time against a stored gallery.
- Weights must be shared by construction, never merely initialized equally.