## 2 Background and Preliminaries
### Siamese Neural Network (SNN)  
A Siamese neural network is a category of neural network architectures that contain two or more identical subnetworks. By identical, we mean that they have the same structure, the same parameters, and the same weights. It was introduced by [PERSON] et al. [18] for signature verification written on a tablet. During training, the two sub-networks extract features from two signatures, while the joining neuron measures the distance between the two feature vectors. Verification consists of comparing an extracted feature vector with a stored feature vector of the person signing.  
These subnetworks, which make up the Siamese neural networks, are constructed as feedforward perceptrons, and utilize error backpropagation during the training process. They work in parallel, comparing their output using the cosine distance as illustrated in Figure 1.  
Figure 1: Representation of the structure of the Siamese neural network model. The data are processed from left to right. The value of the cosine distance is a measure of the similarity between the input pair of data instances as the final output [19].  
Deep neural networks (DNNs) are recognized for their reliance on extensive datasets for effective training. For instance, if a model is trained on 10 classes and an extra class is introduced later, the entire model necessitates retraining. In contrast, Siamese neural networks are distinguished for their one-shot learning capability. This signifies that the incorporation of a new class does not mandate a complete retraining of the model. One-shot learning teaches the model to make its own assumptions about their similarities based on the minimal number of visuals. There can be only one image or a very limited number of them, in which case it is often called few-shot learning for each class.  
As an example, consider differentiating between dogs and cats. A traditional ML model would necessitate a large dataset of thousands of training example [20], encompassing various angles, lighting conditions, and backgrounds. In contrast, one-shot learning defies the need for an extensive array of examples in each category. It harnesses its acquired knowledge from prior tasks of the same type, drawing connections among similar objects and effectively categorizing unfamiliar objects into their respective classes.  
During the training of the SNN, we need to ensure two inputs:  
1. The feature vectors of similar and dissimilar pairs should be descriptive, informative, and distinct enough from each other so that segregation can be learned effectively.
2. The feature vectors of similar image pairs should be similar enough, and those for dissimilar pairs should be dissimilar enough so that the model can quickly learn semantic similarity.  
To ensure the model can learn similarity and dissimilarity, it uses a loss function called the contrastive loss function. The contrastive loss function is a distance-based loss function that updates weights such that two similar feature vectors have a minimal Euclidean distance. In comparison, the distance is maximized between two different vectors. The constrastive loss function is given in Equation (1) below:  
\[(1-Y)\frac{1}{2}(D_{w})^{2}+Y\frac{1}{2}(\max(0,m-D_{w}))^{2} \tag{1}\]  
In Equation (1), \(y\) represents whether or not the vectors are dissimilar, and \(D_{w}\) is the Euclidean distance between the vectors. When the vectors are dissimilar (\(y=1\)), the loss function minimizes the second term, for which \(D_{w}\) must be maximized (encourage more distance between dissimilar vectors). We want these vectors to have a distance of more than at least \(m\) (which is a Margin), and we avoid computation if the vectors are already \(m\) units apart by defaulting to zero.