Software Defect Prediction Method Based on Local Code Features and Long-Distance Dependencies
By combining a temporal convolutional neural network and a two-layer bidirectional LSTM model, the local features and long-distance dependencies of the code are captured, which solves the shortcomings of existing models in understanding complex code and achieves more efficient software defect prediction results.
Patent Information
- Application Number
- CN202411831537.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2044-12-12
AI Technical Summary
Existing technologies struggle to effectively utilize local features and long-range dependencies in software defect prediction, resulting in poor model performance when dealing with complex, large-scale data, particularly when understanding complex control flows and long-range dependencies.
Semantic features are extracted using a temporal convolutional neural network (TCNN), and combined with a CNN and a two-layer bidirectional LSTM (BILSTM) model to capture local features and long-range dependencies. The model is trained using a weighted binary cross-entropy loss function and an Adam optimizer to improve prediction accuracy and robustness.
It significantly improves the accuracy of software defect prediction and the adaptability of the model, especially in its performance on complex code logic and projects of different sizes, improves the F1 score, and achieves more efficient defect identification.
Smart Images

Figure CN119783111B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the fields of software security and machine learning technology, specifically relating to a software defect prediction method based on local code features and long-distance dependencies. Background Technology
[0002] Software defect prediction is a crucial step in software development and maintenance. By predicting and identifying potential software defects, development teams can fix these defects before software release, thereby improving the quality of the final product. It helps project managers identify which parts or modules are more likely to have problems, allowing for more effective allocation of limited testing and development resources and ensuring critical components receive sufficient attention. Early identification and resolution of problems reduces rework during development, thus shortening the development cycle. It also helps identify high-risk software components, enabling project teams to take preventative measures and avoid potential risks from becoming actual problems. It not only relates to software quality and reliability but is also key to improving development efficiency and reducing costs.
[0003] Some studies employ manually designed static metrics to construct predictors. These traditional features primarily 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 one project may perform poorly in others. Furthermore, traditional defect prediction methods often struggle with complex, large-scale data.
[0004] Beyond the features represented by a series of code metrics, source code, as a unique text, contains rich syntactic and semantic information. Ignoring contextual information can significantly affect the understanding of the program. An Abstract Syntax Tree (AST) is an abstract representation of the syntactic structure of source code, describing the program structure in tree form, where each node represents a structure or symbol in the source code. Some researchers have verified that joint semantic features and traditional features outperform traditional features alone in defect prediction. However, while the selected prediction model can capture deep features, it is still insufficient to capture all the semantic information in the AST, especially when dealing with long-range dependencies and complex control flow. Local features provide detailed information, while long-range dependencies ensure that the model can understand the overall structure and logic. The lack of local features and long-range dependencies can render the model ineffective when analyzing complex sequences, leading to a significant decline in the overall performance of the model. Summary of the Invention
[0005] To address the shortcomings of existing technologies, this 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 a two-layer BILSTM to capture long-distance dependencies, thereby better understanding and extracting higher-level contextual information, enhancing the model's performance, and improving the accuracy and reliability of predictions.
[0006] The software defect prediction method based on local code features and long-distance dependencies includes the following steps:
[0007] Step 1: Extract traditional and semantic features from the source code and defect reports of the original software defect dataset, label the defects, and perform imbalance and standardization processing.
[0008] Step 2: Use a Temporal Convolutional Neural Network (TCNN) to perform preliminary training and selection on the extracted semantic features, retaining potential features related to defects, in order to improve the accuracy and robustness of subsequent software defect prediction.
[0009] Step 3: Merge the semantic features retained in Step 2 with the traditional features to form a feature matrix of the target dimension.
[0010] Step 4: Use a CNN layer to extract local features from the feature matrix of the target dimension obtained in Step 3. Then, pass it through two consecutive BILSTM layers to capture long-range dependencies in the time series, helping the model better understand and extract higher-level contextual information and enhancing the model's performance. Finally, use a dense layer to integrate the features from the second BILSTM layer, weight and summarize this information, and finally output the binary classification result through the sigmoid activation function.
[0011] Step 5: Defect prediction in code can be viewed as a binary classification problem. Therefore, a weighted binary cross-entropy loss function is used to adjust the weights of positive class samples (i.e., defect samples) to improve the model's ability to identify such samples. Simultaneously, the Adam optimizer is selected for model training.
[0012] Step 6: Extract semantic features and traditional features from the source code program to be detected and its defect report. Select semantic features through a temporal convolutional neural network, merge them with traditional features, and then input them into the model trained in Step 5 to output the defect prediction results.
[0013] Compared with the prior art, the present invention has the following beneficial effects:
[0014] 1. CNNs effectively extract local features from input sequences through mechanisms such as local perception and weight sharing. In code analysis, local features may include specific code patterns, structures, syntax errors, etc., while the features extracted by convolutional layers can reflect information such as the frequency of code usage and structural complexity, helping the model to identify potential defects.
[0015] 2. In code, function calls and variable scopes often involve long distances, which are crucial for predicting defects. The unique structure of BILSTM can capture long-distance dependencies in time-series data. The first BiLSTM layer learns local contextual information from the input sequence and simultaneously captures forward and backward dependencies. The second BiLSTM layer helps the model better understand and extract higher-level contextual information, enhancing its performance. Therefore, two consecutive BILSTM layers can consider both past and future contextual information. Especially with increased network depth, it can learn more complex and abstract feature representations, better model complex long-distance dependencies, and thus gain a more comprehensive understanding of the code's structure and logic, improving the model's adaptability 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 helps capture richer semantic information. Especially in sequence tasks, the second BiLSTM layer further strengthens 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-range dependencies captured by BiLSTM, the model can more comprehensively understand the input data. This combination allows the model to not only focus on individual features but also consider the dynamic relationships between features and their evolution over time. Attached Figure Description
[0017] Figure 1 The flowchart shows a software defect prediction method based on local code features and long-distance dependencies.
[0018] Figure 2 This is a schematic diagram of the model structure for capturing local features and long-distance dependencies of the code established in the embodiment. Detailed Implementation
[0019] The present invention will be further explained below with reference to the accompanying drawings;
[0020] like Figure 1 As shown, a software defect prediction method based on local code features and long-distance dependencies improves the ability to predict software defects by capturing local code features and long-distance dependencies. Specifically, it includes the following steps:
[0021] Step 1: In this embodiment, the camel, Jedit4, log4j1, lucene, poi, synapse, xalan, and xerces datasets are used as the original software defect dataset. Module instance names are extracted from traditional feature files. The source code is parsed to extract the AST (Abstract Syntax Tree) sequence, and then the AST sequence is converted into integer numerical vectors (Tokens) so that subsequent models can use semantic features in numerical form.
[0022] Extract semantic token sequences and traditional features from traditional feature files and encoded tokens, and label them with defect tags.
[0023] 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 patterns, code similarity, etc.
[0024] Finally, imbalance and standardization were performed. The RandomOverSampler method was used for oversampling on the XALAN dataset, and the SMOTE method was used for oversampling on the other datasets.
[0025] Step 2: Initial training and selection of the extracted semantic features are performed using a Temporal Convolutional Neural Network (TCNN). TCNN can handle time-dependent data, enhance prediction stability, and maintain high performance even with data noise and missing data. In each training batch, the input semantic token sequence is forward-propagated, and the output retains potential, defect-related features. A binary cross-entropy loss is selected. Calculate the loss value:
[0026]
[0027] Where N is the number of samples. These are the true labels of the input semantic features. This 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. This is based on the gradient of the loss. Update TCNN model parameters:
[0028]
[0029] The Adam optimizer is used to optimize the model's parameters, adjusting the learning rate and other hyperparameters. The semantic features retained after training are stored as NumPy arrays.
[0030] Step 3: Use hstack to merge the semantic features with the traditional features at the same level to form a feature matrix of the target dimension, including 180-dimensional semantic features, 20-dimensional traditional features, and 1-dimensional labels.
[0031] Step 4, as follows Figure 2 As shown, a model including CNN layers and dual BILSTM layers is built to capture local features and long-range dependencies in code. The specific steps are as follows:
[0032] s4.1. A one-dimensional convolution operation is performed on the input target dimension feature matrix using a convolutional layer (Conv1D) to extract local features. In code, certain specific code snippets, such as syntax structures and common function calls, may be recognizable features. By sliding across the input data, the convolutional kernel can capture specific patterns or features, learn these local features, and improve classification accuracy.
[0033] s4.2. The output of the convolutional layer is standardized by batch normalization to generate a standardized feature map. By adjusting and scaling the output of the activation function, the internal covariate offset 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 overall stability of the network.
[0034] s4.3. The feature maps output by the batch normalization layer are further processed through a max pooling layer (Max Pooling1D) to reduce the dimensionality of the feature maps while retaining the most salient features, generating pooled feature maps. By retaining the maximum value, the max pooling layer allows the model to focus on the strongest local features, thereby reducing unnecessary noise.
[0035] s4.4. The pooled feature map is input into the first BILSTM layer. This BILSTM layer can transfer information over longer time steps, remembering important information from previous sequences and influencing subsequent outputs, thereby capturing temporal dependencies in the sequence. The second BILSTM layer, based on the output of the first BILSTM layer, is used to better understand and extract higher-level contextual information, enhancing the model's performance.
[0036] In sequence tasks, the second BILSTM layer further enhances the transmission of information flow, thereby improving the model's understanding of the sequence. Therefore, by using two consecutive BILSTM layers to process features at different levels, the model can better model complex long-distance dependencies and enhance its generalization ability. Simultaneously, the dual BILSTM layers can better propagate gradients.
[0037] s4.5. Use a dense layer to integrate the features output by the second BILSTM layer, perform weighted summation of the information output by the second BILSTM layer, and finally output the binary classification result through the sigmoid activation function.
[0038] Step 5: To further address the data imbalance issue, a weighted binary cross-entropy loss function is used during the model training phase. :
[0039]
[0040] Where N represents the number of samples, This represents the true label of the i-th sample. This indicates that the sample is a positive class, i.e., a defective sample. This indicates that the sample is a negative class, i.e., a defect-free sample. This represents the predicted probability of the i-th sample output by the model, and its value is between 0 and 1. These are the weight values corresponding to the positive class samples. The weights of the positive class samples are adjusted accordingly. This can improve the model's ability to identify less frequently occurring categories.
[0041] The Adam optimizer is selected for model training, and 5-fold cross-validation is used to ensure good generalization ability and stability. In each fold, the model is trained on 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 no longer improves, so as to fine-tune the model parameters; ModelCheckpoint is used to save the model that performs best on the validation loss. If the validation loss does not improve within a certain number of epochs, training is terminated early.
[0042] To demonstrate the effectiveness of this method, comparative experiments were conducted with commonly used methods in existing related technologies. The F1 score was used as the evaluation metric; a higher F1 score indicates better predictive performance. The experimental results are shown in Table 1.
[0043] Table 1
[0044] 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
[0045] Compared with existing technologies, our method achieves a significant improvement in F1 score, particularly in the Jedit4 project, where it reaches 0.9137, far exceeding DP-GCN's 0.64 and ECNN's 0.711, demonstrating its highly efficient adaptability to defect prediction tasks. Furthermore, in the log4j1 project, our method achieves almost perfectly accurate defect prediction with a near-perfect score of 0.9974, showcasing its superior predictive ability.
[0046] Furthermore, the experimental results show that while the ECNN model performs well in some projects, it primarily focuses on using long-range dependencies for defect prediction, neglecting the hierarchical structural features of the code. DP-GCN demonstrates advantages in handling graph-structured data, while PSFM excels at extracting information from the semantic level of the code. However, our proposed method exhibits high robustness and efficiency across software projects of varying sizes and types, indicating that it can accurately identify potential defect patterns, thereby improving prediction accuracy.
Claims
1. A software defect prediction method based on local code features and long-distance dependencies, characterized in that: Specifically, the following steps are included: Step 1: Extract traditional and semantic features from the source code and defect reports, and label the defects. Step 2: Select the extracted semantic features using a pre-trained temporal convolutional neural network; Step 3: Merge the semantic features selected in Step 2 with the traditional features to form a feature matrix for 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, then pass it through two consecutive BILSTM layers to capture long-range dependencies in the time series; then use a dense layer to integrate the features output by the second BILSTM layer, and finally output the predicted defect prediction result through the sigmoid activation function; the specific process is as follows: s4.
1. Perform a one-dimensional convolution operation on the input target dimension feature matrix through a convolutional layer to extract local features; s4.
2. The output of the convolutional layer is standardized by using a batch normalization layer to generate a standardized feature map; s4.
3. The feature maps output by the batch normalization layer are further processed by the max pooling layer to reduce the dimension of the feature maps while retaining the most significant features, and generate pooled feature maps. s4.4 Input the pooled feature map into the first BILSTM layer to capture the temporal dependencies in the sequence; The second BILSTM layer, based on the output of the first BILSTM layer, further enhances the transmission of information flow; s4.
5. Use a dense layer to integrate the features output by the second BILSTM layer, perform weighted summation on the information output by the second BILSTM layer, and finally output the binary classification result through the sigmoid activation function; Step 5: Compare the defect prediction results from Step 4 with the defect labels, and train the model parameters from Step 4; use the weighted binary cross-entropy loss function Loss: Where N represents the number of samples input to the model, y true,i Let y represent the true label of the i-th sample. true,i =1 indicates that the sample is a positive sample, i.e., a defective sample. true,i =0 indicates that the sample is a negative class, i.e., a defect-free sample; y pred,i Pos_weight represents the predicted probability of the i-th sample output by the model; Pos_weight is the weight value corresponding to the positive class sample. Step 6: Extract semantic features and traditional features from the source code program to be detected and its defect report. Select semantic features through the temporal convolutional neural network pre-trained in Step 2, then merge them with traditional features, and then input them into the model trained in Step 5 to output the defect prediction results.
2. The software defect prediction method based on local code features and long-distance dependencies as described in 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 patterns, and code similarity information.
3. The software defect prediction method based on local code features and long-distance dependencies as described in claim 1, characterized in that: The RandomOverSampler or SMOTE methods are used to oversample the extracted features, thus completing the imbalance and standardization of the data.
4. The software defect prediction method based on local code features and long-distance dependencies as described in claim 1, characterized in that: Temporal convolutional neural networks are used to predict the probability of the influence of different semantic features on the defect prediction results.
5. The software defect prediction method based on local code features and long-distance dependencies as described in claim 4, characterized in that: Loss is calculated using binary cross-entropy loss. BCE Calculate the loss value of the temporal convolutional neural network: Where N is the number of input semantic features, y i It is the true label of the i-th input semantic feature. It is the predicted probability output by the temporal convolutional neural network; based on the gradient of the loss value. Update the parameters of the temporal convolutional neural network: Use the Adam optimizer to optimize the parameters of the temporal convolutional neural network.
6. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to perform the method of any one of claims 1 to 5.
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