# TCCCD: Triplet-Based Cross-Language Code Clone Detection
## 3 Background
### Triplet Learning  
Triplet learning is a fundamental concept in metric learning, first introduced and applied in the field of image recognition by researchers [46] at Google in 2015. Triplet learning utilizes triplets composed of an anchor sample \(X_{i}^{n}\), a positive sample \(X_{i}^{p}\) (similar to the anchor), and a negative sample \(X_{i}^{n}\) (dissimilar to the anchor) to train the model.  
The distance function between two vector, \(X_{1},X_{2}\), is defined as \(D(X_{1},X_{2})\). The triplet loss function is defined as Equation (1):  
\[L=\sum_{i=1}^{N}[D(X_{i}^{a},X_{i}^{p})-D(X_{i}^{a},X_{i}^{n})+\alpha]_{+}. \tag{1}\]  
where \(N\) is cardinality of train set. \(\alpha\) is a margin that is enforced between positive and negative pairs.  
By optimizing this loss function, a discriminative embedding representation is learned by the model, where similar samples are brought closer together and dissimilar samples are pushed farther apart in the embedding space. This leads to improved similarity measurement and sample classification performance.  
Researchers have proposed various improved triplet loss functions to better capture the relative relationships between features. These improvements include cosine-margin-triplet loss [23], weighted triplet loss [47; 48], adaptive triplet loss [49], and others [49; 50; 51]. Among them, the cosine-margin-triplet loss, based on cosine similarity, restricts the embedding vectors of samples within a specific cosine boundary. This helps enhance the similarity between positive samples while ensuring that the embedding vectors of negative samples are located outside the cosine boundary. This approach can strengthen the discriminative power of embedding vectors. The goal of these methods is to increase the separation of samples in the embedding space, thereby enhancing the model's effectiveness. Weighted triplet loss takes into account the importance of different samples for model training by introducing sample weights for optimization. Adaptive triplet loss allows the model to adaptively adjust the loss function based on the current training state and sample distribution.  
In triplet learning, the construction of appropriate negative samples plays a crucial role in effectively training the model. Researchers have proposed various approaches and strategies to address this key issue, such as soft triple loss [52] and distance-weighted sampling loss [22].  
Due to the effectiveness of triplet learning in capturing the relative relationships between features and learning discriminative embedding representations, it is combined with other loss functions to enhance learning effectiveness. Specifically, the distance-weighted sampling loss and triplet loss are integrated, and the cosine-margin-triplet loss [23] is employed as an improved approach to enhance triplet learning.  
The triplet learning approach we employed can be summarized in the following steps:  
1. Using an encoder \(f(.)\) to transform code fragments into high-dimensional vectors.
2. Selecting an appropriate sampling method called \(S(.)\) for triplet learning; some research has been effective, such as [22]. Treating each vector in the batch as an anchor X, according to sampling method \(S(.)\) to chose positive samples and negative samples.
3. Putting the triplets into the cosine-margin-triplet loss function.  
Recent research on triplet loss has shown state-of-the-art results in the fields of image recognition and text classification [23; 51; 53].