In the modern world of deep learning, there are neural networks to perform tasks like regression and classification, but training these models requires a large amount of data. For example, if we want our model to classify cats and dogs, we must pass many images to our model to classify them accurately. The model's accuracy depends upon the dataset and the neural network size. Remember that in a classification problem, the model can only predict the class it is trained in. If you want your model to recognize elephants, you must retrain it on the elephant dataset.
This retraining process in deep learning is very costly and sometimes requires much time. To avoid this problem, Siamese networks help train the model with dynamic classes (adding more classes). In short, we can make the model predict more classes without training.
In the traditional classification setting, the goal is to assign each input to a specific class label, and the model is trained on labeled examples representing different classes. On the other hand, a Siamese network is designed to learn a similarity metric directly from the data, which enables it to compare pairs of inputs and determine their similarity or dissimilarity.
The Siamese neural network consists of two identical sub-networks, a loss function, and a metric layer. In this network, we take pair of inputs, extract the features and capture the similarity and dissimilarity with the help of metrics such as Euclidean distance or Manhattan distance.
The architecture of the Siamese neural network is given below.
An explanation of the working of the Siamese neural network is given below.
Prepare the dataset in a pair-based format. Label these pair as similar or dissimilar based on their ground truth.
Choose a suitable loss function according to your dataset.
Extract the features from the input pairs, calculate the similarity index, and compute the gradients.
While training, update the shared parameters based on the computed gradients.
In a Siamese neural network, they share the same weights and parameters to measure the similarity effectively.
The two commonly used loss functions are mentioned below for the Siamese neural network.
Contrastive loss: It trains the Siamese network with similar (positive) and dissimilar (negative) data points. It aims to minimize the distance between similar pairs and maximize the distance between dissimilar pairs in the embedding space.
Triplet loss: It is used when training the Siamese network with triplets of data points, such as an anchor, a positive example (similar to the anchor), and a negative example (dissimilar to the anchor). It aims to minimize the distance between the anchor and the positive points while maximizing the distance for the negative points.
Note: If you are interested in learning the difference between a triplet loss and a contrastive loss, then here is a link.
Several notable applications of Siamese neural networks exist in image recognition and classification. Some of these are discussed below.
They are used to determine the similarity between two images. It is widely applicable in image search, face recognition, and image clustering.
They are used to verify the signatures to determine whether they are genuine or forged.
They help determine the molecular similarity by comparing the chemical compounds' structures.
Most recommendation systems are based on Siamese neural networks that predict user preferences.
Moreover, they are still in use where similarity is required.
In conclusion, Siamese neural networks are a robust architecture designed for learning similarity or distance metrics between pairs of data points. They consist of two identical sub-networks with the same set of parameters, enabling them to learn a standard representation for each input. They are particularly useful in scenarios with limited labeled data and pairwise comparison tasks. Siamese neural networks are a valuable tool for tasks involving similarity assessment, verification, and comparison, and they continue to be an active area of research in deep and machine learning.
Test your understanding.
What does the Euclidean distance measure in the context of Siamese neural networks?
The difference in activations between the two sub-networks.
The similarity between the input data points.
The gradient of the loss function.
The margin between similar and dissimilar pairs.
Free Resources