# SJBCD: A Java Code Clone Detection Method Based on
## 3 Methodology
### Building GRU-Based Siamese Neural Network
#### 3.2.2 Building Siamese Neural Network  
Deep learning technology is increasingly used in the field of code clone detection, and the Siamese neural network technology [23] has been frequently used lately, achieving a good detection effect [18; 26; 31]. A Siamese neural network is a neural network structure that contains two identical subnetworks that share weights, which has the advantage of being able to handle pairs of input data and having fewer weight parameters. Gated Recurrent Units (GRU), as one of the representatives of RNNs, not only inherits the advantages of RNNs in processing sequence information but also adds the ability to learn long-term dependencies. Compared with a long short-term memory model (LSTM), GRU has fewer training parameters and is faster to train. We compared the two in our model and found that the detection effect is not much different, but GRU converges faster. Therefore, we built a Siamese neural network model based on GRU. The model structure is shown in Figure 5.  
Firstly, we input paired opcode sequences to the model, and opcode sequences were converted into vector sequences of length 200 \(\times\) 300 by the word vector pretrained model. Secondly, the model fed the sequences of vectors into the GRU layer, which converted them into 175 \(\times\) 1 vectors. Finally, the model fed vectors into subsequent comparison networks. In the comparison network, a variety of distance calculation methods can be used, such as cosine distance, Euclidean distance, etc. We can also use the \"concatenate\" method commonly used in deep learning techniques to concatenate the paired vectors output by the GRU layer. We designed two model variants, one that spliced the output of the GRU module called SJBCD, and the other that calculated the cosine distance of the output called SJBCD-cos. The cosine distance is as follows:  
\[\cos(\theta)=\frac{X_{1}\cdot X_{2}}{\parallel X_{1}\parallel\parallel X_{2}\parallel} \tag{2}\]  
\[\frac{X_{1}\cdot X_{2}}{\parallel X_{1}\parallel\parallel X_{2}\parallel}= \frac{\sum_{i=1}^{n}X_{1}^{i}\cdot X_{2}^{i}}{\sqrt{\sum_{i=1}^{n}{{{\left({X_ {1}^{i}}\right)}^{2}}}\sqrt{\sum_{i=1}^{n}{{{\left({X_{2}^{i}}\right)}^{2}}}}}} \tag{3}\]  
Figure 4: Extraction process of opcode word vector corpus: (**a**) Corpus extraction process; (**b**) Bytecode sequence results.  
where \(X_{1}\) and \(X_{2}\) are vectors output by GRU. \(X_{1}^{i}\) and \(X_{2}^{i}\) are the elements of vectors.  
To improve training speed and prevent overfitting, we added the BN layer and Dropout layer to the comparison network. These techniques are common in deep learning and help achieve better model detection results.  
We put the opcode sequence pair into the trained model, obtained a probability in the range of [0, 1], and judged whether the opcode sequence pair was a clone pair based on a threshold.  
Binary cross entropy is a loss function commonly in classification problems. Our model is essentially a binary classifier, so binary cross entropy is used as the loss function. The calculation formula is as follows:  
\[\text{loss}=y_{i}\cdot\text{log}\hat{y}_{i}+(1-y_{i})\cdot\text{log}(1-\hat{y }_{i}) \tag{4}\]  
where \(y_{i}\) is the actual value and \(\hat{y}_{i}\) is the predicted value.  
In general, the SJBCD method can be divided into two steps. Firstly, the SJBCD method obtained opcode sequences of functions through multiple translation and text extraction methods and used word embedding technology to obtain the semantic information of opcodes. Secondly, a Siamese neural network based on GRU was used to predict whether opcode sequence pairs were clones.