A Deep Learning-Based Code Similarity Detection Method
By using the AST-GCN model and a multi-scale semantic feature adaptive fusion model, the problem that existing code similarity detection methods cannot fully parse semantic features is solved, achieving more accurate and efficient code similarity detection.
Patent Information
- Application Number
- CN202510393968.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-31
- Publication Date
- 2025-10-31
- Estimated Expiration
- 2045-03-31
AI Technical Summary
Existing code similarity detection methods struggle to fully analyze the semantic features of code, leading to misjudgments when faced with similar grammatical structures but different semantics. Furthermore, graph convolutional models cannot fully learn the grammatical features of code by aggregating features from tree-like structures, thus affecting detection performance.
A deep learning-based approach is adopted, which extracts syntactic features through the AST-GCN model and enhances the features using a multi-head self-attention layer. Semantic features are extracted by combining the multi-scale semantic feature adaptive fusion model MSF. Finally, the syntactic and semantic features are adaptively fused through a gating mechanism, and similarity is calculated using a three-layer perceptron.
It improves the accuracy and efficiency of code similarity detection, better captures the syntactic and semantic features of code, reduces false positives, and enhances detection results.
Smart Images

Figure CN120336871B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of code similarity detection technology, specifically a code similarity detection method based on deep learning. Background Technology
[0002] In software development, code similarity detection serves two main purposes: firstly, it can detect code duplication and plagiarism, improving software development quality and efficiency, and promoting code innovation and progress; secondly, it helps locate similar code snippets, facilitating code reuse and integration, enhancing maintainability and reusability, and promoting code sharing. Currently, commonly used code similarity detection methods include the following categories:
[0003] (1) Code similarity detection methods based on code text comparison: These methods use character sequence matching algorithms to compare the character sequences of code text to calculate code similarity. Common character sequence matching algorithms include the Longest Common Subsequence (LCS) algorithm and the Key-Minute Pattern (KMP) algorithm. These methods are simple to implement, relatively fast in computation, and effective for code with simple structure and small differences in variable names and format.
[0004] (2) Syntax Tree-Based Code Similarity Detection Methods: These methods use compiler front-end tools to convert code into a syntax tree. A syntax tree represents the syntactic structure of code in a tree-like structure, where nodes represent syntactic elements (such as expressions, statements, declarations, etc.) and edges represent syntactic relationships. These methods compare the syntax trees of two pieces of code and quantify the syntactic similarity by measuring the degree of similarity between the syntax trees. Tree comparison algorithms can be used to compare and calculate the similarity between two syntax trees. Common tree comparison algorithms include the tree edit distance algorithm and the subtree matching algorithm. The tree edit distance algorithm calculates the minimum number of edit operations required to convert one tree into another, while the subtree matching algorithm finds identical subtree structures in the two trees.
[0005] (3) Graph-based code similarity detection methods: The basic idea of this type of method is similar to that of syntax tree-based code similarity detection methods. By analyzing the syntactic structure of the code, as well as function call relationships, control dependencies, data flow, etc., a dependency graph of the program is constructed. Nodes in the graph are matched, and the similarity of the dependency graph can be calculated through graph comparison algorithms or graph convolutional neural networks, thereby performing code similarity detection. In this detection approach, the nodes and connections of the graph reflect the syntax of the code, while the data flow can be regarded as an abstract expression of the semantics of the code. Therefore, it has a good similarity detection capability and may be able to detect similar code with different text structures but similar functions.
[0006] However, current code similarity detection faces several challenges. Methods based on textual code comparison are highly sensitive to formatting changes (such as indentation and spaces) and variable name modifications, making it difficult to handle semantically identical but lexically different cases. Syntax tree-based methods, when parsing code syntax, cannot accurately learn the relationships between nodes, thus failing to provide results that accurately reflect the code's syntactic features. Furthermore, these methods are not comprehensive enough in extracting semantics, leading to misjudgments when faced with similar syntactic structures but different semantics. While constructing a Program Dependency Graph (PDG) can reflect semantic features to some extent, it fails to fully encompass syntactic features, and constructing a PPG is costly and inefficient.
[0007] Based on the above, the current method has the following drawbacks:
[0008] (1) Code similarity detection methods based on code text or code similarity detection methods based on syntax trees cannot fully parse the semantic features of code. These methods mainly focus on the syntactic form of code, but do not fully consider the actual semantics of code.
[0009] (2) When using the graph convolution model to traverse and update the node features of the syntax tree, the graph convolution model cannot fully learn the syntax features of the code by aggregating the features of the tree structure, thus reducing the effect of similarity detection.
[0010] (3) Traditional methods for fusing syntactic and semantic features usually involve fixed-value weighting or direct concatenation, which cannot be tailored to the characteristics of different codes. This leads to the weakening of important features and the overemphasis of unimportant features, thus affecting the similarity detection performance.
[0011] Therefore, a deep learning-based code similarity detection method was invented. Summary of the Invention
[0012] To address the aforementioned technical problems, according to one aspect of the present invention, the present invention provides the following technical solution:
[0013] A deep learning-based code similarity detection method includes the following specific steps:
[0014] S1: Collect code, preprocess the code, including deleting relevant comments, standardizing variable and function names, unifying code format, and combining the processed code into any pairwise combinations and code similarity rating to obtain a triple <code A, code B, similarity y>. The set of all triples constitutes a code dataset, and code A or code B is designated as code C.
[0015] S2: Use a code extraction tool to parse code C and generate a syntax tree. Traverse the syntax tree to obtain the adjacency matrix of code C. and depth weight matrix d' C Then, Word2Vec is used to convert the code C into a feature vector of a specified dimension, resulting in the feature matrix Z. C ;
[0016] S3: Will and Z C Inputting the AST-GCN model, AST-GCN performs syntactic feature aggregation on the syntax tree of code C to obtain the syntax feature matrix. Finally, through the multi-head self-attention layer... Feature enhancement is performed to obtain the syntactic feature vector V of code C. gC ;
[0017] S4: Perform word segmentation on code C. Use a word segmentation tool to split code C into several lexical units, resulting in a lexical unit sequence X. C ;
[0018] S5: Construct a multi-scale semantic feature adaptive fusion model (MSF) for multi-scale fusion and extraction of code semantic features, and integrate X... C After being input into the MSF model, the feature vector V representing the semantics of the code is obtained. sC ;
[0019] S6: Adaptively fuse code C syntax feature vectors V using gating mechanisms gC and semantic feature vector V sC The fusion vector V is obtained. fusC ;
[0020] S7: V fusA and V fusB By concatenating the vectors, we obtain the vector to be measured, V. t V t The input is fed into a three-layer perceptron for similarity comparison, and the output is a similarity score. calculate The loss value between the actual value and the true value is used to update the learnable syntax matrix W in reverse. gC With the learnable semantic matrix W sC ;
[0021] S8: Apply the trained code similarity detection model to the code similarity detection task, calculate the similarity of any code pair <code X, code Y> in the actual input, and obtain the similarity score of the code pair;
[0022] The specific steps of S3 are as follows:
[0023] S31: Construct an AST-GCN model to extract the syntactic feature matrix of code C. Its AST-GCN model consists of an input layer, a feature update layer, and an output layer. The AST-GCN model fuses the features of each node in the syntax tree with the features of its neighboring nodes and sibling nodes to obtain a feature sequence containing local syntax features of the code. Finally, the syntax feature matrix is obtained through linear transformation.
[0024] S32: Utilizing multi-head self-attention mechanism to... Attention weights are calculated, the syntactic feature sequences output by each attention head are concatenated, and then a linear transformation is performed to obtain the syntactic feature vector V. gC .
[0025] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S31 are as follows:
[0026] S311: Traverse the syntax tree of code C level by level to obtain the sibling matrix. and depth weight matrix d' C ;
[0027] S312: Will d' C , and Z C The input feature update layer consists of L convolutional layers. Let the syntax tree be updated at the (l+1)th layer (l∈(0,1,…,L-1)) of the feature update layer. Then the feature matrix output by that layer... It consists of the sum of three features, namely the sibling feature matrix. Hierarchical Neighbor Feature Matrix and the feature matrix output from the layer above the feature update layer
[0028] S313: Will as well as Adding them together yields the updated feature matrix after the (l+1)th feature update layer.
[0029]
[0030] Where ReLU is the activation function;
[0031] S314: After the final layer calculation of the feature update layer, the feature matrix is output.
[0032]
[0033] in i∈(1,2,…,n) is Z C Feature sequences of each node The updated feature sequence output from the final layer of the feature update layer is used for... Perform a linear transformation to obtain the syntax feature matrix.
[0034] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S311 are as follows:
[0035] S3111: Traverse the syntax tree level by level. For any node r encountered during the traversal, determine the sibling nodes of r based on the adjacency relationships between nodes, thereby obtaining the sibling matrix. exist The syntax tree stores all sibling relationships between nodes; that is, if node p and node q are siblings, then x... p,q , x p,q =x q,p =1; otherwise x p,q =x q,p =0;
[0036] S3112: When traversing the syntax tree, record the depth of each node in the syntax tree in the depth matrix d. C In the middle, d C It is a diagonal matrix, where the values on the diagonal are the depths of each node; the weighting coefficients during feature aggregation are adjusted according to a pre-set maximum critical depth m, and the weights for d are... C For any node i with a depth greater than m, the value of i is replaced with Where α is the scaling factor, d i This represents the depth of node i; for any node j with a depth less than or equal to m, the value of j is replaced with 1. After adjusting the weight coefficients as described above, the final depth weight matrix d' is obtained. C ;
[0037] The specific steps of S312 are as follows:
[0038] S3121: Calculate the sibling characteristic matrix use and Perform feature aggregation operation to obtain
[0039]
[0040] in W is the inverse of the degree matrix. s It is a weight matrix;
[0041] S3122: For nodes deeper in the syntax tree, in order to reduce the impact of the features of these nodes on the overall code similarity, ... Left-multiply depth weight matrix d' C Then, feature aggregation is performed to obtain the hierarchical neighbor feature matrix.
[0042]
[0043] Among them W n It is a weight matrix.
[0044] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S5 are as follows:
[0045] S51: First, X C The semantic sequence output by each self-attention head of CodeBERT is concatted to obtain a preliminary feature vector representing the semantics of the code. This feature vector is then linearly transformed to obtain the semantic vector v of code C. C ;
[0046] S52: For v C Perform downsampling to obtain a semantically coarse-grained sequence. Then v C and The input is fed into a bidirectional simple recurrent unit F-BiRSU with an adaptive forgetting factor to obtain the semantic feature sequence H of code C. C ;
[0047] S53: Utilizing a self-attention layer to apply H C Further semantic enhancement can be performed to make H C It can incorporate information from other positions throughout the sequence, better capture the global semantic information of the code, and ultimately obtain the semantic feature vector V of code C. sC .
[0048] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S52 are as follows:
[0049] S521: Employ max pooling on v C Perform downsampling to obtain a semantic coarse-grained order. For any The calculation formula is as follows:
[0050]
[0051] in, It is the semantic vector of code C, where k is the size of the max pooling window and n is an integer multiple of k;
[0052] S522: Construct the F-BiSRU module for use from v C and Extract semantic feature sequence H C Its F-BiSRU module consists of a forward FSRU network and a reverse FSRU network, each network containing several FSRU units;
[0053] S523: Will and Perform a concat operation to obtain The semantic feature sequence H is obtained through a linear transformation. C ,Right now:
[0054]
[0055] in
[0056] The specific steps of S522 are as follows:
[0057] S5221: First, define the gate vector I. C Used for control and v C The fusion ratio of the input time series at each time step, I C The calculation formula is as follows:
[0058]
[0059] Where Sigmoid is the activation function, W t It is a weight matrix. It is a bias vector. Indicates that v C and Time series at time t and Then splice them together, and then and According to I C Multi-scale information fusion is performed to obtain
[0060]
[0061] Where ⊙ represents element-wise multiplication;
[0062] S5222: Forget gate in calculating the forward FSRU network at time t When outputting, the hidden state from the previous time step is introduced. Will and splicing as an update The parameters are calculated using the following formula:
[0063]
[0064] Among them W f It is the weight matrix, b f It is a bias vector. This represents the hidden state at a given moment on a positive FSRU network. Based on The statistical property function of the variance, where θ is a coefficient used for scaling. The magnitude of the value;
[0065] Reset gate r at time t in a forward FSRU network t Memory cells c t And the positive hidden state output at time t The calculation process is as follows:
[0066]
[0067]
[0068]
[0069] The reverse hidden state output by the reverse FSRU network at time t The calculation steps are the same as those for the forward hidden state output at time t in a forward FSRU network, but the calculation steps differ in the forget gate. The hidden state of the reverse FSRU network at that moment is introduced.
[0070]
[0071] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S6 are as follows:
[0072] S61: Design a learnable grammar matrix W gC and bias value b gC The linear layer forms the syntax gating vector g of code C. gC :
[0073] g gC =ReLU(W gC V gC +b gC )
[0074] Similarly, design a learnable semantic matrix W. sC and bias value b sC The linear layer forms the semantic gating vector g of code C. sC :
[0075] g sC =ReLU(W sC V sC +b sC )
[0076] Calculate the fusion weight α of the syntactic feature vectors of code C. C The fusion weight β of the semantic feature vector C :
[0077]
[0078] β C =1-α C
[0079] S62: Utilizing α C β C For V gC V sC The weighted summation yields the final fusion vector V from code C. fusC :
[0080] V fusC =α C V gC +β C V sC ;
[0081] Since code A or code B is set to code C in S1, therefore V fusC In fact, it also represents the fusion vector V of code A. fusA The fusion vector V of code B fusB That is, after S62, V has been obtained. fusA and V fusB .
[0082] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S7 are as follows:
[0083] S71: Merge vector V fusA V fusB By concatenating the vectors, we obtain the vector to be measured, V. t :
[0084]
[0085] in Indicates a splicing operation;
[0086] S72: V t The input is a three-layer perceptron for training, and the output is a value between 0 and 1 as the similarity score between code A and code B.
[0087] Z1 = ReLU(W1V) t +b1)
[0088] Z2 = ReLU(W2Z1 + b2)
[0089] Z3 = ReLU(W3Z2 + b3)
[0090]
[0091] Where W1, W2, and W3 are weight matrices, b1, b2, and b3 are bias vectors, and Z1, Z2, and Z3 are the feature representations of the first, second, and third layers of the three-layer perceptron, respectively.
[0092] S73: Calculation The loss value between the actual similar value and the true similar value is calculated using the following formula:
[0093]
[0094] Where y i This is the true similarity value between code A and code B in this training. The closer the value is to 1, the more similar code A and code B are.
[0095] S74: Utilizing the loss value to analyze the learnable syntax matrix W gC and the learnable semantic matrix W sC Perform a reverse update.
[0096] As a preferred embodiment of the deep learning-based code similarity detection method described in this invention, the specific steps of S74 are as follows:
[0097] S741: Against W gC The update process is as follows:
[0098]
[0099] S742: Against W sC The update process is as follows:
[0100]
[0101] in η1 and η2 represent the partial derivatives, and η1 and η2 are the learning rates.
[0102] S743: By updating W gC and W sC This enables the gating mechanism to adaptively fuse the syntactic and semantic feature vectors of the code, thereby improving the accuracy of similarity score calculation.
[0103] Compared with existing technologies:
[0104] This invention evaluates the overall similarity of code by calculating the similarity of its syntax and semantic features. In extracting syntax feature vectors, when aggregating features of each node in the syntax tree, the depth of the node and the features of its sibling nodes are considered, avoiding feature loss when aggregating tree structures using conventional methods. In extracting semantic feature vectors, an adaptive forgetting factor is introduced, which can retain the most critical semantic information of the code and improve computational efficiency when extracting semantic feature vectors. Attached Figure Description
[0105] Figure 1 This is a schematic diagram of the process of the present invention;
[0106] Figure 2 This is a schematic diagram of the Python code for this invention;
[0107] Figure 3 The syntax tree T of this invention c Schematic diagram;
[0108] Figure 4 R is the adjacency matrix of this invention. C (Left) and Adjacency Matrix (Right) Schematic diagram;
[0109] Figure 5 This is a diagram of the AST-GCN model architecture of the present invention;
[0110] Figure 6 The sibling matrix of this invention Schematic diagram;
[0111] Figure 7 The depth matrix d of this invention C (Left) and depth weight matrix d' C (Right) Schematic diagram;
[0112] Figure 8 This is a schematic diagram of the multi-scale semantic feature adaptive fusion model of the present invention;
[0113] Figure 9 This is a structural diagram of the F-BiSRU module of the present invention;
[0114] Figure 10 This is a structural diagram of the FSRU unit of the present invention. Detailed Implementation
[0115] To make the objectives, technical solutions, and advantages of the present invention clearer, the embodiments of the present invention will be described in further detail below with reference to the accompanying drawings.
[0116] This invention provides a deep learning-based code similarity detection method. Please refer to [link / reference]. Figures 1-10 The specific steps are as follows:
[0117] S1: Collect code and preprocess it. This includes deleting relevant comments, standardizing variable and function names, and unifying code format. Then, combine the processed code into any pairwise combinations and rate the code similarity to obtain a triple <code A, code B, similarity y>. The set of all triples constitutes a code dataset. Specifically, since code A and code B in each triple need to be processed exactly the same way, unless otherwise specified, the following steps apply to code A and code B. For ease of description, code C is used to uniformly represent code A and code B in the following steps, that is, code A or code B is set as code C.
[0118] S2: Use a code extraction tool to parse code C and generate a syntax tree. Traverse the syntax tree to obtain the adjacency matrix of code C. and depth weight matrix d' C Then, Word2Vec is used to convert the code C into a feature vector of a specified dimension, resulting in the feature matrix Z. C ;
[0119] S2 includes, but is not limited to, the following embodiments:
[0120] The syntax tree of the code is extracted using code extraction tools such as Joern or Antlr. These tools first perform lexical analysis, breaking down the code into basic word units. According to the order in which the words appear in the code, the identified lexical units are assembled into a lexical unit stream. Then, the syntax analysis of the lexical unit stream is performed to gradually build the syntax tree. The syntax tree is presented in a multi-branch tree structure, with each node of the tree corresponding to a different syntax element.
[0121] Here is a Python code for calculating a product: Figure 2 As shown;
[0122] The above code is parsed and a syntax tree T is constructed. c And use a preorder traversal method to traverse T c The process involves traversing the tree, numbering each node starting from 1, to obtain the syntax tree T. c like Figure 3 As shown;
[0123] right Figure 3 T inc Perform a preorder traversal. When a node and its child nodes are visited, record the edges between the node and its child nodes. Specifically, form a tuple with the parent node number and the child node number to represent the edge, and then add the tuple to an edge list. For example, when traversing to the FunctionDeclarction node and the Param:[x,y] node, add the tuple <2,3> to the edge list. Since the syntax tree is an undirected graph, <3,2> also needs to be added to the edge list.
[0124] Using Python libraries such as NumPy, the adjacency matrix R is generated directly from the edge list. C Since each node must retain its own features when aggregating neighbor features based on the adjacency matrix, each node must also have an edge connected to itself; that is, in R... C In this process, each node's position is changed to 1, resulting in an adjacency matrix. like Figure 4 As shown;
[0125] Graph convolutional neural networks can only process points and edges in vector format. Word2Vec can convert points and edges in a syntax tree into vector representations. For example, for the node FunctionDeclaration (node 1), Word2Vec will output a 32-dimensional sequence [1.2, 4.0, ..., 1.5] representing the features of node 1. The feature sequences of all nodes are arranged in order of node number to form a matrix, where each row corresponds to the feature sequence of a node, and the number of columns is the dimension of the feature vector; thus, the feature matrix Z is obtained. C :
[0126]
[0127] in i∈(1,2,…,15) represents the feature sequence of node i;
[0128] Word2Vec is a method in the Python toolkit Gensim. Its purpose is to learn distributed representations of words through a neural network model, mapping words to a low-dimensional vector space. In this vector space, semantically similar words are close to each other, thus better capturing the semantic and grammatical relationships between words.
[0129] Note: NumPy (Numerical Python) is a fundamental library for scientific computing in the Python language. It provides Python with efficient and flexible multidimensional array processing capabilities, simplifying the data processing flow. The array() method provided by NumPy can convert the information of the edge list into an adjacency matrix.
[0130] S3: Will and Z C Inputting the AST-GCN model, AST-GCN performs syntactic feature aggregation on the syntax tree of code C to obtain the syntax feature matrix. Finally, through the multi-head self-attention layer... Feature enhancement is performed to obtain the syntactic feature vector V of code C. gC The structure of the AST-GCN model is as follows: Figure 5 As shown;
[0131] The specific steps of S3 are as follows:
[0132] S31: Construct an AST-GCN model to extract the syntactic feature matrix of code C. Its AST-GCN model consists of an input layer, a feature update layer, and an output layer. The AST-GCN model fuses the features of each node in the syntax tree with the features of its neighboring nodes and sibling nodes to obtain a feature sequence containing local syntax features of the code. Finally, the syntax feature matrix is obtained through linear transformation.
[0133] Among them, Graph Convolutional Network (GCN) is a deep learning method specifically designed for processing graph-structured data. The core idea of GCN is to define convolution operations on the graph structure, thereby learning the feature representations of nodes in the graph. It extends convolution operations from traditional grid data (such as the pixel grid of an image or the sequence of text) to irregular graph structures, enabling the use of neighborhood information of nodes to update node features. GCN often consists of multiple convolutional layers. When updating the features of a node, each layer aggregates the features of that node and outputs the updated feature sequence as the input of the next layer.
[0134] The specific steps in S31 are as follows:
[0135] S311: Traverse the syntax tree of code C level by level to obtain the sibling matrix. and depth weight matrix d' C ;
[0136] The specific steps of S311 are as follows:
[0137] S3111: Traverse the syntax tree level by level. For any node r encountered during the traversal, determine the sibling nodes of r based on the adjacency relationships between nodes, thereby obtaining the sibling matrix. exist The syntax tree stores all sibling relationships between nodes; that is, if node p and node q are siblings, then x... p,q , x p,q =x q,p =1; otherwise x p,q =x q,p =0;
[0138] Among them, for Figure 3 T in c Perform a level-order traversal, starting from node 1, using a queue to assist in the traversal; add node 1 to the queue; traverse all its child nodes; for these child nodes, they are siblings, and these nodes are marked as sibling nodes, for example, nodes 2 and 11 are siblings, then x 2,11 , x 2,11 =x 11,2 =1, and add nodes 2 and 11 to the queue so that the child nodes can be traversed later to obtain the sibling matrix. like Figure 6 As shown;
[0139] S3112: When traversing the syntax tree, record the depth of each node in the syntax tree in the depth matrix d. C In the process, the weighting coefficients during feature aggregation are adjusted according to the pre-set maximum critical depth m, and the weighting coefficients for d are adjusted accordingly. C For any node i with a depth greater than m, the value of i is replaced with Where α is the scaling factor, d i This represents the depth of node i; for any node j with a depth less than or equal to m, the value of j is replaced with 1. After adjusting the weight coefficients as described above, the final depth weight matrix d' is obtained. C ;
[0140] Among them, for Figure 3 China T c Perform a level-order traversal, using the depth matrix d during the traversal. C Record the depth of each node in the syntax tree; d C It is a diagonal matrix, where the values on the diagonal represent the depth of the corresponding node; in this embodiment, the critical maximum depth m is set to 2, then for d C Replace values greater than 2 with Here, α is set to 1; for values with a depth less than or equal to 2, they are replaced with 1; thus, the depth weight matrix d' is obtained. C ,like Figure 7 As shown;
[0141] S312: Will '
[0142] dC , and Z C The input feature update layer consists of L convolutional layers. Let the syntax tree be updated at the (l+1)th layer (l∈(0,1,…,L-1)) of the feature update layer. Then the feature matrix output by that layer... It consists of the sum of three features, namely the sibling feature matrix. Hierarchical Neighbor Feature Matrix and the feature matrix output from the layer above the feature update layer
[0143] The specific steps in S312 are as follows:
[0144] S3121: Calculate the sibling characteristic matrix use and Perform feature aggregation operation to obtain
[0145]
[0146] in W is the inverse of the degree matrix. s It is a weight matrix;
[0147] S3122: For nodes deeper in the syntax tree, in order to reduce the impact of the features of these nodes on the overall code similarity, ... Left-multiply depth weight matrix d' C Then, feature aggregation is performed to obtain the hierarchical neighbor feature matrix.
[0148]
[0149] Among them W n It is a weight matrix;
[0150] S313: Will as well as Adding them together yields the updated feature matrix after the (l+1)th feature update layer.
[0151]
[0152] Where ReLU is the activation function;
[0153] S313 includes, but is not limited to, the following embodiments:
[0154] Figure 3 T c The feature update process in the first layer of the feature update layer:
[0155] 1. Calculate the sibling characteristic matrix
[0156]
[0157] 2. Calculate the hierarchical neighbor feature matrix
[0158] Assuming the maximum critical depth m is 2 and the scaling factor α is 1, obtain the depth weight matrix d'. C ,calculate
[0159]
[0160] 3. Calculate the feature sequence output from the first layer of the feature update layer.
[0161]
[0162] 4. At this point, Z C After the first feature update layer, it is updated to... Will The features are then updated using the input to the second layer, and the above operation is repeated until the final layer is reached.
[0163] S314: After the final layer calculation of the feature update layer, the feature matrix is output.
[0164]
[0165] in i∈(1,2,…,n) is Z C Feature sequences of each node The updated feature sequence output from the final layer of the feature update layer is used for... Perform a linear transformation to obtain the syntax feature matrix.
[0166] S32: Utilizing multi-head self-attention mechanism to... Attention weights are calculated, the syntactic feature sequences output by each attention head are concatenated, and then a linear transformation is performed to obtain the syntactic feature vector V. gC ;
[0167] The self-attention mechanism is a deep learning mechanism. For an input sequence, it is transformed into a query vector, a key vector, and a value vector using a learnable weight matrix. Then, the dot product of the query vector and the key vector is calculated and scaled to obtain an attention score. After Softmax normalization, the attention weights are obtained. Finally, the value vectors corresponding to the attention weights are weighted and summed to obtain the output. This can capture the dependencies between elements in the sequence and is not limited by distance. The multi-head self-attention mechanism repeats the above self-attention process multiple times, using a different weight matrix each time. Each self-attention head outputs a sequence. These sequences are then concatenated and mapped back to the original input dimension through a linear transformation.
[0168] Linear transformations change the dimensions of vectors in a vector space. These transformations do not change the linear relationships between vectors and maintain the basic structure and properties of the vector space as a whole. Typically, linear transformations can be achieved by right-multiplying by a matrix of a specific dimension to match the dimensions of the model. Unless otherwise specified in the following steps, the purpose of linear transformation operations is to match the vector dimensions with the input dimensions of the model.
[0169] S4: Perform word segmentation on code C. Use a word segmentation tool to split code C into several lexical units, resulting in a lexical unit sequence X. C ;
[0170] The tool typically used for code segmentation is the Tokenizer, which can divide code into multiple lexical units (called tokens); it usually operates based on predefined rules or learned patterns.
[0171] S5: Construct a multi-scale semantic feature adaptive fusion model (MSF) for multi-scale fusion and extraction of code semantic features, and integrate X... C After being input into the MSF model, the feature vector V representing the semantics of the code is obtained. sC The architecture of the multi-scale semantic adaptive fusion model is as follows: Figure 8 As shown;
[0172] The specific steps of S5 are as follows:
[0173] S51: First, X C The semantic sequence output by each self-attention head of CodeBERT is concatted to obtain a preliminary feature vector representing the semantics of the code. This feature vector is then linearly transformed to obtain the semantic vector v of code C. C ;
[0174] CodeBERT is a pre-trained model based on the Transformer encoder architecture. This model contains multiple self-attention heads, which are specifically designed to process program code and natural language. Each self-attention head outputs a semantic sequence, so a concat operation is needed to concatenate these sequences to obtain a complete semantic vector.
[0175] S52: For v C Perform downsampling to obtain a semantically coarse-grained sequence. Then v C and The input is fed into a bidirectional simple recurrent unit F-BiRSU (Adapted Forgetable BiSRU) with an adaptive forgetting factor to obtain the semantic feature sequence H of code C. C ;
[0176] The specific steps of S52 are as follows:
[0177] S521: Employ max pooling on v C Perform downsampling to obtain a semantic coarse-grained order. For any The formula for calculating 1≤i≤m is as follows:
[0178]
[0179] in, It is the semantic vector of code C, where k is the size of the max pooling window and n is an integer multiple of k;
[0180] S521 includes, but is not limited to, the following embodiments:
[0181] Assuming the semantic vector x = [3, 5, 9, 11, 13, 17] and the downsampling window k = 3, then:
[0182] When i = 1, (1-1)k+1 = 1, 1*k = 3, then
[0183] When i = 2, (2-1)k+1 = 4, 2*k = 6, then
[0184] When i = 3, (3-1)k+1 = 7, 3*k = 9, but the original sequence only has 8 elements, so we take the last element here; then
[0185] The resulting coarse-grained sequence x after downsampling is summarized above. d=[7,13,17,0,0,0];
[0186] S522: Construct the F-BiSRU module for use from v C and Extract semantic feature sequence H C The overall structure of the F-BiSRU module is as follows: Figure 9 As shown, its F-BiSRU module consists of a forward FSRU network and a reverse FSRU network. Each network contains several FSRU units, and the structure of the FSRU unit is as follows: Figure 7 As shown;
[0187] The principle of the F-BiSRU module is: to... v C When used as input to the F-BiSRU module, it is necessary to... v C Divide it into several time series, and you will get The FSRU unit in the F-BiSRU module is used to process a pair of time series at any time t. And the processing procedure is the same: and Feature fusion is performed proportionally to obtain a multi-scale information fusion vector. And introduce the output of the hidden state from the previous time step. and The concatenation is used as the parameter for updating the forget gate, ultimately yielding the output of the hidden state at the current moment.
[0188] The F-BiSRU module designed in this invention is an improvement on the existing BiSRU model. The BiSRU model is briefly introduced below.
[0189] Bidirectional Simple Recurrent Unit (BiSRU) is a neural network structure developed based on Simple Recurrent Unit (SRU). It consists of a forward SRU network and a backward SRU network, with each SRU network composed of several SRU units. BiSRU combines the idea of bidirectional processing of sequence data, which can simultaneously consider the forward and backward information of the sequence, thus providing a more comprehensive model of the sequence data. In the forward SRU network, it updates the values of its forget gate f, reset gate r, and memory cell c by combining the forward input sequence, weight parameters, and bias values. Then, it calculates the value of the hidden layer vector h by combining the forget gate f, reset gate r, candidate memory cell c, and tanh function. The calculation process of the gate unit and memory cell unit in the backward SRU network is the same as that in the forward SRU network.
[0190] The specific steps of S522 are as follows:
[0191] S5221: First, define the gate vector I. C Used for control and v C The fusion ratio of the input time series at each time step, I C The calculation formula is as follows:
[0192]
[0193] Where Sigmoid is the activation function, W t It is a weight matrix. It is a bias vector. Indicates that v C and Time series at time t and Then splice them together, and then and According to I C Multi-scale information fusion is performed to obtain
[0194]
[0195] Where ⊙ represents element-wise multiplication;
[0196] S5221 includes, but is not limited to, the following embodiments:
[0197] Element-wise multiplication refers to multiplying corresponding elements of two matrices or vectors with the same dimensions. For example, if there are two vectors a = [a1, a2, a3] and b = [b1, b2, b3], their element-wise multiplication result is a⊙b = [a1×b1, a2×b2, a3×b3]. If it is a matrix, the elements at the same position are also multiplied one by one.
[0198] S5222: Forget gate in calculating the forward FSRU network at time t When outputting, the hidden state from the previous time step is introduced. Will and splicing as an update The parameters are calculated using the following formula:
[0199]
[0200] Among them W f It is the weight matrix, b f It is a bias vector. This represents the hidden state at a given moment on a positive FSRU network. Based on The statistical property function of the variance, where θ is a coefficient used for scaling. The magnitude of the value;
[0201] Reset gate r at time t in a forward FSRU network t Memory cells c t And the positive hidden state output at time t The calculation process is as follows:
[0202]
[0203]
[0204]
[0205] The reverse hidden state output by the reverse FSRU network at time t The calculation steps are the same as those for the forward hidden state output at time t in a forward FSRU network, but the calculation steps differ in the forget gate. The hidden state of the reverse FSRU network at that moment is introduced.
[0206]
[0207] S523: Will and Perform a concat operation to obtain The semantic feature sequence H is obtained through a linear transformation. C ,Right now:
[0208]
[0209] in
[0210] S53: Utilizing a self-attention layer to apply H C Further semantic enhancement can be performed to make H C It can incorporate information from other positions throughout the sequence, better capture the global semantic information of the code, and ultimately obtain the semantic feature vector V of code C. sC ;
[0211] S6: Adaptively fuse code C syntax feature vectors V using gating mechanisms gC and semantic feature vector V sC The fusion vector V is obtained. fusC ;
[0212] The specific steps of S6 are as follows:
[0213] S61: Design a learnable grammar matrix W gC and bias value b gC The linear layer forms the syntax gating vector g of code C. gC :
[0214] g gC =ReLU(W gC V gC +b gC )
[0215] Similarly, design a learnable semantic matrix W. sC and bias value b sC The linear layer forms the semantic gating vector g of code C. sC :
[0216] g sC =ReLU(W sC V sC +b sC )
[0217] Calculate the fusion weight α of the syntactic feature vectors of code C. C The fusion weight β of the semantic feature vector C :
[0218]
[0219] β C =1-α C
[0220] S62: Utilizing α C β C For V gC V sC The weighted summation yields the final fusion vector V from code C. fusC :
[0221] V fusC =α C V gC +β C V sC ;
[0222] Since code A or code B is set to code C in S1, therefore V fusC In fact, it also represents the fusion vector V of code A. fusA The fusion vector V of code B fusB That is, after S62, V has been obtained. fusA and V fusB ;
[0223] S7: V fusA and V fusB By concatenating the vectors, we obtain the vector to be measured, V.t V t The input is fed into a three-layer perceptron for similarity comparison, and the output is a similarity score. calculate The loss value between the actual value and the true value is used to update the learnable syntax matrix W in reverse. gC With the learnable semantic matrix W sC ;
[0224] In deep learning, backpropagation is achieved through the chain rule of differentiation. After constructing a neural network model, the difference between the model's output value and the true value is compared to obtain the loss value. Then, starting from the output layer, the loss value is used to update the parameters (such as weights and biases) of each layer of the model in reverse according to the chain rule of differentiation. This method uses the loss value to update the learnable syntax matrix and learnable semantic matrix in the gating mechanism in reverse, thereby reducing the loss value between the final output similarity score and the true value.
[0225] The specific steps of S7 are as follows:
[0226] S71: Merge vector V fusA V fusB By concatenating the vectors, we obtain the vector to be measured, V. t :
[0227]
[0228] in Indicates a splicing operation;
[0229] S72: V t The input is a three-layer perceptron for training, and the output is a value between 0 and 1 as the similarity score between code A and code B.
[0230] Z1 = ReLU(W1V) t +b1)
[0231] Z2 = ReLU(W2Z1 + b2)
[0232] Z3 = ReLU(W3Z2 + b3)
[0233]
[0234] Where W1, W2, and W3 are weight matrices, b1, b2, and b3 are bias vectors, and Z1, Z2, and Z3 are the feature representations of the first, second, and third layers of the three-layer perceptron, respectively.
[0235] S72 includes, but is not limited to, the following embodiments:
[0236] In the first training task, calculate the fusion vector V of code A and code B. fusA and V fusB Then calculate the similarity score between code A and code B.
[0237] (1) Calculate the fusion vector V of code A fusA :
[0238] Let the syntax feature vector of code A be... semantic feature vector
[0239] Calculate the syntax gating vector g gA :
[0240] Let the learnable syntax matrix be... bias value Calculate g gA :
[0241]
[0242] Compute the semantic gate vector g sA :
[0243] Let the learnable semantic matrix be... bias value Calculate g sA :
[0244]
[0245] Calculate the fusion weight α A and β A :
[0246]
[0247] β A =1-α A ≈[0.57,0.5]
[0248] Calculate the fusion vector V fusA :
[0249]
[0250] (2) Calculate the fusion vector V of code B. fusB :
[0251] The code B fusion vector is calculated using the same steps as in (1).
[0252] (3) Calculate the similarity score between code A and code B.
[0253] VfusA and V fusB By concatenating the vectors, we obtain the vector to be measured, V. t :
[0254]
[0255] Calculate the similarity score
[0256] V t The input is fed into a three-layer perceptron to obtain the similarity score between code A and code B for this training task.
[0257] S73: Calculation The loss value between the actual similar value and the true similar value is calculated using the following formula:
[0258]
[0259] Where y i This is the true similarity value between code A and code B in this training. The closer the value is to 1, the more similar code A and code B are.
[0260] S74: Utilizing the loss value to analyze the learnable syntax matrix W gC and the learnable semantic matrix W sC Perform a reverse update;
[0261] The specific steps of S74 are as follows:
[0262] S741: Against W gC The update process is as follows:
[0263]
[0264] S742: Against W sC The update process is as follows:
[0265]
[0266] Where θ represents the partial derivative, and η1 and η2 are the learning rates;
[0267] S743: By updating W gC and W sC This enables the gating mechanism to adaptively fuse the syntactic and semantic feature vectors of the code, thereby improving the accuracy of similarity score calculation;
[0268] In S74, since code C has already been declared in S1 to represent the operation process on code A and code B, therefore W gC Essentially, the learnable syntax matrix W simultaneously represents code A and code B. gA WgB W sC The learnable semantic matrix W represents code A and code B. sA W sB ;
[0269] S741: Learnable syntax matrix W for code A and code B gA and W gB The update process is as follows:
[0270]
[0271]
[0272] S742: For the learnable semantic matrix W sA and W sB The update process is as follows:
[0273]
[0274]
[0275] in η1 and η2 represent the partial derivatives, and η1 and η2 are the learning rates.
[0276] S743: By updating W gA W gB and W sA W sB This enables the gating mechanism to adaptively fuse the syntactic and semantic feature vectors of the code, thereby improving the accuracy of similarity score calculation;
[0277] The three-layer perceptron is an artificial neural network consisting of an input layer, a hidden layer, and an output layer. Each layer outputs a feature representation after linear transformation and activation by an activation function, which serves as the input to the next layer. The hidden layer can increase the complexity and expressive power of the model, enabling it to handle more complex nonlinear problems. This method uses a three-layer perceptron to calculate vector similarity.
[0278] S8: Apply the trained code similarity detection model to the code similarity detection task, calculate the similarity of any code pair <code X, code Y> in the actual input, and obtain the similarity score of the code pair.
[0279] In summary, this invention proposes an AST-GCN model adapted to the characteristics of tree structures. When traversing the syntax tree of the code and updating node features, this model fuses the features of each node's sibling nodes and applies different aggregation weights based on the syntax tree depth. Finally, the multi-head self-attention layer is input to obtain the syntax feature vector of the code.
[0280] This invention proposes a multi-scale semantic adaptive fusion model (MSF). This model uses CodeBERT to initially extract the semantic vector of the code, then uses a max pooling layer to downsample the semantic vector, and then uses the F-BiSRU module to fuse information and extract features. Finally, it inputs the self-attention layer to calculate the semantic feature vector that fuses the multi-scale information of the code.
[0281] This invention assigns learnable weight matrices to the syntactic and semantic feature vectors of two sets of code respectively, enabling them to update the values of the learnable weight matrices in reverse based on the final loss value, so as to adaptively fuse the syntactic and semantic feature vectors of the code.
[0282] Although the present invention has been described above with reference to embodiments, various modifications can be made and components can be replaced with equivalents without departing from the scope of the invention. In particular, as long as there is no structural conflict, the features in the disclosed embodiments can be combined with each other in any manner. The lack of an exhaustive description of these combinations in this specification is merely for the sake of brevity and resource conservation. Therefore, the present invention is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.
Claims
1. A code similarity detection method based on deep learning, characterized in that, The specific steps are as follows: S1: Collect code, preprocess the code, including deleting relevant comments, standardizing variable and function names, unifying code format, and combining the processed code into any pairwise combinations and code similarity rating to obtain a triple <code A, code B, similarity y>. The set of all triples constitutes a code dataset, and code A or code B is designated as code C. S2: Use a code extraction tool to parse code C and generate a syntax tree. Traverse the syntax tree to obtain the adjacency matrix of code C. and depth weight matrix d' C Then, Word2Vec is used to convert the code C into a feature vector of a specified dimension, resulting in the feature matrix Z. C ; S3: Will and Z C Inputting the AST-GCN model, AST-GCN performs syntactic feature aggregation on the syntax tree of code C to obtain the syntax feature matrix. Finally, through the multi-head self-attention layer... Feature enhancement is performed to obtain the syntactic feature vector V of code C. gC ; S4: Perform word segmentation on code C. Use a word segmentation tool to split code C into several lexical units, resulting in a lexical unit sequence X. C ; S5: Construct a multi-scale semantic feature adaptive fusion model (MSF) for multi-scale fusion and extraction of code semantic features, and integrate X... C After being input into the MSF model, the feature vector V representing the semantics of the code is obtained. sC ; S6: Adaptively fuse code C syntax feature vectors V using gating mechanisms gC and semantic feature vector V sC The fusion vector V is obtained. fusC ; S7: V fusA and V fusB By concatenating the vectors, we obtain the vector to be measured, V. t V t The input is fed into a three-layer perceptron for similarity comparison, and the output is a similarity score. calculate The loss value between the actual value and the true value is used to update the learnable syntax matrix W in reverse. gC With the learnable semantic matrix W sC ; S8: Apply the trained code similarity detection model to the code similarity detection task, calculate the similarity of any code pair <code X, code Y> in the actual input, and obtain the similarity score of the code pair; The specific steps of S3 are as follows: S31: Construct an AST-GCN model to extract the syntactic feature matrix of code C. Its AST-GCN model consists of an input layer, a feature update layer, and an output layer. The AST-GCN model fuses the features of each node in the syntax tree with the features of its neighboring nodes and sibling nodes to obtain a feature sequence containing local syntax features of the code. Finally, the syntax feature matrix is obtained through linear transformation. S32: Utilizing multi-head self-attention mechanism to... Attention weights are calculated, the syntactic feature sequences output by each attention head are concatenated, and then a linear transformation is performed to obtain the syntactic feature vector V. gC .
2. The code similarity detection method based on deep learning according to claim 1, characterized in that, The specific steps of S31 are as follows: S311: Traverse the syntax tree of code C level by level to obtain the sibling matrix. and depth weight matrix d' C ; S312: Will d′ C , and Z C The input feature update layer consists of L convolutional layers. Let the syntax tree be updated at the (l+1)th layer (l∈(0,1,…,L-1)) of the feature update layer. Then the feature matrix output by that layer... It consists of the sum of three features, namely the sibling feature matrix. Hierarchical Neighbor Feature Matrix and the feature matrix output from the layer above the feature update layer S313: Will as well as Adding them together yields the updated feature matrix after the (l+1)th feature update layer. Where ReLU is the activation function; S314: After the final layer calculation of the feature update layer, the feature matrix is output. in i∈(1,2,…,n) is Z C Feature sequences of each node The updated feature sequence output from the final layer of the feature update layer is used for... Perform a linear transformation to obtain the syntax feature matrix.
3. The code similarity detection method based on deep learning according to claim 2, characterized in that, The specific steps of S311 are as follows: S3111: Traverse the syntax tree level by level. For any node r encountered during the traversal, determine the sibling nodes of r based on the adjacency relationships between nodes, thereby obtaining the sibling matrix. exist The syntax tree stores all sibling relationships between nodes; that is, if node p and node q are siblings, then x... p,q , x p,q =x q,p =1; otherwise x p,q =x q,p =0; S3112: When traversing the syntax tree, record the depth of each node in the syntax tree in the depth matrix d. C In the middle, d C It is a diagonal matrix, where the values on the diagonal are the depths of each node; the weighting coefficients during feature aggregation are adjusted according to a pre-set maximum critical depth m, and the weights for d are... C For any node i with a depth greater than m, the value of i is replaced with Where α is the scaling factor, d i This represents the depth of node i; for any node j with a depth less than or equal to m, the value of j is replaced with 1. After adjusting the weight coefficients as described above, the final depth weight matrix d' is obtained. C ; The specific steps of S312 are as follows: S3121: Calculate the sibling characteristic matrix use and Perform feature aggregation operation to obtain in W is the inverse of the degree matrix. s It is a weight matrix; S3122: For nodes deeper in the syntax tree, in order to reduce the impact of the features of these nodes on the overall code similarity, ... Left-multiply depth weight matrix d' C Then, feature aggregation is performed to obtain the hierarchical neighbor feature matrix. Among them W n It is a weight matrix.
4. The code similarity detection method based on deep learning according to claim 1, characterized in that, The specific steps of S5 are as follows: S51: First, X C The semantic sequence output by each self-attention head of CodeBERT is concatted to obtain a preliminary feature vector representing the semantics of the code. This feature vector is then linearly transformed to obtain the semantic vector v of code C. C ; S52: For v C Perform downsampling to obtain a semantically coarse-grained sequence. Then v C and The input is fed into a bidirectional simple recurrent unit F-BiRSU with an adaptive forgetting factor to obtain the semantic feature sequence H of code C. C ; S53: Utilizing a self-attention layer to apply H C Further semantic enhancement is performed to make H C It can incorporate information from other positions throughout the sequence, better capture the global semantic information of the code, and ultimately obtain the semantic feature vector V of code C. sC .
5. The code similarity detection method based on deep learning according to claim 4, characterized in that, The specific steps of S52 are as follows: S521: Employ max pooling on v C Perform downsampling to obtain a semantic coarse-grained order. For any The formula for calculating 1≤i≤m is as follows: in, It is the semantic vector of code C, where k is the size of the max pooling window and n is an integer multiple of k; S522: Construct the F-BiSRU module for use from v C and Extract semantic feature sequence H C Its F-BiSRU module consists of a forward FSRU network and a reverse FSRU network, each network containing several FSRU units; S523: Will and Perform a concat operation to obtain The semantic feature sequence H is obtained through a linear transformation. C ,Right now: in The specific steps of S522 are as follows: S5221: First, define the gate vector I. C Used for control and v C The fusion ratio of the input time series at each time step, I C The calculation formula is as follows: Where Sigmoid is the activation function, W t It is a weight matrix. It is a bias vector. Indicates that v C and Time series at time t and Then splice them together, and then and According to I C Multi-scale information fusion is performed to obtain Where ⊙ represents element-wise multiplication; S5222: Forget gate in calculating the forward FSRU network at time t When outputting, the hidden state from the previous time step is introduced. Will and splicing as an update The parameters are calculated using the following formula: Among them W f It is the weight matrix, b f It is a bias vector. This represents the hidden state at a given moment on a positive FSRU network. Based on The statistical property function of the variance, where θ is a coefficient used for scaling. The magnitude of the value; Reset gate r at time t in a forward FSRU network t Memory cells c t And the positive hidden state output at time t The calculation process is as follows: The reverse hidden state output by the reverse FSRU network at time t The calculation steps are the same as those for the forward hidden state output by the forward FSRU network at time t, but the calculation steps differ in the forget gate. The hidden state of the previous moment in the reverse FSRU network is introduced at that time.
6. The code similarity detection method based on deep learning according to claim 1, characterized in that, The specific steps of S6 are as follows: S61: Design a learnable grammar matrix W gC and bias value b gC The linear layer forms the syntax gating vector g of code C. gC : g gC =ReLU(W gC V gC +b gC ) Similarly, design a learnable semantic matrix W. sC and bias value b sC The linear layer forms the semantic gating vector g of code C. sC : g sC =ReLU(W sC V sC +b sC ) Calculate the fusion weight α of the syntactic feature vectors of code C. C The fusion weight β of the semantic feature vector C : β C =1-α C S62: Utilizing α C β C For V gC V sC The weighted summation yields the final fusion vector V from code C. fusC : V fusC =α C V gC +β C V sC ; Since code A or code B is set to code C in S1, therefore V fusC In fact, it also represents the fusion vector V of code A. fusA The fusion vector V of code B fusB That is, after S62, V has been obtained. fusA and V fusB .
7. The code similarity detection method based on deep learning according to claim 6, characterized in that, The specific steps of S7 are as follows: S71: Merge vector V fusA V fusB By concatenating the vectors, we obtain the vector to be measured, V. t : in Indicates a splicing operation; S72: V t The input is a three-layer perceptron for training, and the output is a value between 0 and 1 as the similarity score between code A and code B. Z1=ReLU(W1V t +b1) Z2 = ReLU(W2Z1 + b2) Z3 = ReLU(W3Z2 + b3) Where W1, W2, and W3 are weight matrices, b1, b2, and b3 are bias vectors, and Z1, Z2, and Z3 are the feature representations of the first, second, and third layers of the three-layer perceptron, respectively. S73: Calculation The loss value between the actual similar value and the true similar value is calculated using the following formula: Where y i This is the true similarity value between code A and code B in this training. The closer the value is to 1, the more similar code A and code B are. S74: Utilizing the loss value to analyze the learnable syntax matrix W gC and the learnable semantic matrix W sC Perform a reverse update.
8. The code similarity detection method based on deep learning according to claim 7, characterized in that, The specific steps of S74 are as follows: S741: Against W gC The update process is as follows: S742: Against W sC The update process is as follows: in η1 and η2 represent the partial derivatives, and η1 and η2 are the learning rates. S743: By updating W gC and W sC This enables the gating mechanism to adaptively fuse the syntactic and semantic feature vectors of the code, thereby improving the accuracy of similarity score calculation.
Citation Information
Patent Citations
Smart contract code clone detection method based on AST multi-dimensional feature fusion
CN115422541A
Source code vulnerability static detection and positioning method based on graph neural network
CN115935367A