Software defect prediction method based on local code features and long-distance dependencies and computer-readable storage medium
The method enhances software defect prediction by using CNN to extract local features and two-layer BiLSTM to capture long-distance dependencies, improving accuracy and reliability in defect detection.
Patent Information
- Application Number
- JP2025203063
- Authority / Receiving Office
- JP · JP
- Patent Type
- Patents
- Current Assignee / Owner
- Priority Date
- 2024-12-12
- Filing Date
- 2025-11-25
- Publication Date
- 2026-02-17
- Estimated Expiration
- 2045-11-25
AI Technical Summary
Existing software defect prediction methods struggle with capturing comprehensive contextual information, particularly long-distance dependencies and complex control flows, leading to poor performance in large-scale data analysis.
A software defect prediction method utilizing CNN to extract local code features and two-layer BiLSTM to capture long-distance dependencies, enhancing the model's understanding and representation ability.
Improves the accuracy and reliability of defect prediction by effectively capturing local features and long-distance dependencies, demonstrating high F1 scores in various software projects.
Smart Images

Figure 0007814794000001_ABST
Abstract
Description
[Technical Field]
[0001] The present invention belongs to the technical fields of software security and machine learning, and specifically relates to a software defect prediction method based on local features and long-distance dependencies of code. [Background technology]
[0002] Software defect prediction is an important step in the software development and maintenance process. By predicting and identifying potential software defects, the development team can fix these defects before releasing the software, thereby improving the quality of the final product. It also helps project managers identify areas or modules that are likely to cause problems, allowing them to allocate limited testing and development resources more effectively and ensure that critical areas receive sufficient attention. By identifying and resolving problems in advance, it reduces rework in the development process, thereby shortening the development cycle. It also helps identify high-risk software components, allowing the project team to take preventive measures and prevent potential risks from becoming actual problems. This is not only related to the quality and reliability of software, but also key to improving development efficiency and reducing costs.
[0003] Some studies have used manually designed static metrics to build predictors. These traditional features mainly focus on statistical features of programs, such as Halstead features based on operators and operands, McCabe features based on dependencies, and CK features for object-oriented programs. However, traditional features that perform well in one project may not perform well in another. Furthermore, traditional defect prediction methods are complex and perform poorly when processing large-scale data.
[0004] In addition to the features represented by a set of code metrics, source code, as a unique text, also contains rich syntactic and semantic information. Ignoring contextual information can significantly affect program comprehension. An abstract syntax tree (AST) is an abstract representation of the syntactic structure of source code. It can describe program structure in the form of a tree, with each node representing a structure or symbol in the source code. Some researchers have verified that combining semantic features with traditional features is superior to using traditional features alone in defect prediction. However, while the selected prediction model can capture deep features, it is insufficient to capture all the semantic information in the AST. Especially when it comes to long-distance dependencies and complex control flows, local features provide detailed information, while long-distance dependencies ensure that the model can understand the overall structure and logic. The lack of local features and long-distance dependencies can render the model ineffective when analyzing complex sequences, resulting in a significant decline in the model's overall performance. Summary of the Invention [Problem to be solved by the invention]
[0005] In response to the shortcomings of the prior art, this invention proposes a software defect prediction method based on local code features and long-distance dependencies, using CNN to deeply analyze the code structure and extract local features, and two-layer BiLSTM to capture long-distance dependencies, thereby better understanding and extracting higher-level context information, enhancing the model's expressive ability, and thereby improving the accuracy and reliability of prediction. [Means for solving the problem]
[0006] A software defect prediction method based on local code features and long-distance dependencies, specifically including the following steps:
[0007] Step 1: Extract traditional and semantic features from the source code programs and defect reports of the original software defect dataset, label the defects, and perform imbalance processing and standardization processing.
[0008] Step 2: Temporal convolutional neural network (TCNN) is used to initially train and select the extracted semantic features, and retain the latent, defect-related features to improve the accuracy and robustness of subsequent software defect prediction.
[0009] Step 3: The semantic features retained in step 2 are merged with the traditional features to form a feature matrix of the target dimension.
[0010] Step 4: One CNN layer is used to extract local features from the target-dimensional feature matrix obtained in Step 3, followed by two consecutive BiLSTM layers to capture long-distance dependencies in the time sequence, helping the model better understand and extract higher-level context information and enhancing the model's representation ability. Finally, a dense layer is used to integrate the features of the second BiLSTM layer, and a weighted sum is performed on this information, finally outputting a binary classification result using a sigmoid activation function.
[0011] Step 5: Code defect prediction can be considered as a binary classification problem. Therefore, we use a weighted binary cross-entropy loss function to adjust the weight of the positive class samples, i.e., the defect samples, to improve the model's discrimination ability for this class of samples. At the same time, we select the Adam optimizer to train the model.
[0012] Step 6: Extract semantic features and traditional features from the source code program to be detected and its defect report, select the semantic features using a temporal convolutional neural network, and then merge them with the traditional features. Then, input them into the model trained in Step 5, and output the defect prediction results. [Effects of the Invention]
[0013] Compared with the prior art, the present invention has the following beneficial effects:
[0014] 1. CNNs use local sensing and weight sharing mechanisms to effectively extract local features from input sequences. In code analysis, local features may include specific code modes, structures, syntax errors, etc. The features extracted by the convolutional layer can reflect information such as the frequency of code use and structural complexity, helping the model identify potential defects.
[0015] 2. In code, function calls, variable scopes, etc. often span long distances, and this information is crucial for defect prediction. The special structure of BiLSTM can capture long-distance dependencies in time sequence data. The first BiLSTM layer can learn local context information in the input sequence and simultaneously capture forward and backward dependencies. The second BiLSTM layer can help the model better understand and extract higher-level context information, enhancing the model's representation capability. Therefore, two consecutive BiLSTM layers can simultaneously consider past and future context information, especially as the network depth increases, and can learn more complex and abstract feature representations and better model complex long-distance dependencies, thereby better representing the code structure and logic. This provides a more comprehensive understanding and improves the adaptability of the model to complex code logic.
[0016] 3. The representation learned by the first BiLSTM layer is based on the original input features, while the second BiLSTM layer is based on the output of the first layer. This layer-by-layer information fusion contributes to capturing richer semantic information. Particularly in sequence tasks, the second BiLSTM layer further enhances information flow transmission, thereby strengthening the model's understanding of sequences. By combining the local features extracted by CNN with the long-range dependencies captured by BiLSTM, the model can gain a more comprehensive understanding of the input data. This combination allows the model to not only focus on a single feature, but also consider the dynamic relationships between features and their changes over time. [Brief explanation of the drawings]
[0017] [Figure 1] 1 is a flowchart of a software defect prediction method based on local features and long-distance dependencies of code. [Figure 2] FIG. 1 is a structural schematic diagram of a model constructed in an embodiment that captures local features and long-distance dependencies of code. DETAILED DESCRIPTION OF THE INVENTION
[0018] The present invention will now be further described with reference to the drawings.
[0019] As shown in Figure 1, the software defect prediction method based on local code features and long-distance dependencies improves the ability of software defect prediction by capturing local code features and long-distance dependencies. Specifically, it includes the following steps:
[0020] Step 1: This example uses the camel, Jedit4, log4j1, lucene, poi, synapse, xalan, and xerces datasets as the original software defect datasets, and extracts module instance names from traditional feature files. The source code is analyzed, and AST (Abstract Syntax Tree) sequences are extracted. The AST sequences are then converted into integer vectors called tokens, allowing subsequent models to use numerical semantic features.
[0021] Semantic token sequences and traditional features are extracted from the traditional feature file and the coded tokens, and defect labels are assigned.
[0022] The traditional features include statistical data such as code metrics, defect history, development process, and version control, while the semantic features include information such as program structure, data flow, design mode, and code similarity.
[0023] Finally, we perform imbalance and standardization processes, and then oversample the xalan dataset using the RandomOverSampler method, and the remaining datasets using the SMOTE method.
[0024] Step 2: Temporal convolutional neural networks (TCNNs) are used to initially train and select the extracted semantic features. TCNNs can handle time-varying data, enhance prediction stability, and maintain efficient performance even in the presence of noise or missing data. For each training batch, forward propagation is performed on the input semantic token sequence, and potential defect-related features are output and retained, using binary cross-entropy loss (Loss). BCE Select to calculate the loss value.
[0025]
number
[0026]
number
[0027]
number
[0028] We use the Adam optimizer to optimize the model parameters, tuning the learning rate and other hyperparameters. We store the semantic features retained after training as a numpy array.
[0029] Step 3: Using hstack to horizontally merge the semantic features and traditional features, a target-dimensional feature matrix is formed, which contains 180-dimensional semantic features, 20-dimensional traditional features, and 1-dimensional labels.
[0030] Step 4: As shown in Figure 2, a model including a CNN layer and a double BiLSTM layer is constructed to capture the local features and long-range dependencies of the code. The specific steps are as follows:
[0031] In step s4.1, the convolution layer (Conv1D) performs a one-dimensional convolution operation on the input feature matrix of the target dimension to extract local features. In code, certain code fragments, such as syntax structures and common function calls, may be distinguishable features. By sliding the convolution kernel over the input data, it can capture specific modes or features, and learn these local features to improve classification accuracy.
[0032] s4.2, Batch normalization layer standardizes the output of the convolution layer to generate standardized feature maps, and adjusts and scales the output of the activation function, thereby effectively reducing the problem of internal covariate shift. Therefore, batch normalization can not only accelerate the model training process, but also improve the adaptability of the model to different data distributions and the stability of the entire network.
[0033] In s4.3, a max pooling layer (Max Pooling1D) further processes the feature map output by the batch normalization layer, reducing the dimensionality of the feature map while retaining the most salient features to generate a pooled feature map. By retaining the maximum value, the max pooling layer allows the model to focus attention on the strongest local features, thereby reducing unnecessary noise.
[0034] s4.4, the pooled feature map is input to the first BiLSTM layer, which can transmit information over a relatively long time step, memorize important information in the previous sequence, and influence subsequent outputs, thereby capturing the temporal dependencies in the sequence. The second BiLSTM layer is based on the output of the first BiLSTM layer to better understand and extract higher-level context information and enhance the model's expressive capabilities.
[0035] In sequence tasks, the second BiLSTM layer further enhances the transmission of information flow, thereby strengthening the model's understanding of sequences. Therefore, two consecutive BiLSTM layers process features at different levels, allowing the model to better model complex long-range dependencies and enhance the model's generalization ability. Furthermore, double BiLSTM layers can better transmit gradients.
[0036] s4.5, one dense layer is used to integrate the features output by the second BiLSTM layer, and a weighted sum is performed on the information output by the second BiLSTM layer, and finally a binary classification result is output using a sigmoid activation function.
[0037] Step 5: To further solve the data imbalance problem, we use the weighted binary cross-entropy loss function Loss in the model training stage.
[0038]
number
[0039] where N is the number of samples and y true,i represents the true label of the i-th sample, and y true,i = 1 indicates that the sample is a positive class, i.e., a defective sample, and y true,i = 0 indicates that the sample is a negative class, i.e., a sample without defects. pred,i represents the predicted probability of the i-th sample output by the model, and its value ranges from 0 to 1. pos_weight represents the weight value corresponding to the positive class sample. By adjusting the weight pos_weight of the positive class sample, the model's discrimination ability for classes with low frequency of occurrence can be improved.
[0040] The Adam optimizer is selected to train the model, and 5-fold cross-validation is used to ensure the model has good generalization ability and stability. At each fold, the model is trained using the training data for the current fold and evaluated on the validation data. ReduceLROnPlateau is used to fine-tune the model parameters by reducing the learning rate if the validation loss no longer improves. ModelCheckpoint is used to save the model that performs best in validation loss, and terminates training early if the validation loss does not improve within a certain number of epochs.
[0041] To demonstrate the effectiveness of this method, we conducted comparative experiments with conventional methods in related art. The F1 score was selected as the evaluation metric, and the higher the F1 score, the better the model's predictive performance. The experimental results are shown in Table 1.
[0042] [Table 1]
[0043] Compared with conventional techniques, the F1 score of our method is significantly improved. In particular, in the Jedit4 project, the F1 score of our method reached 0.9137, which is much higher than the 0.64 of DP-GCN and the 0.711 of ECNN, demonstrating its efficient adaptability to defect prediction tasks. In addition, in the log4j1 project, our method achieved a nearly perfect score of 0.9974, achieving nearly perfect and accurate defect prediction, demonstrating its excellent predictive ability.
[0044] Furthermore, experimental results show that although the ECNN model has performed well in some projects, it focuses on long-distance dependencies to predict defects, and does not pay enough attention to the hierarchical structure of the code. On the other hand, DP-GCN shows certain advantages when processing graph structure data, and PSFM excels at extracting information from the semantic level of the code. However, our method shows high robustness and efficiency in software projects of various scales and types, which indicates that our method can accurately identify potential defect modes and has high prediction accuracy.
Claims
1. A software defect prediction method based on local code features and long-distance dependencies, specifically comprising: Step 1: extracting traditional and semantic features from source code programs and defect reports and labeling the defects; Step 2: Selecting extracted semantic features using a pre-trained temporal convolutional neural network; Step 3: Merging the semantic features selected in step 2 with traditional features to form a target-dimensional feature matrix; Step 4: Use one CNN layer to extract local features for the target-dimensional feature matrix obtained in step 3, then use two consecutive BiLSTM layers to capture long-distance dependencies in the time sequence, then use a dense layer to integrate the features output by the second BiLSTM layer, and finally output the predicted defect prediction result using a sigmoid activation function. The specific process is as follows: s4.1, the convolution layer performs one-dimensional convolution on the input target dimension feature matrix to extract local features; s4.
2. Standardize the output of the convolutional layer using a batch normalization layer to generate standardized feature maps; s4.
3. The feature map output by the batch normalization layer is further processed by a max pooling layer to reduce the dimension of the feature map while retaining the most salient features, and generate a pooled feature map; s4.4, the pooled feature map is input to the first BiLSTM layer to capture the temporal dependency in the sequence, and the second BiLSTM layer further enhances the information flow propagation based on the output of the first BiLSTM layer; Step 4: s4.5 uses one dense layer to integrate the features output by the second BiLSTM layer, performs weighted summation on the information output by the second BiLSTM layer, and finally outputs the binary classification result using the sigmoid activation function. Step 5, comparing the defect prediction results predicted in step 4 with the defect labels, and training parameters of the model in step 4, using a weighted binary cross-entropy loss function Loss as follows: [Equation 1] where N represents the number of samples input to the model, and y true,i represents the true label of the i-th sample, and y true,i = 1 indicates that the sample is a positive class, i.e., a defective sample, and y true,i = 0 indicates that the sample is a negative class, i.e., a defect-free sample, and y pred,i Step 5, where pos_weight is the weight value corresponding to the positive class sample, and pos_weight is the predicted probability of the i-th sample output by the model; A software defect prediction method based on local features and long-distance dependencies of code, comprising: extracting semantic features and traditional features from a source code program to be detected and its defect report; selecting the semantic features using a pre-trained temporal convolutional neural network in step 2; merging the semantic features with the traditional features; and inputting the semantic features into a model trained in step 5; and outputting defect prediction results.
2. The traditional features include code metrics, defect history, development process, and version control content, while the semantic features include program structure, data flow, design mode, and code 2. The method for software defect prediction based on local code features and long-distance dependencies according to claim 1, further comprising:
3. 2. The software defect prediction method based on local features and long-distance dependencies of code according to claim 1, further comprising: using a Random OverSampler method or a SMOTE method to perform oversampling on the extracted features and to perform imbalance processing and standardization processing on the data.
4. 2. The software defect prediction method based on local features and long-distance dependencies of code according to claim 1, further comprising: using a temporal convolutional neural network to predict the influence probability of different semantic features on defect prediction results.
5. Binary cross entropy loss BCE Calculate the loss value of the temporal convolutional neural network using [Equation 2] The method for software defect prediction based on local features and long-distance dependencies of code according to claim 4, characterized in that the Adam optimizer is used to optimize parameters of the temporal convolutional neural network.
6. A computer-readable storage medium having a computer program stored thereon, the computer program causing the computer to carry out the method according to any one of claims 1 to 5 when the computer program is executed on the computer.
Citation Information
Patent Citations
Software defect prediction method based on position-sensitive hierarchical attention network
CN116932382A
Cross-project software defect number prediction method based on transfer learning
CN117056226A