Skip to content
AI360Xpert
Beta

Siamese Networks

A siamese network runs two inputs through twin networks with shared weights, learning similarity itself instead of memorizing categories.

Twin shared-weight encoders map two photos into one space where distance decides same or different: 0.06 is same, 0.53 is different at a 0.5 threshold.
Twin shared-weight encoders map two photos into one space where distance decides same or different: 0.06 is same, 0.53 is different at a 0.5 threshold.

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 ff with one shared weight set, producing two embeddings a=f(x1)a = f(x_1) and b=f(x2)b = f(x_2). Here ff is the neural network, x1x_1 and x2x_2 are the two inputs, and aa and bb are their short vector summaries. A distance layer converts the pair into one score: Euclidean distance d=∥a−b∥d = \lVert a - b \rVert or cosine distance 1−cos⁡(a,b)1 - \cos(a, b). Contrastive training pulls same-class pairs together and pushes different pairs apart beyond a margin mm, with loss (1−y)d2/2+ymax⁡(0,m−d)2/2(1 - y) d^2 / 2 + y \max(0, m - d)^2 / 2, where y=0y = 0 marks same and y=1y = 1 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 a=(0.6,0.8)a = (0.6, 0.8) and b=(0.65,0.76)b = (0.65, 0.76) for two signatures, both already unit length. Gaps are (−0.05,0.04)(-0.05, 0.04), squared distance is 0.0025+0.0016=0.00410.0025 + 0.0016 = 0.0041, so Euclidean distance is about 0.064. Against a 0.5 operating threshold this pair is confidently same-writer, and its contrastive loss at y=0y = 0 is 0.0041/2≈0.0020.0041 / 2 \approx 0.002. A forgery at c=(0.1,0.99)c = (0.1, 0.99) gives gaps (0.5,−0.19)(0.5, -0.19), squared distance 0.25+0.0361=0.28610.25 + 0.0361 = 0.2861, distance about 0.535, which lands just over the line as different. With margin m=1.0m = 1.0 its loss is (1−0.535)2/2=0.216225/2≈0.1081(1 - 0.535)^2 / 2 = 0.216225 / 2 \approx 0.1081, 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.