Multi-granularity defect prediction method based on graph convolutional network
Through the multi-grained defect prediction method based on graph convolution network, token-level and row-level graphs are constructed, and joint reasoning is performed using sliding windows and attention mechanisms, the problem of lack of context semantic information and granular defect prediction in the prior art is solved, and the accuracy and efficiency of software defect prediction are improved.
Patent Information
- Application Number
- CN202510277193.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-10
- Publication Date
- 2025-07-25
AI Technical Summary
The existing software defect prediction methods lack context semantic information and ignore joint reasoning between defect predictions of different granularity, resulting in inefficient defect location.
Using a multi-grained defect prediction method based on graph convolution network, the token-level and row-level graphs are constructed, and the context semantic relationship of the code is captured using a sliding window, and combined inference is performed with the attention mechanism to achieve cross-grained defect prediction.
It improves the accuracy and efficiency of software defect prediction, reduces labor and time costs, and improves the quality of software development.
Smart Images

Figure CN120372491A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of deep learning, and in particular, to a multi-granularity defect prediction method based on a graph convolutional network. Background Art
[0002] Software defect prediction is an important research topic in the field of software quality assurance. The scale and complexity of modern software products are increasing continuously. At the same time, development teams are increasingly inclined to release software products in a shorter period of time. To relieve the review pressure brought by software quality assurance, various automatic defect identification technologies have been proposed. However, despite the application of many software quality assurance practices and various technologies (such as code review, static analysis) during the development process, there may still be many defects in the official release version of the software. In addition, software quality assurance usually accounts for 23% of the total workload during the software development process. Therefore, it is crucial to effectively reduce the cost burden of software quality assurance activities and improve their effectiveness.
[0003] Currently, various software defect prediction technologies have been proposed to help developers conduct prior analysis. Although these methods have made progress in software quality assurance, there are still some deficiencies in practical applications. First, the lack of context semantic information. The semantic context between adjacent tokens and adjacent code lines is not fully utilized. Existing research mainly focuses on defects caused by tokens in code lines. However, code has a hierarchical structure of logical relationships. At the same time, these methods often ignore the context semantic information and local interactions between code lines, thus ignoring defects caused by cross-line connections in early code reviews. Therefore, effectively and fully utilizing the semantic information between different code lines is a challenge. Second, the joint reasoning between different granularity defect predictions is ignored. Due to the hierarchical structure of code, codes at different granularities show different degrees of relationships. Only focusing on coarse-grained defect prediction makes it challenging to quickly and accurately locate the defect lines of the code. By using the file-level defect prediction results to further perform line-level defect prediction, developers can significantly improve their efficiency in locating defect code lines. Summary of the Invention
[0004] In view of this, the purpose of the present invention is to propose a multi-granularity defect prediction method based on a graph convolutional network to solve the technical problems that existing software defect prediction methods lack context semantic information and ignore the joint reasoning between different granularity defect predictions.
[0005] The technical means adopted by the present invention are as follows:
[0006] A multi-granularity defect prediction method based on a graph convolutional network, comprising the following steps:
[0007] S1. Preprocess the dataset to obtain preprocessed data;
[0008] S2. Obtain an embedding matrix based on the preprocessed data, and use the embedding matrix to map each token in the code file to obtain code tokens;
[0009] S3. Based on the code tokens, use a sliding window of fixed size to construct a line-level graph and a token-level graph;
[0010] S4. Input the obtained token-level graph and line-level graph into a multi-granularity defect prediction component for prediction.
[0011] Further, S1 specifically includes the following steps:
[0012] Delete blank lines from the code in the dataset, remove special characters, leave useful code tokens, and eliminate noisy data.
[0013] Further, S2 specifically includes the following steps:
[0014] Obtain the vector representation of the code tokens; map each token to the embedding space, and train each different project separately to obtain a unique token embedding matrix for each different project.
[0015] Further, S3 specifically includes the following steps:
[0016] Let the window size be n. For each line of code, include the first n tokens in window w1, and then slide the window one token to the right, including the second to the (n + 1)-th tokens in window w2, until the window contains the last token; these windows of length n form a set of sliding windows;
[0017] Next, traverse the set of sliding windows, considering that all nodes appearing in the current window are connected by an edge, to obtain the token-level graph of the current code line;
[0018] For a file, assign consecutive indices starting from 0 in the order of appearance of each line of code; use a sliding window of the inherent window size to slide from the beginning to the end of the file to generate a series of sliding windows containing index numbers; traverse the sliding windows containing index numbers, and consider all lines of code within the current window as connected by an edge to obtain a graph representing the relationship between adjacent codes.
[0019] Further, in S4, the multi-granularity defect prediction component includes a token-level graph encoder, a token-level attention layer, a line-level graph encoder, and a line-level attention layer;
[0020] The token-level graph encoder is as follows:
[0021] For the i-th line of code, denoted as L i; The token-level graph encoder has two inputs: The first input of the token-level graph encoder is the features of the nodes in the token-level graph of this line of code, denoted as where N is the number of nodes in the graph and also the number of tokens in this line; the second input of the token-level graph encoder is the adjacency matrix in the token-level graph, denoted as A. The token-level encoder propagates the first input of the token-level graph encoder and the second input of the token-level graph encoder to integrate the context connections between each token:
[0022]
[0023] where, I is the N*N identity matrix, is the degree matrix of, W t is the weight matrix that is continuously adjusted during training;
[0024] The token-level attention layer is as follows:
[0025] The attention mechanism is used to obtain the importance of each token, and the importance of each token is used to obtain the line-level features of the code. The output hidden state representation is obtained from the token-level graph convolutional layer using a multi-layer perceptron:
[0026] u it = tanh(W ω h it + b w )
[0027] where, W ω is the weight matrix of the MLP, b w is the bias term;
[0028] Multiply the output of the MLP by the token-level context vector u w , and use the softmax function to calculate the attention score of each token:
[0029]
[0030] where, α it represents the attention score of the t-th token in the i-th row;
[0031] To obtain the vector representation of each line of code, use the attention score of each token in this line and the output vector of the token-level graph encoder to recalculate the hidden layer vector representation of each token, and sum the vectors in the same line to obtain the vector representation of each line of code:
[0032] s i = ∑ t α it h it
[0033] The node features of the line-level graph represented by the obtained file are
[0034] The line-level graph encoder is as follows:
[0035] For a file, the line-level graph encoder has two inputs: the first input of the line-level graph encoder is the node features in each line-level graph, where N is the number of nodes in the file; the second input of the line-level graph encoder is the adjacency matrix A in the line-level graph, and the line-level graph encoder propagates these two inputs to integrate the context connections between code lines:
[0036]
[0037] where, W l is the weight matrix adjusted in the training phase, represents the node feature matrix of the hidden layer and is passed as an input to the row attention layer;
[0038] The row-level attention layer is as follows:
[0039] Use a single-layer MLP to obtain the hidden state of the output of the line-level graph encoder:
[0040] u i = tanh(W s h i + b s )
[0041] where, W s represents the weight matrix, and b s represents the bias term;
[0042] Multiply the row-level context vectors and use the softmax function to obtain the attention scores for each line of code:
[0043]
[0044] Multiply the output of the line-level graph encoder by the attention scores of each line of code to reallocate the vector representation of the code lines; aggregate the vector representations of the code lines within the same file to obtain the vector representation of the code file as the high-dimensional feature of the file:
[0045] e i = ∑ t α i h i
[0046] Feed the vector representation of the file back to a fully connected layer to obtain the predicted label for each file; calculate the final predicted probability of the existence of defects in the file through the sigmoid activation function:
[0047]
[0048] Among them, w h represents the weight matrix of the fully connected layer, and b h represents the bias term.
[0049] Furthermore, in S4, in file-level prediction, the predicted probability is compared and analyzed with the ground truth of the file to obtain evaluation metrics and results; for line-level prediction, attention scores are used for prediction, and the code lines or tokens with higher attention scores are considered to have a higher risk probability;
[0050] In line-level prediction, all predicted defective code files are extracted, the token-level attention scores are extracted from the defective code files and sorted in descending order, and the top k scores are selected for subsequent analysis; the top-k scores are grouped according to the code lines corresponding to the top k scores, and the sum of the scores of each group is calculated. If the labels are not in the top k, the risk score is counted as 0; the risk scores of each line are obtained, sorted in descending order, and further analyzed and evaluated.
[0051] The present invention also provides a storage medium, which includes a stored program. Among them, when the program runs, it executes any one of the above multi-granularity defect prediction methods based on a graph convolutional network.
[0052] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. The processor runs through the computer program to execute any one of the above multi-granularity defect prediction methods based on a graph convolutional network.
[0053] Compared with the prior art, the present invention has the following advantages:
[0054] The technical solution provided by the present invention performs defect prediction through a multi-granularity defect prediction method based on a graph convolutional network, which uses a graph neural network to effectively capture the context semantic relationships in the code. The token embedding component uses an embedding matrix to deduce the vector representation of the token, the graph extraction component captures potential context semantic relationships by applying a sliding window to construct a graph, and the multi-granularity defect prediction component combines the graph convolutional network and the attention mechanism to perform joint reasoning across various defect prediction granularities. Finally, it is applied to defect prediction.
[0055] The present invention saves countless time and labor costs; the present invention is of great significance for improving defect prediction; the present invention improves the development efficiency and quality of software and reduces software defects. Description of the Drawings
[0056] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the accompanying drawings required for the description of the embodiments or the prior art. Obviously, the accompanying drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can also be obtained based on these drawings.
[0057] Figure 1 This is the flowchart of the method of the present invention. Detailed implementation manners
[0058] To enable those skilled in the art to better understand the solution of the present invention, the following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only some of the embodiments of the present invention, rather than all of them. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0059] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above accompanying drawings are used to distinguish similar objects, and do not necessarily need to be used to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances so that the embodiments of the present invention described here can be implemented in an order other than those illustrated or described here. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device including a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0060] As Figure 1 shown, the present invention provides a multi-granularity defect prediction method based on a graph convolutional network, including the following steps:
[0061] S1. Preprocess the data set to obtain preprocessed data;
[0062] The present invention uses the line-level defect data set prepared by Wattanakriengkrai et al. to evaluate its effectiveness. This data set includes 32 different project versions from 9 open-source software systems. In these data sets, each data set contains a different number of files, ranging from 731 to 8,846, and the number of lines of code ranges from 74k to 567k. Among them, the defective code files only account for 2% to 28% of the total files in the project.
[0063] Data preprocessing: Code often contains rich redundant information, so the code data is preprocessed before token vector embedding. First, empty lines are removed from the code. Since special characters such as spaces in the code will introduce additional noise to the model, it is necessary to remove these special characters. Only useful code tokens are left and the noise data is effectively eliminated.
[0064] S2. Obtain an embedding matrix based on the preprocessed data, and use the embedding matrix to map each token in the code file to obtain code tokens;
[0065] Token embedding: After data preprocessing, a vector representation of the code tokens needs to be obtained. To better map each token to the embedding space, the present invention trains each different project separately to obtain a unique token embedding matrix for each different project. This method ensures that the generated code token vectors are based on the specific vocabulary of their corresponding projects, thereby obtaining a better vector representation of the code tokens.
[0066] S3. Based on the code tokens, construct a line-level graph and a token-level graph using a sliding window of a fixed size;
[0067] Graph extraction: Use a sliding window of a fixed size to construct a line-level graph G l =(V l , E l ) and a token-level graph G t =(V t , E t ). The window size is set to 3. For each line of code, for example, for the line of code "import org apache activemq command ActiveMQQueue", it includes the first three tokens in window w1, and then the window is slid one token to the right to include the second to fourth tokens in window w2, and so on until the window includes the last token. These windows of length 3 form a set of sliding windows. Next, traverse this set, considering that all nodes appearing in the current window are connected by an edge. After traversing this set, the token-level graph of the current code line is obtained. In addition, since the length of each line of code is often relatively short, when constructing the token-level graph, the size of the sliding window can be set to a smaller value, such as 2.
[0068] For a file, consecutive indexes can be assigned starting from 0 in the order in which each line of code appears. Similar to token-level graph construction, a sliding window with an inherent window size slides from the beginning to the end of the file to generate a series of sliding windows containing index numbers. Traverse these windows one by one, and consider all the lines of code within the current window as connected by an edge. Through this process, a graph representing the relationships between adjacent codes is ultimately obtained. Since code files usually contain many lines of code, a larger sliding window size is used when constructing the line-level graph.
[0069] S4. Input the obtained token-level graph and line-level graph into the multi-granularity defect prediction component for prediction.
[0070] The multi-granularity defect prediction component is based on a hierarchical attention network. It uses a two-layer graph convolutional network to incorporate the code graph into the model, thereby capturing the context semantic relationships and local interactions in the code. The multi-granularity defect prediction component mainly consists of four parts: (1) Token-level graph encoder; (2) Token-level attention layer; (3) Line-level graph encoder; (4) Line-level attention layer.
[0071] (1) Token-level graph encoder; For the i-th line of code, denoted as L i . The token-level graph encoder has two inputs: The first is the feature of the nodes in the token-level graph of this line of code, denoted as (where N is the number of nodes in the graph and also the number of tokens in this line). The second is the adjacency matrix in the token-level graph, denoted as A. The token-level encoder propagates the above two inputs to integrate the context connections between each token:
[0072]
[0073] where, I is the N*N identity matrix, is 's degree matrix, and W t is the weight matrix that is continuously adjusted during training.
[0074] (2) Token attention layer; Since not all token pairs contribute equally to the potential risk of a line of code, an attention mechanism is adopted to obtain the importance of each token and use it to obtain the line-level characteristics of the code. Use a multi-layer perceptron to obtain the output hidden state representation from the token-level graph convolutional layer:
[0075] u it =tanh(W ω h it +b w )
[0076] where, W ω is the weight matrix of the MLP, and b wis the bias term. Multiply the output of the MLP by the token-level context vector u w , and use the softmax function to calculate the attention score for each token. That is:
[0077]
[0078] where αi t represents the attention score of the t-th token in the i-th row.
[0079] Finally, to obtain the vector representation of each line of code, it is necessary to use the attention scores of each token in that line and the output vector of the token-level graph encoder to recalculate the hidden layer vector representation of each token, and sum the vectors in the same line, so as to obtain the vector representation of each line of code:
[0080] s i =∑ t α it h it
[0081] The node features of the line-level graph represented by the finally obtained file are
[0082] (3) Line-level graph encoder; Similar to the token-level graph encoder, for a file, the line-level graph encoder has two inputs: the first is the node features in each line-level graph, (where N is the number of nodes in the file). The second is the adjacency matrix A in the line-level graph. The line-level graph encoder propagates these two inputs to integrate the context connections between code lines:
[0083]
[0084] where, W l is the weight matrix adjusted during the training phase. represents the node feature matrix of the hidden layer and is passed as input to the row attention layer.
[0085] (4) Row attention layer; Similar to the token-level attention layer, not all code lines in the file contribute equally to the defects in the file. First, use a single-layer MLP to obtain the hidden state output by the line-level graph encoder:
[0086] u i =tanh(W s h i +b s )
[0087] where, W s represents the weight matrix, b sDenote the bias term. Then multiply the line-level context vectors and use the softmax function to obtain the attention scores for each line of code:
[0088]
[0089] Then multiply the output of the line-level graph encoder by the attention scores of each line of code to re-distribute the vector representation of the code lines. Aggregate the vector representations of the code lines within the same file to obtain the vector representation of the code file, which can be used as the high-dimensional feature of the file:
[0090] e i = ∑ t α i h i
[0091] Finally, to obtain the predicted label for each file, feed the vector representation of the file into a fully connected layer and calculate the final predicted probability of the existence of defects in the file through the sigmoid activation function:
[0092]
[0093] where, w h denotes the weight matrix of the fully connected layer, and b h denotes its bias term.
[0094] In file-level prediction, compare the predicted probability with the ground truth of the file to obtain various evaluation metrics and results. For line-level prediction, use the attention scores for prediction, and the code lines or tokens with higher attention scores are considered to have a higher risk probability.
[0095] In line-level prediction, first extract all the predicted defective code files, then extract the token-level attention scores from these files, sort them in descending order, and select the top-k scores for subsequent analysis. Then group the top-k scores according to their corresponding code lines, calculate the sum of the scores for each group, and if the tokens are not in the top-k, count the risk score as 0. Furthermore, obtain the risk score for each line, sort it in descending order, and conduct further analysis and evaluation.
[0096] Finally, it should be noted that: the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements on some or all of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A multi-granularity defect prediction method based on graph convolutional network, characterized in that, It includes the following steps: S1. Preprocess the dataset to obtain preprocessed data; S2. Obtain an embedding matrix based on the preprocessed data, and use the embedding matrix to map each token in the code file to obtain code tokens; S3. Based on the code tokens, construct a line-level graph and a token-level graph using a sliding window of a fixed size; S4. Input the obtained token-level graph and line-level graph into a multi-granularity defect prediction component for prediction.
2. The multi-granularity defect prediction method based on graph convolutional network according to claim 1, characterized in that S1 specifically includes the following steps: Delete blank lines from the code in the dataset, remove special characters, leave useful code tokens, and eliminate noisy data.
3. The multi-granularity defect prediction method based on graph convolutional network according to claim 1, characterized in that S2 specifically includes the following steps: Obtain the vector representation of the code tokens; map each token to the embedding space, and train each different project separately to obtain a unique token embedding matrix for each different project.
4. The multi-granularity defect prediction method based on a graph convolutional network according to claim 1, characterized in that S3 specifically includes the following steps: Let the window size be n. For each line of code, include the first n tokens in window w1, then slide the window one token to the right, including the second to the n+1th tokens in window w2, until the window contains the last token; these windows of length n form a set of sliding windows; Next, traverse the set of sliding windows, considering that all nodes appearing in the current window are connected by an edge, to obtain the token-level graph of the current code line; For a file, assign consecutive indexes starting from 0 in the order in which each line of code appears; Use a sliding window with a fixed window size to slide from the beginning to the end of the file to generate a series of sliding windows containing index numbers; traverse the sliding windows containing index numbers, and consider all lines of code within the current window as connected by an edge to obtain the line-level graph.
5. The multi-granularity defect prediction method based on a graph convolutional network according to claim 1, characterized in that In S4, the multi-granularity defect prediction component includes a token-level graph encoder, a token-level attention layer, a line-level graph encoder, and a line-level attention layer; The token-level graph encoder is as follows: For the i-th line of code, denoted as L i ; the token-level graph encoder has two inputs: the first input of the token-level graph encoder is the features of the nodes in the token-level graph of this line of code, denoted as V li = [vect i1 , vect i2 ,... vect iN , where N is the number of nodes in the graph and also the number of tokens in this line; The second input of the token-level graph encoder is the adjacency matrix in the token-level graph, denoted as A. The token-level encoder propagates the first input of the token-level graph encoder and the second input of the token-level graph encoder to integrate the context connections between each token: Among them, I is an N*N identity matrix, is the degree matrix of t the weight matrix that is continuously adjusted during the training process; The token-level attention layer is as follows: Adopt an attention mechanism to obtain the importance of each token, and use the importance of each token to obtain the line-level characteristics of the code. Use a multi-layer perceptron to obtain the output hidden state representation from the token-level graph convolutional layer: u it = tanh(W ω h it + b w ) Among them, W ω is the weight matrix of the MLP, and b w is the bias term; Multiply the output of the MLP by the token-level context vector u w , and calculate the attention scores for each token using the softmax function: where α it represents the attention score of the t-th token in the i-th row; To obtain the vector representation of each line of code, use the attention score of each token in the line and the output vector of the token-level graph encoder to recalculate the hidden layer vector representation of each token, and sum the vectors in the same line to obtain the vector representation of each line of code: s i = ∑ t α it h it The node features of the line-level graph represented by the obtained file are V fi = [s1, s2,..., s N ; The line-level graph encoder is as follows: For a file, the line-level graph encoder has two inputs: the first input to the line-level graph encoder is the node features in each line-level graph, where N is the number of nodes in the file; the second input to the line-level graph encoder is the adjacency matrix A in the line-level graph. The line-level graph encoder propagates these two inputs to integrate the contextual connections between code lines: Among them, W l is the weight matrix adjusted in the training phase, and H fi = [h1, h2, h3,..., h N represents the node feature matrix of the hidden layer and is passed as input to the row attention layer; The line-level attention layer is as follows: Use a single-layer MLP to obtain the hidden state output by the line-level graph encoder: u i = tanh(W s h i + b s ) Among them, W s represents the weight matrix, and b s represents the bias term; Multiply the line-level context vectors and use the softmax function to obtain the attention score of each line of code: Multiply the output of the line-level graph encoder by the attention score of each line of code to reassign the vector representation of the code line; aggregate the vector representations of the code lines within the same file to obtain the vector representation of the code file as the high-dimensional feature of the file: e i = ∑ t α i h i Feed the vector representation of the file to a fully connected layer to obtain the predicted label for each file; calculate the final predicted probability of the existence of defects in the file through the sigmoid activation function: Among them, w h represents the weight matrix of the fully connected layer, and b h represents the bias term.
6. The multi-granularity defect prediction method based on a graph convolutional network according to claim 5, wherein In S4, in file-level prediction, compare and analyze the predicted probability with the groundtruth of the file to obtain evaluation metrics and results; for line-level prediction, use the attention score for prediction, and the code lines or tokens with higher attention scores are considered to have a higher risk probability; In line-level prediction, extract all the predicted defective code files, sort the token-level attention scores extracted from the defective code files in descending order, and select the top k scores for subsequent analysis; group the top-k scores according to the code lines corresponding to the top k scores, calculate the sum of scores for each group, and if the labels are not in the top-k, count the risk score as 0; obtain the risk score for each line, sort it in descending order, and conduct further analysis and evaluation.
7. A storage medium, characterized in that, The storage medium includes a stored program, wherein when the program runs, it executes the multi-granularity defect prediction method based on a graph convolutional network according to any one of claims 1 to 6.
8. An electronic device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor runs and executes the multi-granularity defect prediction method based on a graph convolutional network according to any one of claims 1 to 6 through the computer program.
Citation Information
Cited By
Double-branch row-level defect prediction method based on structure-guided attention mechanism
CN122412301A