## 3 Background  
### Contrastive Learning  
Contrastive learning is self-supervised learning that encourages similar data points to stay close together in the embedding space, while dissimilar ones stay far apart. In other words, contrastive learning is an approach to create a model for finding similar and dissimilar things in machine learning.  
Contrastive loss proposed by [PERSON]. [36; 37] is the first training objective that was used for contrastive learning. It takes a pair of embedding vectors \((x_{i},x_{j})\) and a label, either 1 (if they belong to the same class) or 0 (if they belong to different classes). The objective function then tries to decrease the distance between two embedding vectors with label 1, and increase the distance between two embedding vectors with label 0. The contrastive loss formula is defined as:  
\[L_{CL}(x_{i},x_{j},Y)=(1-Y)\frac{1}{2}(D_{x_{i}x_{j}})^{2}\ +\ Y\frac{1}{2} \Big{\{}max(0,m-D_{x_{i}x_{j}})\Big{\}}^{2} \tag{1}\]  
where \((x_{i},x_{j})\) is a pair of embedding vectors, \(Y\) is a label that can be 0 or 1, \(m\) is a margin that defines the lower boundary distance between dissimilar samples, and \(D(\cdot)\) is a Euclidean distance between a pair of embedding vectors of samples.  
Triplet loss is an improvement of the contrastive loss first proposed in Ref. [38] for face recognition. It outperforms the former by using triplets of samples instead of pairs. Triplet loss consists of an anchor, a positive, and a negative in the form of \((x_{i},x_{i}^{+},x_{i}^{-})\). Anchor and positive belong to the same class while negative belongs to different classes. The loss is calculated over triplets of anchor-positive-negative, so that for each triplet the anchor-positive distance must be smaller than the anchor-negative distance. The formula for triplet loss is defined as follows:  
\[L_{TL}=max\big{(}D\big{(}x,x^{+}\big{)}-D\big{(}x,x^{-}\big{)}+m,\ 0\big{)} \tag{2}\]where \(D(\cdot)\) is distance function, which can be Euclidean or cosine, and \(m\) is a parameter that specifies how far dissimilar samples should be from the anchor.  
Triplet Mining  
The performance of the triplet loss depends strongly on the selection of the triplets. On the other hand, selecting the triplet randomly leads to non-convergence [38; 39]. To overcome this problem, Ref. [38] proposed triplet mining. As shown in Figure 2, depending on the distance between the anchor, positive, and negative points, there are three possible types of triplets: easy triplets, semi-hard triplets, and hard triplets.  
In the original FaceNet paper [38], the authors used semi-hard triplets in their work. However, offline triplet mining, e.g., before training starts, is extremely inefficient. To overcome this restriction, online triplet mining is used. The idea is to select triplets during training epochs within a batch of samples [38; 40], so that for each anchor-positive pair within a batch, other in-batch samples are considered as negatives. Online triplet mining is also known as in-batch negative or batch-wise [41].  
NT-Xent loss, which stands for Normalized Temperature-Scaled Cross-Entropy loss, is the most popular batch-wise contrastive loss, and is proposed in the SimCLR paper by [PERSON]. [42]. It is an extended version of the multi-class N-pair loss [43]. This loss function takes positive pairs in the form of \((x_{i},x_{i}^{+})\) and other possible pairs within the batch considered as negatives. The formula for NT-Xent loss is defined as follows:  
\[L_{NT-Xent}=-\log\frac{e^{sim(x_{i}x_{i}^{+})/\tau}}{\sum_{j=1}^{N}e^{sim(x_{ i}x_{j}^{+})/\tau}} \tag{3}\]  
where \(sim(\cdot)\) is the standard cosine similarity, and \(\tau\) is a temperature parameter to scale the cosine similarity.