# TCCCD: Triplet-Based Cross-Language Code Clone Detection
## 4 Proposed Approach
### Triplet Learning  
We propose an effective triplet-learning method that combines distance-weighted sampling [22] in triplet learning with the cosine-margin-triplet loss [23]. Our objective was to select the most-suitable negative samples for each anchor sample and, then, feed the selected triplets into the cosine-margin-triplet loss function. The selection probability of negative samples is determined by Equation (1), which utilizes the softmax function to convert the distance between negative samples and anchor samples into a probability distribution. The calculation of the selection probability was based on the Euclidean distance between samples, where larger distances correspond to higher probabilities. Our probability of negative samples selected can be expressed as Equation (2):  
\[P_{negative}(i)=\frac{E(D_{pos}(X_{i}^{a},X_{i}^{n}))}{\sum_{j=1}^{r}E(D_{pos}( X_{i}^{a},X_{j}^{n}))}. \tag{2}\]  
In this equation, \(P_{negative}\) is the probability of negative samples selected and \(i\) is the number of the currently selected samples. \(r\) is the total count of all negative samples in a batch. \(X^{a}\) and \(X^{n}\) are anchor samples and negative samples, respectively (i.e., \(X^{a}\) and \(X^{n}\) form a non-clone pair). The \(D_{pos}\) function represents the square of the Euclidean distance between samples. The E function is the softmax function. The distance metric function \(D_{pos}\) used to measure the square of the Euclidean distance between two samples (\(x\) and \(y\) represent two samples) is defined as Equation (3):  
\[D_{pos}(x,y)=\|\mathbf{x}-\mathbf{y}\|^{2}. \tag{3}\]  
The triplet loss function is applied after selecting the negative samples based on the probabilities and finding the positive samples from the same task for each anchor sample. The cosine-margin-triplet loss function calculates the cosine similarity between embedding vectors and introduces a margin parameter (\(m\)) and a scaling factor (\(s\)) to penalize positive and negative samples that do not meet the desired similarity boundary. This encourages the embedding vectors to cluster and separate samples more effectively in the embedding space. The cosine-margin-triplet loss function is defined as Equation (4):  
\[Loss=-\frac{1}{N}\sum_{i}^{N}log\frac{E(s(cos(X_{i}^{a},X_{i}^{p})-m))}{E(s( cos(X_{i}^{a},X_{i}^{n})))+E(s(cos(X_{i}^{a},X_{i}^{p})-m))}. \tag{4}\]  
In Equation (4), N represents the number of all triples. \(m\) represents the minimum angular distance between positive and negative samples. \(s\) is the scaling factor used to adjust the scaling of the exponential term. \(X^{p}\) is the positive sample (i.e., \(X^{a}\) and \(X^{p}\) form a clone pair). The following Algorithm 1 provides a detailed description of the process for selecting negative samples and constructing the loss function. ```
input :A batch of clone code pairs, \(B\) output :Triplet loss, \(L_{T}\)
1 for each pair \(E_{i}\) in \(B\)do
2\(X_{i}^{a}\), \(X_{i}^{p}\leftarrow E_{i}\);
3\(S_{cosp}=E(s(cos(X_{i}^{a},X_{i}^{p})-m))\) ;
4 for each pair \(E_{i}(i\
eq j)\) in \(B\)do
5\(X_{i}^{n_{1}}\), \(X_{i}^{n_{2}}\leftarrow E_{i}\);
6\(D_{sum}=D_{sum}+E(D_{pos}(X_{i}^{a},X_{j}^{n_{1}}))+E(D_{pos_{j}}(X_{i}^{a},X _{j}^{n_{2}}))\);
7\(Set_{ncg}\gets X_{j}^{n_{1}}\), \(X_{j}^{n_{2}}\);
8
9 for each negative sample \(e\)do
10\(P_{ncg}(i)=\frac{E(D(X_{i}^{a},X_{i}^{n}))}{D_{sum}}\) ;
11\(X_{i}^{n}=S(p_{ncg})\) ;
12\(S_{cosp}=E(s(cos(X_{i}^{a},X_{i}^{n})))\) ;
13\(L_{T_{i}}=-log(\frac{S_{cosp}}{S_{cosp}+S_{cosp}})\);
14\(L_{T}=\frac{\sum_{i=1}^{int(B)}L_{T_{i}}}{len(B)}\)
```  
**Algorithm 1**Calculation of triplet loss. For each processed batch, B denotes the number of token sequence instances converted from cloned code within the batch. The traversal of this batch is conducted iteratively for processing purposes. In each pair, the initial instance is designated as the anchor sample, while the subsequent instance assumes the role of the positive sample.