Software defect prediction method based on code local features and long-distance dependency relationship
By combining CNN and dual-layer BILSTM methods, local features of the code are extracted and long-distance dependencies are captured, which solves the problem that the existing technology is difficult to effectively deal with complex and large-scale data, and improves the accuracy and reliability of software defect prediction.
Patent Information
- Application Number
- CN202411831537.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2044-12-12
AI Technical Summary
Existing software defect prediction methods do not perform well when processing complex, large-scale data, and are difficult to effectively capture semantic information of long-distance dependencies and complex control flows in abstract syntax trees (ASTs).
Using CNN and dual-layer BILSTM methods, local features of the code are extracted through CNN and long-distance dependencies are captured through dual-layer BILSTM to enhance the context understanding ability of the model.
It improves the accuracy and reliability of software defect prediction, especially when dealing with complex code logic, the model's performance ability is significantly enhanced, and it can understand the code structure and logic more comprehensively.
Smart Images

Figure CN119783111A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of software security and machine learning, and in particular relates to a software defect prediction method based on local features of codes and long-distance dependencies. Background Art
[0002] Software defect prediction is a key link in the software development and maintenance process. By predicting and identifying potential software defects, the development team can fix these defects before the software is released, thereby improving the quality of the final product; helping project managers identify which parts or modules are more likely to have problems, so as to more effectively allocate limited testing and development resources and ensure that key parts receive adequate attention; identifying and solving problems in advance can reduce rework during the development process, thereby shortening the development cycle; and helping to identify high-risk software components so that the project team can take preventive measures to avoid potential risks from turning into actual problems. It is not only related to the quality and reliability of software, but also the key to improving development efficiency and reducing costs.
[0003] In some studies, manually designed static indicators are used to construct predictors. These traditional features mainly focus on the statistical characteristics 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 a certain project may not perform well in other projects. In addition, traditional defect prediction methods are often unable to cope with complex and large-scale data.
[0004] In addition to the features represented by a series of code metrics, source code as a unique text also contains rich grammatical and semantic information. Ignoring contextual information may significantly affect the understanding of the program. The abstract syntax tree (AST) is an abstract representation of the grammatical structure of the source code, which can describe the program structure in the form of a tree, where each node represents a structure or symbol in the source code. Some researchers have verified that the joint semantic features and traditional features are better than using only traditional features in defect prediction. However, although the selected prediction model can capture deep features, it is not enough 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 may make the model ineffective when analyzing complex sequences, resulting in a significant decrease in the overall performance of the model. Summary of the invention
[0005] In view of the shortcomings of the prior art, the present invention proposes a software defect prediction method based on local code features and long-distance dependencies. It uses CNN to deeply analyze the code structure and extract local features, and uses a double-layer BILSTM to capture long-distance dependencies, so as to better understand and extract higher-level contextual information, enhance the performance of the model, and thus improve the accuracy and reliability of the prediction.
[0006] The software defect prediction method based on local code features and long-distance dependencies specifically includes the following steps: Step 1: Extract traditional features and semantic features from the source code program and defect report of the original software defect dataset, annotate the defect labels, and perform imbalance and standardization processing.
[0007] Step 2: Use the temporal convolutional neural network (TCNN) to perform preliminary training and selection on the extracted semantic features, retaining potential defect-related features to improve the accuracy and robustness of subsequent software defect prediction.
[0008] Step 3: Merge the semantic features retained in step 2 with the traditional features to form a feature matrix of the target dimension.
[0009] Step 4: Use a CNN layer to extract local features from the feature matrix of the target dimension obtained in step 3, and then pass it through two consecutive BILSTM layers to capture long-distance dependencies in the time series, helping the model to better understand and extract higher-level context information and enhance the model's performance. Finally, use a dense layer to integrate the features of the second BILSTM layer, perform weighted aggregation on this information, and finally output the binary classification result through the sigmoid activation function.
[0010] Step 5: Code defect prediction can be regarded as a binary classification problem. Therefore, the weighted binary cross entropy loss function is used to adjust the weights of positive samples, i.e. defect samples, to improve the model's ability to identify such samples. At the same time, the Adam optimizer is selected for model training.
[0011] Step 6: Extract semantic features and traditional features from the source code program to be tested and its defect report, select the semantic features through the temporal convolutional neural network, merge them with the traditional features, and then input them into the model trained in step 5 to output the defect prediction results.
[0012] Compared with the prior art, the present invention has the following beneficial effects: 1. CNN effectively extracts local features from input sequences through local perception and weight sharing mechanisms. In code analysis, local features may include specific code patterns, structures, syntax errors, etc., and the features extracted by the convolutional layer can reflect information such as the frequency of use and structural complexity of the code, which can help the model identify potential defects.
[0013] 2. In the code, function calls and variable scopes are often long-distance. This information is crucial for predicting defects, and the special structure of BILSTM can capture long-distance dependencies in time series data. Through the first BiLSTM layer, local context information in the input sequence can be learned, and forward and reverse dependencies can be captured at the same time. The second BiLSTM layer can help the model better understand and extract higher-level context information and enhance the performance of the model. Therefore, through two consecutive BILSTM layers, past and future context information can be considered at the same time, especially when the network depth increases, more complex and abstract feature representations can be learned, and complex long-distance dependencies can be better modeled, thereby more comprehensively understanding the structure and logic of the code and improving the model's adaptability to complex code logic.
[0014] 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 helps capture richer semantic information. Especially in sequence tasks, the second BiLSTM layer will further strengthen the transmission of information flow, thereby enhancing the model's understanding of the sequence. By combining the local features extracted by CNN with the long-distance dependencies captured by BiLSTM, the model can understand the input data more comprehensively. This combination allows the model to not only focus on a single feature, but also consider the dynamic relationship between features and their evolution over time. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 The flowchart of the software defect prediction method based on local features of code and long-distance dependencies; Figure 2 Schematic diagram of the model structure for capturing local features and long-distance dependencies of codes established in the embodiment. DETAILED DESCRIPTION
[0016] The present invention will be further explained below with reference to the accompanying drawings; like Figure 1 As shown, 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:
[0017] Step 1. This embodiment uses camel, Jedit4, log4j1, lucene, poi, synapse, xalan, and xerces datasets as original software defect datasets to extract module instance names from traditional feature files. Parse the source code, extract the AST (abstract syntax tree) sequence, and then convert the AST sequence into an integer vector Token, so that the subsequent model can use semantic features in numerical form.
[0018] Extract semantic token sequences and traditional features from traditional feature files and encoding tokens, and annotate defect labels.
[0019] The traditional features include statistical data on code metrics, defect history, development process, version control, etc.; the semantic features include information on program structure, data flow, design pattern, code similarity, etc.
[0020] Finally, imbalance and standardization processing are performed, and the RandomOverSampler method is used for oversampling for the xalan dataset, and the SMOTE method is used for oversampling for the remaining datasets.
[0021] Step 2: Use the temporal convolutional neural network (TCNN) to perform preliminary training and selection on the extracted semantic features. TCNN can handle time-dependent data, enhance the stability of prediction, and maintain efficient performance in the presence of data noise and missing data. In each training batch, the input semantic token sequence is forward propagated, and the output retains the potential, defect-related features, and the binary cross entropy loss is selected. Calculate the loss value:
[0022] Where N is the number of samples, is the true label of the input semantic feature, is the predicted probability output by the TCNN model. The predicted probability output by the TCNN model can measure the contribution of different input features to the final prediction result. For example, by calculating the changes of certain features under different labels, we can understand which features are most helpful for classification tasks. According to the gradient of the loss Update TCNN model parameters:
[0023] The Adam optimizer is used to optimize the parameters of the model, adjust the learning rate and other hyperparameters. The semantic features retained after training are stored as numpy arrays.
[0024] Step 3: Use hstack to merge semantic features with traditional features horizontally to form a feature matrix of the target dimension, including 180-dimensional semantic features, 20-dimensional traditional features, and 1-dimensional labels.
[0025] Step 4: Figure 2 As shown in the figure, a model including a CNN layer and a double BILSTM layer is established to capture the local features and long-distance dependencies of the code. The specific steps are as follows: s4.1. Perform a one-dimensional convolution operation on the input target dimension feature matrix through the convolution layer (Conv1D) to extract local features. In the code, certain specific code snippets such as grammatical structures and common function calls may be recognizable features, and the convolution kernel can capture specific patterns or features by sliding on the input data. These local features can be learned to improve the accuracy of classification.
[0026] s4.2. The output of the convolutional layer is standardized through the batch normalization layer to generate a standardized feature map. By adjusting and scaling the output of the activation function, the internal covariate shift problem is effectively reduced. Therefore, batch normalization can not only accelerate the training process of the model, but also improve the model's adaptability to different data distributions and the stability of the overall network.
[0027] s4.3. The feature map output by the batch normalization layer is further processed through the maximum pooling layer (Max Pooling1D), which reduces the dimension of the feature map while retaining the most significant features to generate a pooled feature map. The maximum pooling layer enables the model to focus on the strongest local features by retaining the maximum value, thereby reducing unnecessary noise.
[0028] s4.4, the pooled feature map is input into the first BILSTM layer, which can transfer information between longer time steps, remember important information in the previous sequence, affect the subsequent output, and thus capture the temporal dependency in the sequence. The second BILSTM layer is based on the output of the first BILSTM layer, and is used to better understand and extract higher-level context information and enhance the performance of the model.
[0029] In sequence tasks, the second BILSTM layer will further strengthen the transmission of information flow, thereby enhancing the model's understanding of the sequence. Therefore, by processing features at different levels through two consecutive BILSTM layers, the model can better model complex long-distance dependencies and enhance the generalization ability of the model. At the same time, the dual BILSTM layers can better transfer gradients.
[0030] s4.5. Use a dense layer to integrate the features output by the second BILSTM layer, perform weighted aggregation on the information output by the second BILSTM layer, and finally output the binary classification result through the sigmoid activation function.
[0031] Step 5: In order to further solve the problem of data imbalance, a weighted binary cross entropy loss function is used in the model training stage. : Where N represents the number of samples, represents the true label of the i-th sample, Indicates that the sample is a positive class, i.e., a defective sample. It means that the sample is a negative sample, i.e. a sample without defects. It represents the predicted probability of the i-th sample output by the model, and its value is between 0 and 1. is the weight value corresponding to the positive sample, by adjusting the weight of the positive sample , which can improve the model's ability to recognize less common categories.
[0032] The Adam optimizer is selected for model training, and 5-fold cross validation is used to ensure that the model has good generalization ability and stability. In each fold, the model is trained with the training data of the current fold and evaluated on the validation data. ReduceLROnPlateau is used to reduce the learning rate when the validation loss is no longer improved to fine-tune the model parameters; ModelCheckpoint is used to save the model with the best performance on the validation loss. If the validation loss does not improve within a certain number of cycles, the training is terminated early.
[0033] To prove the effectiveness of this method, a comparative experiment was conducted with the commonly used methods in existing related technologies. The evaluation index was F1 score. The higher the F1 score, the better the prediction performance of the model. The experimental results are shown in Table 1:
[0034] Table 1 Dataset DP-GCN PSFM ECNN This method camel 0.4014 0.533 0.489 0.5524 Jedit4 0.64 —— 0.711 0.9137 log4j1 0.6835 0.505 0.710 0.9974 lucene 0.7782 0.762 0.796 0.8498 poi 0.7808 0.782 0.700 0.9038 synapse 0.5714 0.503 0.688 0.7411 xalan 0.6492 0.669 0.834 0.8267 xerces 0.359 —— 0.777 0.7482 average 0.6079 0.626 0.713 0.8166 Compared with the existing technology, the F1 score of this method has been greatly improved, especially in the Jedit4 project, the F1 score of this method reached 0.9137, which is much higher than 0.64 of DP-GCN and 0.711 of ECNN, showing its efficient adaptability to defect prediction tasks. In addition, in the log4j1 project, this method achieved almost completely correct defect prediction with a near-perfect score of 0.9974, which shows its superior prediction ability.
[0035] In addition, according to the experimental results, it can be seen that although the ECNN model performs well in some projects, it mainly focuses on using long-distance dependencies for defect prediction, and does not pay enough attention to the hierarchical characteristics of the code. DP-GCN shows certain advantages in processing graph structure data, and PSFM is good at mining information from the semantic level of the code. However, this method has shown high robustness and efficiency in software projects of different sizes and types, indicating that this method can accurately identify potential defect patterns, thereby improving the accuracy of prediction.
Claims
1. A software defect prediction method based on local code features and long-distance dependencies, characterized by: The specific steps include: Step 1: Extract traditional features and semantic features from source code programs and defect reports, and annotate defect labels; Step 2: Use the pre-trained temporal convolutional neural network to select the extracted semantic features; Step 3: Merge the semantic features selected in step 2 with the traditional features to form a feature matrix of the target dimension; Step 4: Use a CNN layer to extract local features from the feature matrix of the target dimension obtained in step 3, and then pass it through two consecutive BILSTM layers to capture the long-distance dependencies in the time series; Then, the dense layer is used to integrate the features output by the second BILSTM layer, and finally the predicted defect prediction results are output through the sigmoid activation function; Step 5: Compare the defect prediction result predicted in step 4 with the defect label, and train the model parameters in step 4; Step 6: Extract semantic features and traditional features from the source code program to be tested and its defect report, select the semantic features through the temporal convolutional neural network pre-trained in step 2, merge them with the traditional features, and then input them into the model trained in step 5 to output the defect prediction results.
2. The method for predicting software defects based on local features and long-distance dependencies of code according to claim 1, characterized in that: The traditional features include code metrics, defect history, development process and version control content; the semantic features include program structure, data flow, design pattern and code similarity information.
3. The software defect prediction method based on local code features and long-distance dependencies as claimed in claim 1, characterized in that: Use the RandomOverSampler method or the SMOTE method to oversample the extracted features to complete the imbalance and standardization of the data.
4. The method for predicting software defects based on local features of code and long-distance dependencies as claimed in claim 1, characterized in that: A temporal convolutional neural network is used to predict the probability of different semantic features affecting defect prediction results.
5. The method for predicting software defects based on local features of code and long-distance dependencies as claimed in claim 4, characterized in that: Using binary cross entropy loss Calculate the loss value of the temporal convolutional neural network: Where N is the number of input semantic features, is the true label of the i-th input semantic feature, is the predicted probability of the output of the temporal convolutional neural network; according to the gradient of the loss value Update the temporal convolutional neural network parameters: The Adam optimizer is used to optimize the parameters of the temporal convolutional neural network.
6. The method for predicting software defects based on local features of code and long-distance dependencies as claimed in claim 1, characterized in that: The specific process of outputting the predicted defect prediction results in step 4 is as follows: s4.
1. Perform one-dimensional convolution operation on the input target dimension feature matrix through the convolution layer to extract local features; s4.2, standardize the output of the convolutional layer through the batch normalization layer to produce a standardized feature map; s4.3, further processing the feature map output by the batch normalization layer through the maximum pooling layer, reducing the dimension of the feature map while retaining the most significant features, and generating a pooled feature map; s4.4, input the pooled feature map into the first BILSTM layer to capture the temporal dependencies in the sequence; The second BILSTM layer is based on the output of the first BILSTM layer to further strengthen the transmission of information flow; s4.
5. Use a dense layer to integrate the features output by the second BILSTM layer, perform weighted aggregation on the information output by the second BILSTM layer, and finally output the binary classification result through the sigmoid activation function.
7. The method for predicting software defects based on local features of code and long-distance dependencies as claimed in claim 1, characterized in that: During the model training process in step 5, the weighted binary cross entropy loss function is used : Where N represents the number of samples input to the model. represents the true label of the i-th sample, Indicates that the sample is a positive class, i.e., a defective sample. Indicates that the sample is a negative class, i.e., a sample without defects; Represents the predicted probability of the i-th sample output by the model; is the weight value corresponding to the positive sample.
8. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Multi-scale CNN-BiLSTM non-coding RNA interaction relationship prediction method with introducing attention
CN111341386A
Software defect prediction method based on position-sensitive hierarchical attention network
CN116932382A
Software defect prediction method and model, terminal and storage medium
CN118245373A
Global and local feature reconstruction network-based medical image segmentation method
US20230274531A1
Cited By
Software defect prediction method based on dynamic path adaptive graph convolutional network
CN120705051A