Code retrieval method and system based on multi-modal fusion neural network
Through multi-modal fusion neural network, combining different intermediate representations and natural language descriptions, the problem of semantic information loss in traditional methods is solved, more accurate code search is achieved, and the accuracy and recall rate of search tasks are improved.
Patent Information
- Application Number
- CN202410621980.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-20
- Publication Date
- 2024-09-13
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Traditional machine learning code search methods ignore semantic information such as the syntax tree of the code, resulting in the loss of semantic information and the inability to extract complex features.
Multi-modal fusion neural network is used to extract features through different intermediate representations such as Token, AST and MIX Graph, and neural network models such as LSTM, Tree-LSTM and GGNN are used for feature extraction and fusion, and natural language description is used for similarity comparison. Construct multi-modal fusion neural network for code retrieval.
It improves the accuracy and richness of code search, can more accurately extract the semantic features of the code, and significantly improves the recall rate and average reciprocal ranking.
Smart Images

Figure CN118643116A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software engineering code analysis, and more specifically to a code retrieval method and system based on a multimodal fusion neural network. Background Art
[0002] Since traditional machine learning code search methods mostly use code sequences (Tokens) as the semantic representation of the code, and ignore code structure representations with richer semantic information such as AST (Abstract Syntax Tree), CFG (Control Flow Graph) and PDG (Program Dependency Graph), it is impossible to accurately and completely extract the actual semantic features of the code. Summary of the invention
[0003] In view of this, the purpose of the present invention is to provide a code retrieval method and system based on a multimodal fusion neural network, so as to solve the defects in the prior art that the code structure rich in semantic information may cause the loss of semantic information, does not consider the context dependency, and is difficult to extract complex features.
[0004] To achieve the above object, the present invention provides the following technical solutions:
[0005] A code retrieval method based on a multimodal fusion neural network comprises the following steps:
[0006] S1. Obtain code data and parse it into three different intermediate representations: Token, AST, and MIX Graph;
[0007] S2. Construct a multimodal fusion neural network, extract features from three different intermediate representations, namely Token, AST and MIX Graph, based on different neural network models in the multimodal fusion neural network, and obtain three code feature vectors based on Token, AST and MIX Graph;
[0008] S3, using the fusion layer in the multimodal fusion neural network to concatenate and fuse the three code feature vectors based on Token, AST and MIX Graph to obtain the code feature representation;
[0009] S4. Construct a feature extraction neural network for the natural language description of the code data based on the LSTM network model, and use the feature extraction neural network of the natural language description to extract features of the natural language description to obtain code description features;
[0010] S5, comparing the code description features with the code feature representations for similarity, and training the constructed multimodal fusion neural network;
[0011] S6. Use the trained multimodal fusion neural network for code retrieval.
[0012] Furthermore, in step S2, feature extraction is performed on three different intermediate representations of Token, AST and MIX Graph based on different neural network models in the multimodal fusion neural network, specifically including:
[0013] Build a Token feature extraction neural network model based on the LSTM network model, and use the Token feature extraction neural network model to extract the features of the Token;
[0014] Based on the Tree-LSTM network model, a neural network model for feature extraction of AST is constructed, and the feature extraction neural network model of AST is used to extract features from AST;
[0015] A feature extraction neural network model of Mix Graph is constructed based on the GGNN network model, and features of Mix Graph are extracted using the feature extraction neural network model of Mix Graph.
[0016] Furthermore, the feature extraction neural network model of Token is constructed based on the LSTM network model, and the feature extraction neural network model of Token is used to extract the features of Token, specifically:
[0017] For the token representation of the i-th code data, use the LSTM network model to extract features:
[0018]
[0019] Where: w emb It is the word embedding layer, which is used to embed Token representation;
[0020] Then the self-attention mechanism is used to further refine the features to obtain the vector representation of the Token:
[0021]
[0022] in, represents the i-th hidden state in Token, f token is a linear layer, g token is the inner product; u token A context vector representing the tag modality;
[0023] The vector representation of the Token is further processed to obtain a vector representing the final Token:
[0024]
[0025] in, Represents the attention weight of Token.
[0026] Furthermore, the feature extraction neural network model of AST is constructed based on the Tree-LSTM network model, and the feature extraction neural network model of AST is used to extract features from AST, specifically:
[0027] For the AST representation of the i-th code data, the Tree-LSTM network model is used for feature extraction;
[0028] First, convert the AST normal tree into a binary tree;
[0029]
[0030] Among them, (h L , c L ) represents the hidden state and memory unit state of the left child node, (h R , c R ) represents the hidden state and memory unit state of the right child node; and Indicates the concatenation of the hidden state and memory unit state of the left and right nodes, connecting the two vectors into a longer vector; [;] indicates the concatenation between vectors;
[0031] Then the self-attention mechanism is used to further refine the features to obtain the vector representation AST of AST;
[0032]
[0033] In the above formula, Represents the attention weight of AST.
[0034] Furthermore, the feature extraction neural network model of Mix Graph is constructed based on the GGNN network model, and the feature extraction neural network model of Mix Graph is used to extract features from Mix Graph, specifically:
[0035] For a graph Where (v, l v ) represents the nodes of the graph (v i , v j , l e ) represents the edge ε,l v and l e Labels for nodes and edges:
[0036] First, for each vertex in the graph Initialize the hidden state:
[0037]
[0038] Where w represents the one-hot encoded embedding;
[0039] For each round t, each vertex We will get a vector m v,t+1 , which represents the messages converged from its neighboring nodes. These messages are aggregated into the following expression:
[0040]
[0041] Among them, N(V) represents the neighboring set of vertex v; in the tth round, is the weight matrix that maps the messages of each neighboring node to the shared space;
[0042] Next, the GGNN network model uses a gated recurrent unit to update the hidden state of each vertex:
[0043]
[0044] Finally, after t rounds of iterations, the hidden states of all vertices are summed up to obtain the embedded representation; then the weight α is used graph To represent each graph node:
[0045]
[0046] in, represents the i-th hidden state in the graph node; f graph is a linear layer, g graph is the inner product; u graph The context vector representing the graph is a high-level representation of the entire graph node;
[0047] Finally, the final semantic representation of the graph is obtained:
[0048]
[0049] In the above formula, G is the final semantic representation of the graph, and w represents the attention weight of the Mix Graph.
[0050] Furthermore, in step S2, the code description feature and the code feature representation are compared for similarity, and the constructed multimodal fusion neural network is trained, specifically:
[0051] Each training instance is constructed as a triplet<c,d+,d-> ;
[0052] Then use the following loss function and train the model using two pairs of functions c, d+ and c, d- respectively,
[0053]
[0054] Where,∈ is a constant boundary; (C,D+,D-) is the embedding vector of c,d+,d-;
[0055] c is each code snippet; d+ is a positive description; d- is a negative description.
[0056] The present application also provides a code retrieval system based on a multimodal fusion neural network, the system comprising:
[0057] Data processing module: obtains code data and parses it into three different intermediate representations: Token, AST, and MIXGraph;
[0058] Data extraction module: construct a multimodal fusion neural network, extract features from three different intermediate representations of Token, AST and MIX Graph based on different neural network models in the multimodal fusion neural network, and obtain three code feature vectors based on Token, AST and MIX Graph;
[0059] The fusion layer in the multimodal fusion neural network is used to concatenate and fuse the three code feature vectors based on Token, AST and MIX Graph to obtain the code feature representation;
[0060] Model building module: Based on the LSTM network model, a feature extraction neural network for the natural language description of code data is built. The feature extraction neural network for the natural language description is used to extract features from the natural language description to obtain code description features.
[0061] Compare the similarity between the code description features and the code feature representation, and train the constructed multimodal fusion neural network;
[0062] Code retrieval module: Use the trained multimodal fusion neural network to perform code retrieval.
[0063] According to the specific embodiments provided by the present invention, the present invention improves the limitations of the traditional machine learning method for code search methods and has the following technical effects:
[0064] (1) The method of the present invention parses the code snippet and generates a plurality of intermediate representations containing different code semantics, thereby further obtaining richer semantic information in the code snippet;
[0065] (2) The present invention uses a variety of different neural networks to extract different intermediate representations and fuse them, thereby improving the accuracy of code search tasks;
[0066] (3) In the present invention, the code parsing is transformed to obtain different intermediate representations: Token, AST, Mix Graph (composed of CFG and PDG). These different intermediate representations explain the semantic information of the code from multiple perspectives, thereby helping the model to obtain code features more accurately.
[0067] (4) In the present invention, multiple different neural network encoders are established to extract different intermediate code representation information, and then a fusion strategy is designed to fuse the extracted feature information to obtain the code features. Multiple ablation experiments are designed to explore the effects of different intermediate representations and their combinations on code search. At the same time, the present invention also has significant improvements compared with other baseline models. BRIEF DESCRIPTION OF THE DRAWINGS
[0068] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0069] The code retrieval method based on the multimodal fusion neural network of the present invention is further described below in conjunction with the accompanying drawings;
[0070] Figure 1 It is a code search framework of a multimodal neural network in a code retrieval method based on a multimodal fusion neural network provided by the present invention;
[0071] Figure 2 It is an example of code snippets, code descriptions, and different intermediate representations in the code retrieval method based on multimodal fusion neural network provided by the present invention;
[0072] Figure 3 are the results of different ablation experiments in the code retrieval method based on multimodal fusion neural network provided by the present invention;
[0073] Figure 4 This is the comparison effect between the present invention and the traditional machine learning method. DETAILED DESCRIPTION
[0074] The specific implementation of the present invention is further described in detail below in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.
[0075] In order to better understand the purpose, structure and function of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings.
[0076] The present invention provides a code retrieval method based on a multimodal fusion neural network. Specifically, the code retrieval method in the present invention is expressed through four main processes:
[0077] (1) Parse the code data; (2) Extract and fuse the intermediate representation features of the parsed code; (3) Construct a multimodal fusion neural network based on the code representation features, and use the LSMT network model to extract the code representation features; (4) Train, verify and test the multimodal fusion neural network. The following is a detailed description of each step:
[0078] like Figure 1 As shown, (1) the process of parsing code data: S1, obtaining code data, and parsing the code data into three different intermediate representations: Token, AST and MIX Graph;
[0079] S2. Construct a multimodal fusion neural network, and extract features from three different intermediate representations, namely Token, AST and MIX Graph, based on different neural network models in the multimodal fusion neural network, to obtain three code feature vectors based on Token, AST and MIX Graph.
[0080] It should be noted that: Figure 1 The code search framework of multimodal neural network is presented in the paper, which mainly includes data parsing, intermediate representation feature extraction, code feature fusion, code description feature extraction, model training and prediction. The model is divided into two parts: online and offline. The offline part is the main model training part. For code snippets, three different modal representations, Token, AST, and Graph, are generated. LSTM, Tree-LSTM, and GGNN are used to extract features and self-attention mechanism is used to further refine the features. Then, these three features are concatenated and fused through the fusion layer to represent the code features. For the natural language description of the code, LSTM+self-attention mechanism is used to extract features. Finally, the cosine distance between the two in the embedding space is used as the similarity measure. The online part uses the trained multimodal fusion neural network, inputs the desired code function description, and the multimodal fusion neural network returns the corresponding code.
[0081] Step S2 is as follows: Figure 2 As shown in the figure: Based on different neural network models in the multimodal fusion neural network, feature extraction is performed on three different intermediate representations: Token, AST and MIX Graph, including:
[0082] Build a Token feature extraction neural network model based on the LSTM network model, and use the Token feature extraction neural network model to extract the features of the Token;
[0083] Based on the Tree-LSTM network model, a neural network model for feature extraction of AST is constructed, and the feature extraction neural network model of AST is used to extract features from AST;
[0084] A feature extraction neural network model of Mix Graph is constructed based on the GGNN network model, and features of Mix Graph are extracted using the feature extraction neural network model of Mix Graph.
[0085] Based on the LSTM network model, a Token feature extraction neural network model is built, and the Token feature extraction neural network model is used to extract the Token features. Specifically:
[0086] For the token representation of the i-th code data, use the LSTM network model to extract features:
[0087]
[0088] Where: w emb It is the word embedding layer, which is used to embed Token representation;
[0089] Then the self-attention mechanism is used to further refine the features to obtain the vector representation of the Token:
[0090]
[0091] in, represents the i-th hidden state in Token, f token is a linear layer, g token is the inner product; u token A context vector representing the tag modality;
[0092] The vector representation of the Token is further processed to obtain a vector representing the final Token:
[0093]
[0094] in, Represents the attention weight of Token.
[0095] A feature extraction neural network model of AST is constructed based on the Tree-LSTM network model, and features of AST are extracted using the feature extraction neural network model of AST. Specifically, for the AST representation of the i-th code data, the Tree-LSTM network model is used to extract features;
[0096] First, convert the AST normal tree into a binary tree;
[0097]
[0098] Among them, (h L ,c L ) represents the hidden state and memory unit state of the left child node, (h R ,c R ) represents the hidden state and memory unit state of the right child node; and Indicates the concatenation of the hidden state and memory unit state of the left and right nodes, connecting the two vectors into a longer vector; [;] indicates the concatenation between vectors;
[0099] Then the self-attention mechanism is used to further refine the features to obtain the vector representation AST of AST;
[0100]
[0101] In the above formula, Represents the attention weight of AST.
[0102] In step S2, a feature extraction neural network model for Mix Graph is constructed based on the GGNN network model, and the feature extraction neural network model of Mix Graph is used to extract features from Mix Graph, specifically:
[0103] For a graph Where (v,l v ) represents the nodes of the graph (v i ,v j ,l e ) represents the edge ε,l v and l e Labels representing nodes and edges;
[0104] First, for each vertex in the graph Initialize the hidden state:
[0105]
[0106] Where w represents the one-hot encoded embedding;
[0107] For each round t, each vertex We will get a vector m v,t+1 , which represents the messages converged from its neighboring nodes. These messages are aggregated into the following expression:
[0108]
[0109] Among them, N(v) represents the neighboring set of vertex v; in the tth round, is the weight matrix that maps the messages of each neighboring node to the shared space;
[0110] Next, the GGNN network model uses a gated recurrent unit to update the hidden state of each vertex:
[0111]
[0112] Finally, after t rounds of iterations, the hidden states of all vertices are summed up to obtain the embedded representation; then the weight α is used graph To represent each graph node:
[0113]
[0114] in, represents the i-th hidden state in the graph node; f graph is a linear layer, g graph is the inner product; u graph The context vector representing the graph is a high-level representation of the entire graph node;
[0115] Finally, the final semantic representation of the graph is obtained:
[0116]
[0117] In the above formula, G is the final semantic representation of the graph, and w represents the attention weight of the Mix Graph.
[0118] It should be noted that: Figure 2 The figure shows a piece of data and its other intermediate representations, including the source code snippet in Figure a and its corresponding natural language description; Figure b shows the AST that has been converted into a binary tree, Figure c shows CFG, and Figure d shows PDG (including PDGd (green data flow) and PDGc (blue control flow)).
[0119] S3, using the fusion layer in the multimodal fusion neural network to concatenate and fuse the three code feature vectors based on Token, AST and MIX Graph to obtain the code feature representation;
[0120] S4. Construct a feature extraction neural network for the natural language description of the code data based on the LSTM network model, and use the feature extraction neural network for the natural language description to extract features from the natural language description to obtain code description features;
[0121] S5, comparing the code description features with the code feature representations for similarity, and training the constructed multimodal fusion neural network;
[0122] S6. Use the trained multimodal fusion neural network for code retrieval.
[0123] like Figure 3 As shown, the present invention further provides ablation experiment results: the present invention explores the effects of different intermediate representations and different combination schemes on code search tasks, uses recall rate (R@1, R@5, R@10) and mean reciprocal ranking (MRR) to quantify the specific performance, and compares single modal experiments that only use CFG, PDG, Mix (Mix graph), AST and Token to represent code features in a single modality. It can be concluded that Token (0.235, 0.433, 0.522, 0.333) performs best, followed by AST (0.232, 0.403, 0.481, 0.317), and then Mix. The effects of Graph (0.106, 0.241, 0.310, 0.177), CFG (0.052, 0.118, 0.177, 0.095) and PDG (0.061, 0.154, 0.201, 0.112) are almost the same; then the two-by-two combinations of different modes are studied; finally, the three modes are mixed together, and it can be concluded that the combination of Token+AST+Mix graph is the best.
[0124] like Figure 4 As shown, the present invention further demonstrates the comparison with traditional machine learning methods. The present invention is compared with the latest baseline methods (DeepCS and MMAN) in the field, and the components of Mix Graph, CFG and PDG, are separated. Token, AST, CFG fusion experiments and Token, AST, PDG fusion experiments and Token, AST, Mix experiments are designed respectively. Obviously, the method Token+AST+Mix (0.370, 0.594, 0.671, 0.474) of the present invention includes its separable sub-methods Token+AST+CFG (0.369, 0.583, 0.657, 0.469) and Token+AST+PDG (0.351, 0.569, 0.651, 0.456) in recall rate (R@1, R@5, R@10) and mean reciprocal ranking (MRR) are higher than the traditional machine learning methods DeepCS (0.294, 0.495, 0.589, 0.393) and MMAN (0.319, 0.532, 0.622, 0.422).
[0125] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A code retrieval method based on multimodal fusion neural network, characterized in that: The following steps are involved: S1. Obtain code data and parse it into three different intermediate representations: Token, AST, and MIX Graph; S2. Construct a multimodal fusion neural network, extract features from three different intermediate representations, namely Token, AST and MIX Graph, based on different neural network models in the multimodal fusion neural network, and obtain three code feature vectors based on Token, AST and MIX Graph; S3, using the fusion layer in the multimodal fusion neural network to concatenate and fuse the three code feature vectors based on Token, AST and MIX Graph to obtain the code feature representation; S4. Construct a feature extraction neural network for the natural language description of the code data based on the LSTM network model, and use the feature extraction neural network of the natural language description to extract features of the natural language description to obtain code description features; S5, comparing the code description features with the code feature representations for similarity, and training the constructed multimodal fusion neural network; S6. Use the trained multimodal fusion neural network for code retrieval.
2. The code retrieval method based on multimodal fusion neural network according to claim 1 is characterized in that: In the step S2, feature extraction is performed on three different intermediate representations of Token, AST and MIXGraph based on different neural network models in the multimodal fusion neural network, specifically including: Build a Token feature extraction neural network model based on the LSTM network model, and use the Token feature extraction neural network model to extract the features of the Token; Based on the Tree-LSTM network model, a neural network model for feature extraction of AST is constructed, and the feature extraction neural network model of AST is used to extract features from AST; A feature extraction neural network model of Mix Graph is constructed based on the GGNN network model, and features of Mix Graph are extracted using the feature extraction neural network model of Mix Graph.
3. The code retrieval method based on multimodal fusion neural network according to claim 2 is characterized in that: The Token feature extraction neural network model is constructed based on the LSTM network model, and the Token feature extraction neural network model is used to extract the features of the Token, specifically: For the token representation of the i-th code data, use the LSTM network model to extract features: Where: w emb It is the word embedding layer, which is used to embed Token representation; Then the self-attention mechanism is used to further refine the features to obtain the vector representation of the Token: in, represents the i-th hidden state in Token, f token is a linear layer, g token is the inner product; u token A context vector representing the tag modality; The vector representation of the Token is further processed to obtain a vector representing the final Token: in, Represents the attention weight of Token.
4. The code retrieval method based on multimodal fusion neural network according to claim 2 is characterized in that: The feature extraction neural network model of AST is constructed based on the Tree-LSTM network model, and the feature extraction neural network model of AST is used to extract features from AST, specifically: For the AST representation of the i-th code data, the Tree-LSTM network model is used for feature extraction; First, convert the AST normal tree into a binary tree; Among them, (h L ,c L ) represents the hidden state and memory unit state of the left child node, (h R ,c R ) represents the hidden state and memory unit state of the right child node; and Indicates the concatenation of the hidden state and memory unit state of the left and right nodes, connecting the two vectors into a longer vector; [;] indicates the concatenation between vectors; Then the self-attention mechanism is used to further refine the features to obtain the vector representation AST of AST; In the above formula, Represents the attention weight of AST.
5. The code retrieval method based on multimodal fusion neural network according to claim 2 is characterized in that: The feature extraction neural network model of Mix Graph is constructed based on the GGNN network model, and the feature extraction neural network model of Mix Graph is used to extract features from Mix Graph, specifically: For a graph Where (v,l v ) represents the nodes of the graph v i ,v j ,l e ) represents the edge ε,l v and l e Labels representing nodes and edges; First, for each vertex in the graph Initialize the hidden state: Where w represents the one-hot encoded embedding; For each round t, each vertex We will get a vector m v,t+1 , which represents the messages converged from its neighboring nodes. These messages are aggregated into the following expression: Among them, N(v) represents the neighboring set of vertex v; in the tth round, is the weight matrix that maps the messages of each neighboring node to the shared space; Next, the GGNN network model uses a gated recurrent unit to update the hidden state of each vertex: Finally, after t rounds of iterations, the hidden states of all vertices are summed up to obtain the embedded representation; then the weight α is used graph To represent each graph node: in, represents the i-th hidden state in the graph node; f graph is a linear layer, g graph is the inner product; u graph The context vector representing the graph is a high-level representation of the entire graph node; Finally, the final semantic representation of the graph is obtained: In the above formula, G is the final semantic representation of the graph, and w represents the attention weight of the Mix Graph.
6. The code retrieval method based on multimodal fusion neural network according to claim 1 is characterized in that: In step S2, the code description feature and the code feature representation are compared for similarity, and the constructed multimodal fusion neural network is trained, specifically: Each training instance is constructed as a triplet<c,d+,d-> ; Then use the following loss function and train the model using two pairs of functions c,d+ and c,d- respectively: Where,∈ is a constant boundary; (C,D+,D-) is the embedding vector of c,d+,d-; c is each code snippet; d+ is a positive description; d- is a negative description.
7. A code retrieval system based on multimodal fusion neural network, characterized in that: The system is applied to the code retrieval method based on multimodal fusion neural network described in any one of claims 1 to 6 above, and the system includes: Data processing module: obtains code data and parses it into three different intermediate representations: Token, AST, and MIX Graph; Data extraction module: construct a multimodal fusion neural network, extract features from three different intermediate representations of Token, AST and MIX Graph based on different neural network models in the multimodal fusion neural network, and obtain three code feature vectors based on Token, AST and MIX Graph; The fusion layer in the multimodal fusion neural network is used to concatenate and fuse the three code feature vectors based on Token, AST and MIX Graph to obtain the code feature representation; Model building module: Based on the LSTM network model, a feature extraction neural network for the natural language description of code data is built. The feature extraction neural network for the natural language description is used to extract features from the natural language description to obtain code description features. Compare the similarity between the code description features and the code feature representation, and train the constructed multimodal fusion neural network; Code retrieval module: Use the trained multimodal fusion neural network to perform code retrieval.
Citation Information
Patent Citations
Code retrieval method and system based on graph neural network and computer readable storage medium
CN113886520A
Code searching method based on multi-dimensional matching
CN114186017A
Code similarity detection method based on code attribute graph
CN115438709A
Code completion method and apparatus, and device
WO2024027279A1
