- Introduced by
- Jane Bromley & Yann LeCun et al.; popularized by Koch et al.
- Year
- 1993
- Type
- Deep metric-learning architecture
- Family
- Metric learning
What does it do?
A Siamese network is a deep architecture built from two identical, weight-sharing branches that map inputs into an embedding space where similar inputs lie close together and dissimilar inputs lie far apart.
Introduced by Bromley, LeCun and colleagues in 1993 for signature verification, and applied by Koch et al. (2015) to one-shot image recognition, it learns a similarity measure rather than fixed class labels, which makes it suited to verification, matching and few-shot tasks.
The intuition behind the method
A standard classifier learns a fixed set of categories, which fits poorly when new classes keep appearing (new people, new signatures) or when only one example per class exists. A Siamese network instead learns what 'similar' means. Two inputs pass through the same network, which produces two embeddings, and training pulls genuine pairs together and pushes impostor pairs apart. At test time, verification compares two embeddings, and recognition compares a query against stored references. A new identity therefore needs a reference example rather than a retrained classifier.
History
Bromley, Guyon, LeCun, Säckinger and Shah introduced the Siamese architecture in 1993 for signature verification, using two weight-sharing time-delay networks. Koch et al. (2015) applied the approach to one-shot image recognition. Later metric-learning work developed contrastive and triplet objectives for learning embeddings. Contrastive self-supervised methods use a related idea of pulling matched views together and pushing other samples apart, although they use different architectures and training setups.
When to use it
Choose a Siamese network when the task is to decide whether two inputs are the same or similar, or to match a query against a set of references, for example signature or face verification, person re-identification, duplicate detection, or one-shot recognition. It suits settings where the set of classes is large, open-ended or changing, or where only one or a few examples per class are available, because new classes require reference embeddings rather than retraining. Do not choose it when the class set is fixed and labelled examples per class are plentiful, since a standard classifier is simpler to build and train. It also requires labelled similar and dissimilar pairs or triplets, and the learned metric transfers only to inputs that resemble those in the training pairs.
Limitations and considerations
- Training requires labelled similar and dissimilar pairs or triplets, and the number of possible pairs grows quadratically with the dataset, so informative pairs must be selected.
- Hard-negative mining is often needed, because easy negatives that are already well separated contribute little to learning.
- Each training pair needs two forward passes through the shared encoder (three for a triplet), so pairwise training costs more per example than single-input classification.
- The learned metric generalises only as far as the training pairs cover the variation seen at test time.
- Imbalance between similar and dissimilar pairs can bias the decision threshold τ, so the threshold must be calibrated on pairs whose distribution is close to the intended use.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Learns a similarity measure rather than a fixed label set, so new classes can be added by embedding a reference example instead of retraining.
- Weight sharing passes both inputs through one function, so their embedding distance is directly comparable.
- Supports one-shot and few-shot matching, where a single reference per class is enough at test time.
- The same twin-encoder design applies across input types such as images, signatures and text.
Applications
- Signature, face, and biometric verification.
- Person re-identification and image/object matching.
- Duplicate and near-duplicate detection.
- One-shot and few-shot image recognition.
- Sentence- and document-similarity (Siamese text encoders).
How it works
How it works
Both branches use the same encoder f_θ, with identical architecture and identical weights, so each input is mapped by the same function into a common embedding space. Weight sharing is essential: it guarantees that the two embeddings are produced in the same way, so their distance is meaningful.
Formula
f_\theta(\cdot)\ \text{shared by both branches};\ z_1=f_\theta(x_1),\ z_2=f_\theta(x_2)The two output embeddings are compared with the Euclidean (L2) norm given in the formula. This scalar distance between the two inputs in embedding space is what training adjusts, so that it is small for similar pairs and large for dissimilar pairs. The Euclidean distance is one common choice for this comparison. Other distance functions and learned comparison heads are also used in Siamese designs, so the choice of comparison is part of the model design rather than a fixed property of the architecture.
Formula
d = \lVert z_1 - z_2 \rVert_2
The loss has two terms, selected by the label y. The convention is y = 0 for a similar pair and y = 1 for a dissimilar pair. For a similar pair (y = 0), the (1 - y) term penalises the squared distance directly, which pulls the pair together. For a dissimilar pair (y = 1), only the y term is active; it penalises distances smaller than the margin m, so dissimilar pairs are pushed until they are at least m apart, and pairs already beyond m contribute nothing. A triplet loss over an (anchor, positive, negative) triple is a common alternative that requires the positive to be closer to the anchor than the negative by a margin.
Formula
L = (1-y)\tfrac{1}{2}d^2 + y\tfrac{1}{2}\max(0, m-d)^2After training, the shared encoder produces an embedding for any input. Verification compares the distance with a threshold τ to decide same or different. One-shot or few-shot recognition embeds a query and assigns it to the nearest reference embedding. New classes are handled by adding reference embeddings, without retraining the network.
Formula
\text{similar} \iff d < \tau\ (\text{verification / nearest-embedding match})Common pitfalls
- Sampling pairs at random, which yields mostly easy negatives that are already far apart and weakens learning.
- Choosing the margin m poorly: if it is too small, dissimilar pairs are not separated, and if it is too large, the constraints can become hard to satisfy during training.
- Giving the two branches separate weights, which breaks the common embedding space that the distance depends on.
- Setting the verification threshold τ on training pairs, which overstates accuracy; the threshold should be chosen on held-out pairs.
Frequently asked questions
Why must the two branches share weights?
Weight sharing ensures both inputs are mapped by exactly the same function into the same embedding space, so the distance between their embeddings is meaningful and symmetric. Without shared weights the two branches could embed similar inputs incomparably, making the learned distance unreliable.
What is the difference between contrastive and triplet loss?
Contrastive loss works on pairs: pull similar pairs together, push dissimilar pairs apart beyond a margin. Triplet loss works on an (anchor, positive, negative) triple: make the anchor-positive distance smaller than the anchor-negative distance by a margin. Triplet loss directly optimizes relative ranking and is common in modern metric learning.
How does a Siamese network do one-shot recognition?
It learns a general similarity metric during training. At test time it embeds the query and each candidate reference (one example per class) and assigns the query to the nearest reference embedding. Because it compares similarities rather than predicting fixed labels, a single reference per new class suffices — no retraining.
Related methods
Sources
Bromley, J., Guyon, I., LeCun, Y., Säckinger, E., & Shah, R. (1993). Signature verification using a 'Siamese' time delay neural network. Advances in Neural Information Processing Systems, 6.
Open publication referenceKoch, G., Zemel, R., & Salakhutdinov, R. (2015). Siamese neural networks for one-shot image recognition. ICML Deep Learning Workshop.
Open publication reference